Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) 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 4172 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?

Thanks
Riven

« JGO Overlord »

Medals: 1341
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

« JGO Overlord »

Medals: 1341
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

« JGO Overlord »

Medals: 1341
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

JGO Kernel

Medals: 517

 « Reply #13 - Posted 2013-03-22 09:52:02 »

KEEP IT SIMPLE

outputs: 0.000000

If you only want to see the high significant digits, use printf with %f instead of println.
Pages: [1]
 ignore  |  Print

 EgonOlsen (77 views) 2018-06-10 19:43:48 EgonOlsen (57 views) 2018-06-10 19:43:44 EgonOlsen (77 views) 2018-06-10 19:43:20 DesertCoockie (259 views) 2018-05-13 18:23:11 nelsongames (157 views) 2018-04-24 18:15:36 nelsongames (156 views) 2018-04-24 18:14:32 ivj94 (897 views) 2018-03-24 14:47:39 ivj94 (161 views) 2018-03-24 14:46:31 ivj94 (810 views) 2018-03-24 14:43:53 Solater (174 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org