0

I can not find the correct value of sin 30 ,

 double degrees = 30.0;
 double radians = Math.toRadians(degrees);
 System.out.println(Math.sin(radians));

which produces .499999999 but the exact value matching to calculator is .5

please help me..

Vishal
  • 140
  • 1
  • 4
  • 13
  • 3
    I don't believe that you got that result with `Math.ceil`... – Oliver Charlesworth Feb 17 '13 at 12:07
  • 3
    Floating number arithmetic is bound to suffer from inaccuracy. And `Math.ceil` should give you a double value *rounded up* to closest integer. – phoeagon Feb 17 '13 at 12:07
  • 1
    It seems like you should get `1.0` as output there, and not `0.5`. – Rohit Jain Feb 17 '13 at 12:08
  • 1
    @phoeagon. No, `Math.ceil` gives a double value not an integer. – Rohit Jain Feb 17 '13 at 12:10
  • @RohitJain [sin(30)](http://www.google.com.au/search?q=sin+of+30+degrees&aq=f&oq=sin+of+30+degrees&sourceid=chrome-mobile&ie=UTF-8) is 0.5 – Bohemian Feb 17 '13 at 12:20
  • @Bohemian. Oh boy. Why did you directed that comment at me? By double value I meant `1.0`, which is not the same as integer `1` right? – Rohit Jain Feb 17 '13 at 12:23
  • I think rounding is helpful for you, maybe you could refer to [How to round a number to n decimal places in Java][1]. [1]: http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – goodseeyou Feb 17 '13 at 12:26
  • @RohitJain Looks like the question was edited significantly after your comment. nm. – Bohemian Feb 17 '13 at 19:18

1 Answers1

1

Math.sin(..) make an interpolation to get a good value, therefore you cannot expect to get exact values as result. In addition while interpolating the sin value the problems of float/double arithmetic is an other issue.

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
  • The calculator does the same, but why output there is 0,5? is the question – AlexWien Feb 17 '13 at 12:14
  • @AlexWien: As far as I know the arithmetic in a calculator for the approximation has more digits than the display and is rounded before displayed. Also a different algorithm for the interpolation may be the reason- – MrSmith42 Feb 17 '13 at 12:18
  • your first part (more digits) is ok, other algo is for sure not the reason – AlexWien Feb 17 '13 at 12:26