# Thread: Sine (Sin) Algorithm Help

1. 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. 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

3. 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?

4. Not really, you are still mixing in guess with x - the x value is and should stay the original input angle in radians.

--
Mats

5. 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;
}```

6. Yes, that's looking closer. Does it give you the right value?

--
Mats

7. Well, it isn't compiling, so I'll take it as a no..lol

8. Originally Posted by StaticKyle
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

9. 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

surface = 4 * PI * radSq;
}

{
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 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");
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");
printf("What is the height of the cylinder?\n");
scanf("%f", 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);
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;
}```

--
Mats

11. 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++?

12. 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. 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.

14. 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

15. Originally Posted by laserlight
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.