Results 1 to 10 of 10
Like Tree3Likes
  • 1 Post By JosAH
  • 1 Post By pbrockway2
  • 1 Post By al_Marshy_1981

Thread: sine and cosine

  1. #1
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default sine and cosine

    I wanna create a program how to calculate the sine and cosine of a degree.

    Math.cos(Math.toRadians(degrees)) works to 269
    Math.sin(Math.toRadians(degrees)) works to 179

    cos of 269 = -0.017452406437283498
    cos of 270 = -1.8369701987210297E-16 (WRONG)

    sin of 179 = 0.01745240643728344
    sin of 180 = 1.2246467991473532E-16 (WRONG)

    Does someone know how I can create a working sin/cos function to infinite?

    Thanks,
    Dennis

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What you label as WRONG is actually correct within the limitations of use of floating point numbers. I think that you will want to Google and read up on and understand the limitations of working with floating point numbers.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,334
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Dennis View Post
    cos of 270 = -1.8369701987210297E-16 (WRONG)
    sin of 180 = 1.2246467991473532E-16 (WRONG)
    Read Goldberg's article: What every computer scientist should know about floating point arithmetic.

    kind regards,

    Jos
    jancy likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I think that you will want to Google and read up on and understand the limitations of working with floating point numbers

    I agree.

    It might seem trivial: but there are only a finite number of different values that can be assigned to a double. And there are an infinite number of real numbers even over the range [-1,1] of the sine function. (And all of them are the sine of something).

    The best a computer might do is produce one of the values a double might have that is "close" to the mathematical result. The exact promises of the Math class in this regard are documented in its API documentation.

    Note that it doesn't promise (in the case of sine) the very closest double (what it calls "correctly rounded"). And, anyway, the toRadians(180) produces a double value that is, at best, close to a well known real numeric value. toRadians() "Converts an angle measured in degrees to an approximately equivalent angle measured in radians. The conversion from degrees to radians is generally inexact." Not very awe inspiring! But, as always, the API docs are the defintion of correctness.

    (The business of ulps used in the API documentation is discussed and explained in the Goldberg paper linked to.)

    Another point: we humans notice the oddity with sin(180) and declare it "WRONG". But what about sin(179)? Your lack of comment on the displayed value might well be taken as endorsement, but have you really verified that it is, in some significant sense, "RIGHT"?
    jancy likes this.

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    would dividing a value into Math.PI help here?
    jancy likes this.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    That's what toRadians() does: conceptually, it divides its argument into 180*Math.PI.

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Java works in radians not degrees though right? or am I losing it again :D

    edit: leaving my original dumb post lol thanks for the info pbrockway2

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Yes Math.sin() wants a radians argument. But the OP is working in degrees, so he calls Math.toRadians(arg) first which does something like 180*Math.PI/arg and then calls Math.sin() with the result.

    The OP's problem, though, was not understanding why the output obtained was very very close but not exactly the same as the mathematical sin function.

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Okay thanks for the feedback pbrockway, my maths for graphics course refuses to allow us to use built in functions (so you can imagine implementing it) not too bad for converting degrees to radians though.

    Is the OP post an easy fix though?

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    my maths for graphics course refuses to allow us to use built in functions (so you can imagine implementing it)

    Ouch! It's the sort of thing I would try and not imagine ;)

    The OP's (code) fix is extremely easy: do nothing. The output he posted is correct. He just needs to accept it (*). Understanding why it's correct (the brain fix) is another matter and I guess he has hasn't responded because he's away somewhere reading the Goldberg paper.

    * I have seen posts involving freaky things happening with Java graphics whose cause turned out to be floating point gotchas. (can't remember the details offhand) The solution is to rearrange the maths to avoid the problem. AGain, I'd prefer to imagine someone else doing the numerical calculations...

Similar Threads

  1. Angle to Radians and Calculate the Cosine
    By coldplay7588 in forum New To Java
    Replies: 7
    Last Post: 09-26-2010, 08:12 PM
  2. is Cosine Similarity the Default Similarity in Lucene?
    By sethu.iit@gmail.com in forum Lucene
    Replies: 0
    Last Post: 06-30-2010, 09:49 AM
  3. Drawing the sine curve
    By bumblyb33 in forum Java 2D
    Replies: 7
    Last Post: 03-26-2009, 10:29 PM
  4. Replies: 0
    Last Post: 06-28-2008, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •