?
Solved

sin(180) not equal to 0

Posted on 2003-03-20
8
Medium Priority
?
1,433 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:bleeable
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 

Expert Comment

by:unknownmat
ID: 8178949
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
 

Author Comment

by:bleeable
ID: 8178973
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
 

Expert Comment

by:unknownmat
ID: 8179060
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bleeable
ID: 8179074
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
 

Expert Comment

by:unknownmat
ID: 8179108
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
 

Accepted Solution

by:
unknownmat earned 200 total points
ID: 8179120
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
 

Author Comment

by:bleeable
ID: 8179139
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
 

Author Comment

by:bleeable
ID: 8179232
thanks again
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question