Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Unexpected answer returned by Math.sin  (Read 786 times)
0 Members and 1 Guest are viewing this topic.
Offline Porlus

Junior Member





« Posted 2013-03-21 23: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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2013-03-22 00: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
Offline Porlus

Junior Member





« Reply #2 - Posted 2013-03-22 00:18:03 »

Thanks for the reply! Ah ok, I see. Is there any way around this problem?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


Hand over your head.


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

Is it really a problem?

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

Junior Member





« Reply #4 - Posted 2013-03-22 00:25:38 »

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

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2013-03-22 00: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
Offline Porlus

Junior Member





« Reply #6 - Posted 2013-03-22 00:31:10 »

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

Type cast it to an integer.

Offline Porlus

Junior Member





« Reply #8 - Posted 2013-03-22 00:35:47 »

That's great. Thanks for the help. Smiley
Offline Sparky83

Senior Member


Medals: 6
Projects: 1



« Reply #9 - Posted 2013-03-22 00: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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Agro
« Reply #10 - Posted 2013-03-22 00:41:21 »

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

Offline vbrain
« Reply #11 - Posted 2013-03-22 00: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.
Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« Reply #12 - Posted 2013-03-22 08: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.
Offline Roquen
« Reply #13 - Posted 2013-03-22 10:52:02 »

KEEP IT SIMPLE

System.out.printf("%f\n", Math.sin(Math.toRadians(180.0)));

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.

pw (11 views)
2014-07-24 01:59:36

Riven (9 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (49 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!