sin(180) not equal to 0

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
bleeableAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
unknownmatConnect With a Mentor Commented:
Sorry, <tab> doesn't work the same... haha, I should stop trying to write code in a web-based text-editor.

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.
0
 
unknownmatCommented:
What do you need these values for?

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
0
 
bleeableAuthor Commented:
I need the exact value of 0 and I can not do an if statement. I thought of that, but then the question of coterminal angles arises and I have a mess on my hands. I will be displaying these values and that is why I need 0

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
0
The new generation of project management tools

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.

 
unknownmatCommented:
Could you test to see if the output is below a certain threshold?  :)Besides what is wrong with displaying a very tiny number ...that's the true answer that the math library gives.

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
0
 
bleeableAuthor Commented:
Ok I thought of that too, but then I guess here is the real problem, how do I use that same value? For example

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
0
 
unknownmatCommented:
Well, again just probing what you need ... how about writing your own sin function?

ie.
double MySine( double Val )
{
  double Ans = sin( val );
  if( ( val < THRESHOLD && val > 0 ) ||
      ( val > THRESHOLD && val < 0 ) )

}
0
 
bleeableAuthor Commented:
Ok, I deserved that! I'm sorry for saying that, I should have phrased it better. I also didn't think of putting it like that, putting the sin function inside one of my own, I like that, in fact I think I shall use that, so thanks a lot!
0
 
bleeableAuthor Commented:
thanks again
0
All Courses

From novice to tech pro — start learning today.