Sine (Sin) Algorithm Help

This is a discussion on Sine (Sin) Algorithm Help within the C Programming forums, part of the General Programming Boards category; Okay, this is my attempt to get the required results that are in the first post: Code: double sin(float angle) ...

  1. #16
    Registered User
    Join Date
    May 2008
    Posts
    27
    Okay, this is my attempt to get the required results that are in the first post:

    Code:
    double sin(float angle)
    {
            double guess;
            for (guess = angle; !(fabs(guess * guess - angle) < EPSILON); guess = ((x * guess * guess * -1) / ((guess * 2) +1)!)
    		{		}
    
            return guess;
    }

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need a new variable (which I called x2p1, but you can call it 'fred', 'a' or something else);
    This variable is set to x to begin with, and each loop [after the calculation of y], you calculate
    Code:
    x2p1 = x2p1 *x * x *-1;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    May 2008
    Posts
    27
    Code:
    double sin(float angle)
    {
            double guess;
            for (guess = angle; !(fabs(guess * guess - angle) < EPSILON); guess = ((guess * x * x * -1) / ((x * 2) +1)!)
    		{		}
    
            return guess;
    }
    Better?
    Last edited by StaticKyle; 05-09-2008 at 07:15 AM. Reason: swapped guess in the later part for x.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not really, you are still mixing in guess with x - the x value is and should stay the original input angle in radians.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    May 2008
    Posts
    27
    Okay.. so... lets try this again. (I'm sorry for this again)

    Code:
    double sin(float angle)
    {
            double guess;
            int i = 1;
            float x2p1 = angle;
            for (guess = angle; !(fabs(guess * guess - angle) < EPSILON); guess = (x2p1 / ((i * 2) +1)!)
    		{
    			x2p1 = x2p1 * angle * angle * -1;
    			i = i +1;
    		}
    
            return guess;
    }
    Last edited by StaticKyle; 05-09-2008 at 07:33 AM.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that's looking closer. Does it give you the right value?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Registered User
    Join Date
    May 2008
    Posts
    27
    Well, it isn't compiling, so I'll take it as a no..lol

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by StaticKyle View Post
    Well, it isn't compiling, so I'll take it as a no..lol
    Probably because your factorial calcuation is incorrect (you just put a ! on the end of the line -that doesn't do it).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    Registered User
    Join Date
    May 2008
    Posts
    27
    Thanks

    But I have a different question now, (because I'm pretty sure I'm still doing something wrong.) I am compiling this program on a windows machine and I am supposed to use a static library which I thought was the .h extension at the end, but I had to change my program to a .c to make it go anywhere. Any ideas?

    Here is my code.
    Code:
    #include <cstdlib>
    #include <math.h>
    #define EPSILON 0.000001
    
    
    
    
    void sphere(float radius, float &surface, float &volume)
    {
         #define PI 3.14159265358979323846264338327950288419716939937510582097494459
    
         float radSq = radius * radius;
    
         surface = 4 * PI * radSq;
         volume = (4 * PI * radius * radSq)/3;
    }
    
    float volCylinder(float radius, float height)
    {
          float volume = (PI * radius * radius * height);
          return volume;
    }
    
    float sumFloats(float x[], int numFloats)
    {
          float sum = 0.0;
    
          for(int i = 0 ; i < numFloats ; i++)
          {
                  sum += x[i];
          }
    
          return sum;
    }
    
    double sine(float angle)
    {
    		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;
    }

    And here is my supposed main program:

    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.c"
    
    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);
    		}
    
    	while (again == 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);
    		if (choice == 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);
    		}
    		else if (choice == 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);
    		}
    		else if (choice == 3)
    		{
    			printf("How many floats are being processed? \n");
    			scanf("%d", n);
    			float x[n];
    			for (i = 0; i < n; i++)
    			{
    				printf("What is the float?\n");
    				scanf("%f", x[i]);
    			}
    			for (j = 0; j < n - 1; j++)
    			{
    				for (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);
    		}
    		else if (choice == 4)
    		{
    			printf("What is the angle? \n");
    			scanf("%f", angle);
    			double answer = sin(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);
    		}
    		else
    		{
    			printf("Quitting.");
    			again = 2;
    		}
    	}
    
    	fclose(outFile);
    
    	return 0;
    }

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should include mathlib.h, and add mathlib.c to your project.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    I am compiling this program on a windows machine and I am supposed to use a static library which I thought was the .h extension at the end, but I had to change my program to a .c to make it go anywhere. Any ideas?
    ".h" is a typical header extension. ".c" is a typical C source file extension. Your code clearly belongs in a source file, so naming the file with a ".c" extension is fine. On the other hand, you want to include a header. As such, create another file and place your function prototypes there, and give it a name with a ".h" extension, and include that file in your main program.

    Incidentally, <cstdlib> is a C++ standard library header. You should #include <stdlib.h>

    EDIT:
    Wait a minute. Your code uses the C++ pass by reference mechanism. Your main program has a using directive. Are you programming in C or C++?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    Registered User
    Join Date
    May 2008
    Posts
    27
    Awesome, thanks.

    Is there any compilers that you guys would recommend to use? I'm using visual studio 8 command prompt and I don't think it is very good, but I've been able to use it before. Anyways, I was just wondering. And if you know how to do a compile of a header file on it then I'd appreciate it.

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Is there any compilers that you guys would recommend to use? I'm using visual studio 8 command prompt and I don't think it is very good, but I've been able to use it before.
    If you are using the MSVC8 compiler, then you probably have the VS 2005 IDE, so why not use that instead?

    And if you know how to do a compile of a header file on it then I'd appreciate it.
    We do not compile header files. What we do compile is a translation unit that includes the source file and whatever headers are included with it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Visual studio is a good IDE and the compiler is also good.
    gcc is a very good compiler (but for Windows, it's usually lagging behind in the gcc-mingw release, versus the Linux/Unix versions), and generally better at following the C/C++ standards.

    compiling multiple source files is usually a case of:
    Code:
    cl -o output.exe main.c myfile.c
    gcc works pretty much the same.

    If you have many source files, you'll want to use -c to compile the source into object files, and then produce the final compile by:
    Code:
    cl -o myprog.exe main.o myfile.o ...
    There's only benefit in this when compiling all the input files start taking more than a second or two.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #30
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by laserlight View Post
    You should have learnt this in elementary mathematics, but perhaps you are not familiar with the term: Summation Notation


    That is a recurrence relation, not a sum expressed in summation notation.
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Page 2 of 4 FirstFirst 1234 LastLast
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, 01:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06: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, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21