c - Sin function without using math.h library -
i've got assignment fop create scientific calculator, haven't been taught math.h library! basic approach 1 of function sin i'm failing create work
#include <stdio.h> int main() { int input; float pi; double degree; double sinx; long int powerseven; long int powerfive; long int powerthree; input = 5; degree= (input*pi)/180; pi=3.142; powerseven=(degree*degree*degree*degree*degree*degree*degree); powerfive=(degree*degree*degree*degree*degree); powerthree=(degree*degree*degree); sinx = (degree - (powerthree/6) + (powerfive/120) - (powerseven/5040)); printf("%ld", sinx); getchar(); }
your code works. have few problems:
you using pi
before initializing it. suggest using more accurate value of pi
such 3.14159265359
.
powerseven
, powerfive
, powerthree
should defined double
instead of long int
. losing precision storing these values in integer type. also, when split integer value integer value (such powerthree/6
) remainder lost. instance, 9/6
1
.
since sinx
double
should using printf("%f", sinx);
c calculator
No comments:
Post a Comment