Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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 399 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: 438
Projects: 4


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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
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!
Play the free demo of Revenge of the Titans!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
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: 438
Projects: 4


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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
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
Offline 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
Offline Agro
« Reply #10 - Posted 2013-03-22 00:41:21 »

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

Offline vbrain

Junior Member


Medals: 3



« 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: 7
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.
Online Roquen

JGO Ninja


Medals: 66



« 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  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (76 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (119 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (125 views)
2013-05-15 15:01:13

theagentd (112 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (178 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.117 seconds with 21 queries.