Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Math.sin: sin(pi/6) = 0.4999999999999999994
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Ashi_Mashi2




PostPosted: 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
Sponsor
sponsor
Hikaru79




PostPosted: 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 Smile I don't think there is much you can do about it, except deal with it manually.
Andy




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: