943,744 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 591
  • C++ RSS
May 28th, 2009
0

Simple sin() problem

Expand Post »
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:
c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Massena is offline Offline
5 posts
since Nov 2007
May 28th, 2009
0

Re: Simple sin() problem

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
Reputation Points: 13
Solved Threads: 18
Junior Poster in Training
Topi Ojala is offline Offline
60 posts
since May 2009
May 28th, 2009
0

Re: Simple sin() problem

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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
May 29th, 2009
0

Re: Simple sin() problem

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
c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
May 29th, 2009
0

Re: Simple sin() problem

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).
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
May 29th, 2009
0

Re: Simple sin() problem

And you should really include <cmath> instead.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 29th, 2009
0

Re: Simple sin() problem

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
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 29th, 2009
0

Re: Simple sin() problem

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Massena is offline Offline
5 posts
since Nov 2007
May 29th, 2009
0

Re: Simple sin() problem

>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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Very Basic Encryption
Next Thread in C++ Forum Timeline: Please Hlpe me, lojec erooe





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC