get sin of Math.PI

Page 1 of 1

3 Replies - 236 Views - Last Post: 07 August 2012 - 04:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288173&amp;s=a01d0e9fdaca9a97f68b179554a67028&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 StefanOnRails

Reputation: 35
• Posts: 105
• Joined: 31-July 12

get sin of Math.PI

Posted 07 August 2012 - 01:58 AM

Hi guys, I'm pretty new in Java and I have this problem: I was testing some Math functions when I tried to get sin of Math.PI using the following statement:
```System.out.println( Math.sin(Math.PI) ); // output: 1.2246467991473532E-16

```

Don't get me wrong, I wasn't the best in my math class, but shouldn't this output 0 ?
I've tried to use this:
```double pi = Math.toRadians(180);
System.out.println( Math.sin(pi) );

```
...but I got the same result

I've also tried to get cos of Math.PI and it worked fine. So what's the explanation?

Is This A Good Question/Topic? 0

Replies To: get sin of Math.PI

#2 g00se

• D.I.C Lover

Reputation: 2118
• Posts: 8,821
• Joined: 20-September 08

Re: get sin of Math.PI

Posted 07 August 2012 - 03:12 AM

Floating point values can only be approximated on computers (unless special classes like BigDecimal are used) so will rarely be exact. See http://mindprod.com/...atingpoint.html

#3 StefanOnRails

Reputation: 35
• Posts: 105
• Joined: 31-July 12

Re: get sin of Math.PI

Posted 07 August 2012 - 04:05 AM

Thanks for your reply, I have a better understanding over floating point numbers now (by the way, I managed to get my sin(PI) == 0 using Math.floor )

#4 g00se

• D.I.C Lover

Reputation: 2118
• Posts: 8,821
• Joined: 20-September 08

Re: get sin of Math.PI

Posted 07 August 2012 - 04:36 AM

Good. Of course pi itself can never be represented exactly - even with BigDecimal