Thread: Sine (Sin) Algorithm Help

  1. #31
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    eep. whoops. looks like you're already there. nm.

  2. #32
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by abachler View Post
    I didnt learn how to use the sigma until a few months ago, honestly never saw it through grade school high school, 4 years of college (electronics even) and 10 years in industry. I had to ask a friend of mine what it meant because I was explaining a training concept for neural networks to him and he wanted the formula in that format, lol.
    Seriously? You must live in California. I simply do not know what I would do without summation notation when designing software on paper.

  3. #33
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Nope, Illinois, then Missouri. When we moved down here in '84 I knew more than the teacher in computer class, so much so that he threw me out of class one day because I made him look like an idiot on several occasions. He was prattling on about every command taking exactly 2 bytes or something.

  4. #34
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Good times. Yeah well certainly its no biggy if you weren't familiar with some notation. I am sure someone will jump in and argue that point, but its no thing at all. It would be nice to see books use it more, however. It would serve to closely extend the relationship between math and computer science. In other words, make math seem less scary looking for programmers who aren't as mathematically inclined as you and me.

  5. #35
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Back on topic, can the program use inline assembly? I was thinking an SSE solution

  6. #36
    Registered User
    Join Date
    May 2008
    Posts
    27
    I don't think so. But I'm not 100% sure. (I have no idea what it is actually..lol). But I'm having some issues with the main program. I'll post it and see what you guys think. It gets stuck after I enter in the number to figure out which place I should go in the switch statement. Anyways, here it is.

    Code:
    #include <fstream>    // fstream.h contains file sequential IO methods.
    #include <stdlib.h> // stdlib.h contains the exit method.
    #include <stdio.h>
    #include <string.h>
    #include "mathlib.cpp"
    
    using namespace std;
    
    int main( )
    {
    
    	char fileOutName[25];
    	char response;
    	int n = 0;
    	int choice = 0;
    	int again = 1;
    	float angle = 0.0;
    	float sum = 0.0;
    	float radius = 0.0;
    	float surface = 0.0;
    	float volume = 0.0;
    	float height = 0.0;
    	int filecount = 0;  //The number of lines in the file.
    
    	FILE *outFile;			   // Logical output file name in program.
    
    	//Gets the file out name and then checks to see if it already exists
    	//If so then it asks for you to choose whether or not to override.
    	printf("What is the name of the output file (max 25 characters)?");
    	scanf("%s", &fileOutName);
    	if((outFile = fopen(fileOutName, "r" )) != NULL )
    		{
    			printf("\nA file by the name %s exists.\n", fileOutName);
    			printf("Do you wish to overwrite (Y or N): ");
    			//Skip <cr> in buffer with "%*c", i.e., one character
    			scanf("%*c %c", &response);
    
    			if( (response == 'n') || (response == 'N') )
    			{ // should close file first
    				printf("\nProgram aborted to prevent overwirte!");
    				exit(1);
    			}
    		}
    		outFile = fopen( fileOutName, "wt" );
    		if( outFile == NULL )
    		{
    			printf("Could not create the output file! Program terminating.");
    			exit(1);
    		}
    
    
    		printf("What would you like to do? 1 for Sphere, 2 for Cylinder, 3 for float addition, 4 for sin.\n");
    		scanf("%d", choice);
    		switch( choice )
    		{
    		case 1:
    			printf("What is the radius of the sphere?\n");
    			scanf("%f", radius);
    			sphere(radius, surface, volume);
    			printf("The sphere with a radius of %f has a surface of %f and a volume of %f.\n", radius, surface, volume);
    			fprintf(outFile, "The sphere with a radius of %f has a surface of %f and a volume of %f. \n", radius, surface, volume);
    			printf("Go again? (1 for yes, 2 for no)\n");
    			scanf("%d", again);
    			break;
    		case 2:
    			printf("What is the radius of the cylinder?\n");
    			scanf("%f", radius);
    			printf("What is the height of the cylinder?\n");
    			scanf("%f", height);
    			volume = volCylinder(radius, height);
    			printf("The cylinder with a radius of %f and a height of %f has a volume of %f.\n", radius, height, volume);
    			fprintf(outFile, "The cylinder with a radius of %f and a height of %f has a volume of %f.\n", radius, height, volume);
    			printf("Go again? (1 for yes, 2 for no)\n");
    			scanf("%d", again);
    			break;
    		case 3:
    			printf("How many floats are being processed? \n");
    			scanf("%d", n);
    			float x[10];
    			for (int i = 0; i < n; i++)
    			{
    				printf("What is the float?\n");
    				scanf("%f", x[i]);
    			}
    			for (int j = 0; j < n - 1; j++)
    			{
    				for (int k = j + 1; k < n; k++)
    				{
    					if (x[j] > x[k])
    					{
    						int temp = x[j];
    						x[j] = x[k];
    						x[k] = temp;
    					}
    				}
    			}
    			sum = sumFloats(x, n);
    			printf("The sum of the floats is %f. \n", sum);
    			fprintf(outFile, "The sum of the floats is %f. \n", sum);
    			printf("Go again? (1 for yes, 2 for no)\n");
    			scanf("%d", again);
    			break;
    		case 4:
    			double answer;
    			printf("What is the angle? \n");
    			scanf("%f", angle);
    			answer = sine(angle);
    			printf("The sin of angle %f is %f. \n", angle, answer);
    			fprintf(outFile, "The sin of angle %f is %f. \n", angle, answer);
    			printf("Go again? (1 for yes, 2 for no)\n");
    			scanf("%d", again);
    			break;
    		default:
    			printf("Quitting.");
    			again = 2;
    		}
    
    	fclose(outFile);
    
    	return 0;
    }

  7. #37
    Registered User
    Join Date
    May 2008
    Posts
    27
    Sigh... The sine function didn't work right. Any ideas on how to fix it?

    Code:
    double sine(float angle)
    {
    	#define EPSILON 0.000001
    	int d = 1;
        double guess = 0;
        int i = 1;
        float x2p1 = angle;
        for(guess = angle; guess * guess - angle < EPSILON || guess * guess - angle < -EPSILON; guess = (x2p1 / (d *((i * 2) +1)*(i *2))))
    	{
    		x2p1 = x2p1 * angle * angle * -1;
    		i = i +1;
    	}
        return guess;
    }

  8. #38
    Registered User
    Join Date
    May 2008
    Posts
    27
    Please people, I really really need some help on this, I tried several things and none have worked. Any ideas or any help would be very much appreciated.

  9. #39
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    30
    Quote Originally Posted by StaticKyle View Post
    Please people, I really really need some help on this, I tried several things and none have worked. Any ideas or any help would be very much appreciated.
    Code:
    scanf("&#37;f", angle);
    is it right? are you not supposed to use &
    Code:
    scanf("%f", &angle);
    When using scanf,if the intake variable is not a pointer or an array, then you have to pass the address of the variable to the scanf function. Your entire main program is having this error. Did the other if cases went fine?
    Last edited by chakra; 05-10-2008 at 07:31 PM.

  10. #40
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    30
    scanf("&#37;s", &fileOutName); - not correct.

    fileoutname is an array,so your statement should be

    scanf("%s", fileOutName);

  11. #41
    Registered User
    Join Date
    May 2008
    Posts
    27
    Thank you chakra, I figured out that first one with the & a while back, but I never realised about the fileoutname thing. I had it in almost all of my code and it still worked, so I'll try and remove it and see what happens.

    My biggest problem is the sine function. The rest of the program works great, just not the sine function.

  12. #42
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    n = 0 to infinity
    sine(x) = (-1)^n * (x^(2n+1) / (2n+1)!)

    can be expressed as:
    Code:
    double sine(double x)
    {
        int i;
        int sign = 1;
        double sum = 0.0;
        for (i = 0; i < infinity; ++i)
        {
            sum += sign * (pow(x, 2 * i + 1) / factorial(2 * i + 1));
            sign = -sign;
        }
        return sum;
    }
    given a factorial() function, and with infinity set to some suitable arbitrary value.

    Of course there are problems: it is inefficient since it re-computes the power and factorial from scratch on each iteration. Then, because it computes them separately, they (especially the factorial) will become large very quickly and cause an overflow if infinity is too large. But it shows how easily it is to express a for loop once you express your formula in summation notation. Now, what you should do is fix it as described earlier, and only then do you incorporate your check with an epsilon value instead of an arbitrary value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #43
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    30
    Code:
    double sine(double x)
    {
        int i,n;
        int sign = -1;
        
        double sum = x,power=x,fact = 1.0;
        for (i = 1; i < infinity; ++i)
        {
            power = power * x *x;
            n = (2*i+1); 
            fact = fact * n * (n-1); 
            sum += sign * power/fact;
            sign = -sign;
        }
        return sum;
    }
    I guess this should do. I am just a beginner. So you can just change the code to give a prof. look.

  14. #44
    Registered User
    Join Date
    May 2008
    Posts
    27
    Okay, I implemented yours, laserlight, and I tested it and the answer that I got with setting the loop count or n to 15 wasn't close to the value that I got from my calculated value.

  15. #45
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    You're only going to be able to go up to about 17! before your integer overflows.

    Even something like n=7 should get your decent results for angles less than pi.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM