Math.sin: sin(pi/6) = 0.4999999999999999994
Author |
Message |
Ashi_Mashi2
|
Posted: Sun Dec 25, 2005 12:02 pm Post subject: Math.sin: sin(pi/6) = 0.4999999999999999994 |
|
|
Hey guys,
I have a problem and I found it in many Java calculator applications: why does sin(pi/6)=0.49999...9994 instead of just 0.5? I know it is because of rounding errors, but, does anyone have any solutions to this? (I tried increasing the accuracy of Math.PI, still doesnt work:S)
Thanks in advance |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Hikaru79
|
Posted: Sun Dec 25, 2005 2:38 pm Post subject: (No subject) |
|
|
I think this can be chalked up to the way that floating point decimals are represented by the computer. This problem is not unique to java. For example, here it is in Python:
code: | hikaru79@navi:~$ python
Python 2.4.2 (#2, Sep 30 2005, 21:19:01)
[GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import pi, sin
>>> sin(pi/6)
0.49999999999999994
>>>
|
So it's not just you I don't think there is much you can do about it, except deal with it manually. |
|
|
|
|
|
Andy
|
Posted: Sun Dec 25, 2005 11:44 pm Post subject: (No subject) |
|
|
yea hikaru is right, the reason is because pi is not exactly pi, but rather some decimal representation, thats why trignometric functions are off |
|
|
|
|
|
|
|