3 Replies - 237 Views - Last Post: 07 August 2012 - 04:36 AM Rate Topic: -----

#1 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 8,823
  • 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
Was This Post Helpful? 1
  • +
  • -

#3 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • 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 :lol: )
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 8,823
  • 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 ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1