mercredi 8 avril 2015

How to get exact value of Trigonometric functions in Java

This is not duplicate of this, and this I am developing a Calculator application for Android and I have been searching web for past 20-30 days but did not find any reasonable answer. I have also studied many papers on Floating Point Computation. I have also tried both Math and StrictMath library.

The following values I have tried

Math.cos(Math.PI/4) result in 0.7071067811865476 which is correct answer

Math.cos(Math.PI/2) result in 6.123233995736766E-17 correct answer is 0

Math.cos(Math.PI) result in -1.0 which is correct

Math.cos((3*Math.PI)/2) result in -1.8369701987210297E-16 correct answer is 0

Math.cos(Math.PI*2) result in 1.0 which is correct

Math.sin(Math.PI/4) result in 0.7071067811865476 which is correct answer

Math.sin(Math.PI/2) result in 1 which is correct

Math.sin(Math.PI) result in 1.2246467991473532E-16 correct answer is 0

Math.sin((3*Math.PI)/2) result in -1 which is correct

Math.sin(Math.PI*2) result in -2.4492935982947064E-16 correct answer is 0

Math.tan(Math.PI/4) result in 0.999999999999999 correct answer is 1

Math.tan(Math.PI/2) result in 1.633123935319537E16 correct answer is NaN

Math.tan(Math.PI) result in -1.2246467991473532E-16 correct answer is 0

Math.tan((3*Math.PI)/2) result in 5.443746451065123E15 correct answer is NaN

Math.tan(Math.PI*2) result in -2.4492935982947064E-16 correct answer is 0

When I tried all these calculations on Google's Official calculator which is included in Stock Lollipop yielded all correct answers except for tan((3*PI)/2) and tan(PI/2)

When I tried all these calculation on my Casio fx-991 PLUS all answers were correct.

Now my question is "How Google's calculator and Casio's calculator managed to get correct answer using limited floating precision of CPU?" and "How can I achieve same output?"