Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (653) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Unexpected answer returned by Math.sin  (Read 1488 times) 0 Members and 1 Guest are viewing this topic.
Porlus

Junior Devvie

 « Posted 2013-03-21 22:59:57 »

Hi all, I'm just wondering why the following isn't producing the answer 0 as a sine wave normally be at when it reaches 180 degrees. Can anyone see why?

 1 `System.out.println(Math.sin(Math.toRadians(180.0)));`

Thanks
Riven
« League of Dukes »

« JGO Overlord »

Medals: 1019
Projects: 4
Exp: 16 years

 « Reply #1 - Posted 2013-03-21 23:08:52 »

Math.toRadians(180.0) returns PI. PI cannot be accurately described in any base (except products of pi).
 1 `   Math.sin(Math.toRadians(180.0)) == Math.sin(Math.PI) != 0.00;`

Because:
 1 `        Math.PI != pi`

We can verify that Math.PI is indeed the best approximation of PI:
 1  2  3 `         System.out.println(Math.sin(Math.PI - Math.ulp(Math.PI)));         System.out.println(Math.sin(Math.PI));         System.out.println(Math.sin(Math.PI + Math.ulp(Math.PI)));`

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Porlus

Junior Devvie

 « Reply #2 - Posted 2013-03-21 23:18:03 »

Thanks for the reply! Ah ok, I see. Is there any way around this problem?
Riven
« League of Dukes »

« JGO Overlord »

Medals: 1019
Projects: 4
Exp: 16 years

 « Reply #3 - Posted 2013-03-21 23:22:23 »

Is it really a problem?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Porlus

Junior Devvie

 « Reply #4 - Posted 2013-03-21 23:25:38 »

Well the code snippet above seems to return 1.2246467991473532E-16 rather than 0. Is that typically an acceptable margin of error?
Riven
« League of Dukes »

« JGO Overlord »

Medals: 1019
Projects: 4
Exp: 16 years

 « Reply #5 - Posted 2013-03-21 23:26:45 »

Note the 'e-16'

1.2246467991473532E-16
equals
0.00000000000000012246467991473532

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Porlus

Junior Devvie

 « Reply #6 - Posted 2013-03-21 23:31:10 »

Ah, that's fine then. Is there an easy way to convert that to that readable format in Java? Just for testing
Agro
 « Reply #7 - Posted 2013-03-21 23:31:35 »

Type cast it to an integer.

Porlus

Junior Devvie

 « Reply #8 - Posted 2013-03-21 23:35:47 »

That's great. Thanks for the help.
Sparky83

Senior Devvie

Medals: 6
Projects: 1

 « Reply #9 - Posted 2013-03-21 23:40:07 »

I wouldn't do typecasting to an integer, because the result will be 0 in most of the cases and will only be non zero if the angle is exactly pi/2 or 3*pi/2.

I would rather set a threshold. Like this:
 1  2  3 `double a = Math.sin(Math.toRadians(angle));if(a < 0.0001 && a > -0.00001)    a = 0;`

In which case would typecasting to integer be better?
Agro
 « Reply #10 - Posted 2013-03-21 23:41:21 »

Well, I would convert to degrees if you want to display it.

vbrain
 « Reply #11 - Posted 2013-03-21 23:41:49 »

I wouldn't do typecasting to an integer, because the result will be 0 in most of the cases and will only be non zero if the angle is exactly pi/2 or 3*pi/2.

I would rather set a threshold. Like this:
 1  2  3 `double a = Math.sin(Math.toRadians(angle));if(a < 0.0001 && a > -0.00001)    a = 0;`

In which case would typecasting to integer be better?

In NO case would typecasting to an integer be better.
Alan_W

JGO Knight

Medals: 8
Projects: 3

Java tames rock!

 « Reply #12 - Posted 2013-03-22 07:10:51 »

You could round off the result to however many decimal places you desire.

E.g.

 1 `double result = (int)(1.0e6*sin(angle))/1.0e6`

Time flies like a bird. Fruit flies like a banana.
Roquen
 « Reply #13 - Posted 2013-03-22 09:52:02 »

KEEP IT SIMPLE