Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math.sin returns unexpected results

Tags:

java

Math.sin(Math.PI) returns 1.2246467991473532E-16

Math.sin(-Math.PI) returns -1.2246467991473532E-16

But mustn't it return zeros?

Though sin from 3.14 and 3.15 are correct.

like image 579
Alexiy Avatar asked Apr 10 '26 13:04

Alexiy


1 Answers

Math.PI is not "exactly" π, it's just a roughly-16-digit approximation of π (3.141592653589793). No floating point format can store π exactly. That's why you don't get the exact zero.

like image 65
kennytm Avatar answered Apr 13 '26 02:04

kennytm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!