With monday.comâ€™s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

I have written a trig calculator however everytime the problem of the sin(180) or any of its multiples arise, I always get a number in scientific notation that is quite close to 0, but not 0. How can I fix this, I am converting from degrees to radians correctly, as all my other values work. Also I am using the math.h file that came with my Borland Compiler.

Thanks for any help

Thanks for any help

If it is essential that the value come out to be 0, you could just do a simple if-statement to test for such special cases before calling sin.

Otherwise, these sort of round-off errors are fairly common for floating point numbers, and unless you need extremely precise calculations for that specific point, I might suggest that your application will hardly notice the difference between 0 and a very small value.

You may also want to be sure that the returned value is not negative (if you would prefer to avoid any sign switches).

Matt

a sample output would be like

sin(30) = 0.5

cos(30) = 0.866025

tan(30) = 0.57735

sec(30) = 1.1547

csc(30) = 2

cot(30) = 1.73205

If someone enters 540 I am in trouble

For instance, when I run sin( M_PI ) on my system I get the value: 1.22461e-16 ... Again, it's application specific but given the values you showed above, I doubt that any value 10^-16 would be of much use for you. You could define a threshold (ie. < 10^-5 ) lower-than-which you don't care to work with.

Actually, the more I think about it the more I like this last approach. Testing the input is inherently flawed ... for example M_PI != PI (the mathematical constant) due to floating point limitations.

Matt

I wrote the cot, csc, sec functions and they all use sin and cos

How do I tell the computer that if(sin(a) <= some number) then sin(a) = 0

Because my other functions depend on the value of sin(a) and they perform sin(a) when needed, they don't get it from a variable that I can set

ie.

double MySine( double Val )

{

double Ans = sin( val );

if( ( val < THRESHOLD && val > 0 ) ||

( val > THRESHOLD && val < 0 ) )

}

All Courses

From novice to tech pro — start learning today.

lemme try again...

ie.

#define THRESHOLD 1e-5f

double MySine( double Val )

{

double Ans = sin( val );

if( ( Ans < THRESHOLD && Ans > 0 ) ||

( Ans > THRESHOLD && Ans < 0 ) )

return 0;

else

return Ans;

}

:)You keep saying that you've thought of all the obvious, solutions, so perhaps this won't help you at all ... but again it's just an idea.