Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) 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 1299 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: 954
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: 954
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: 954
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

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.

 BurntPizza (29 views) 2015-04-23 03:42:11 theagentd (35 views) 2015-04-22 16:23:07 Riven (48 views) 2015-04-16 10:48:47 Duke0200 (59 views) 2015-04-16 01:59:01 Fairy Tailz (42 views) 2015-04-14 20:13:12 Riven (44 views) 2015-04-12 21:36:37 bus hotdog (60 views) 2015-04-10 02:39:32 CopyableCougar4 (66 views) 2015-04-10 00:51:04 BurntPizza (70 views) 2015-04-06 22:06:58 ags1 (68 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 17x wessles 15x kingroka123 11x alwex 11x 65K 11x kevglass 8x Rayvolution 7x Roquen 7x Riven 7x chrislo27 7x Hanksha 7x Olo 7x Ecumene 7x ra4king 7x KevinWorkman 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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