Simple sin() problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 5
Reputation: Massena is an unknown quantity at this point 
Solved Threads: 0
Massena Massena is offline Offline
Newbie Poster

Simple sin() problem

 
0
  #1
May 28th, 2009
I hate to be spamming the forums with this, but I've been banging my head at the wall for a few hours now, and I can't really understand what is going on. Here's the full program I'm running:
  1. #include <iostream>
  2. #include "math.h"
  3.  
  4. int main() {
  5. std::cout << sin (3.141592653589793) << "\n";
  6. }
The program is returning 1.22461e-16.
Is this simply because I'm not using enough digits of pi? Then why does cos work flawlessly? Any help is greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 22
Reputation: Topi Ojala is an unknown quantity at this point 
Solved Threads: 5
Topi Ojala's Avatar
Topi Ojala Topi Ojala is offline Offline
Newbie Poster

Re: Simple sin() problem

 
0
  #2
May 28th, 2009
It's not about the digits in your pi. It's just the way sin() function works, not much you can do about it. If you really want the 0.0, write an if
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,491
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Simple sin() problem

 
0
  #3
May 28th, 2009
1.22461e-16 is equal to 0.000000000000000122461, which is pretty close to 0 if you ask me. This is just caused by the precision of the double type and how the data is stored. You could also round the number to remove that problem.
Last edited by William Hemsworth; May 28th, 2009 at 9:02 pm.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: Simple sin() problem

 
0
  #4
May 29th, 2009
It's in the nature of finite precision.
Theres not much you can do about it.

there a basicly two kinds of datatypes.
1. integral
2. float

integral being, int, char, byte etc
floats being, float, double

integral is always precise, floating values are more obscure

What numerical libraries do is to check for equality within a tolerance like
  1. float one = 0;
  2. float two = 0.0000001
  3.  
  4. //wrong
  5. if(one!=two)
  6. puts("not same");
  7.  
  8. //right
  9. #define tole 0.000001
  10. if(fabs(one-two)>tole)
  11. puts("not same")

If you need it depends on your context,
if you just want the value, then you shouldn't care.

But if your program has a branchpoint depending on this value,
you should go the "within tolerance" way.

on a sidenote.
floats are only precise to the 7th or 8th digit,
whereas double are to the 23thd

good luck
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Simple sin() problem

 
0
  #5
May 29th, 2009
May be you still remember (from a school math course) that no fractions which are equal to pi number.
Other post correction: double type provide ~16 decimal digits precision (53 bits of mantissa).
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Simple sin() problem

 
0
  #6
May 29th, 2009
And you should really include <cmath> instead.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 155
Reputation: amrith92 is on a distinguished road 
Solved Threads: 18
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Simple sin() problem

 
0
  #7
May 29th, 2009
Your output is expected, and here's the result of the same when I tried it on Wolfram's Online Math Engine.

Here: The sin() function prototypes
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 5
Reputation: Massena is an unknown quantity at this point 
Solved Threads: 0
Massena Massena is offline Offline
Newbie Poster

Re: Simple sin() problem

 
0
  #8
May 29th, 2009
Ah I see. It's just odd that the cosine of an approximation of pi does return -1, but it's probably just something about the way the math library works.
Anyhow, thanks all!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Simple sin() problem

 
0
  #9
May 29th, 2009
>It's just odd that the cosine of an approximation of pi does return -1
Exactly!
As usually, math libraries (and FPU hardware) implement high precision (~13-15 decimal digits) polynomial approximations of common math functions.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 349 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC