Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (491) Games in Android Showcase (112) games submitted by our members Games in WIP (556) 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 876 times) 0 Members and 1 Guest are viewing this topic.
Porlus

Junior Member

 « 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
« League of Dukes »

JGO Overlord

Medals: 783
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 Member

 « 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: 783
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 Member

 « 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: 783
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 Member

 « 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 Member

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

That's great. Thanks for the help.
Sparky83

Senior Member

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

outputs: 0.000000

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

You cannot reply to this message, because it is very, very old.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 Nickropheliac (15 views) 2014-08-31 22:59:12 TehJavaDev (23 views) 2014-08-28 18:26:30 CopyableCougar4 (32 views) 2014-08-22 19:31:30 atombrot (41 views) 2014-08-19 09:29:53 Tekkerue (40 views) 2014-08-16 06:45:27 Tekkerue (35 views) 2014-08-16 06:22:17 Tekkerue (25 views) 2014-08-16 06:20:21 Tekkerue (37 views) 2014-08-16 06:12:11 Rayexar (72 views) 2014-08-11 02:49:23 BurntPizza (49 views) 2014-08-09 21:09:32
 BurntPizza 39x kevglass 34x Rayvolution 30x Riven 26x princec 25x Longor1996 15x PandaMoniumHUN 12x theagentd 10x Roquen 10x basil_ 10x gouessej 9x ags1 9x SHC 8x HeroesGraveDev 7x trollwarrior1 7x nsigma 7x
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-07-08 01:59:08
 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