Results 1 to 10 of 10
Thread: sine and cosine
- 11-12-2010, 03:43 PM #1
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
-
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.
- 11-12-2010, 04:44 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Read Goldberg's article: What every computer scientist should know about floating point arithmetic.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-12-2010, 10:50 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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"?
- 11-13-2010, 12:42 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
would dividing a value into Math.PI help here?
- 11-13-2010, 02:01 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
That's what toRadians() does: conceptually, it divides its argument into 180*Math.PI.
- 11-13-2010, 02:18 AM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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
- 11-13-2010, 02:23 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 11-13-2010, 02:28 AM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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?
- 11-13-2010, 05:45 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
-
Angle to Radians and Calculate the Cosine
By coldplay7588 in forum New To JavaReplies: 7Last Post: 09-26-2010, 08:12 PM -
is Cosine Similarity the Default Similarity in Lucene?
By sethu.iit@gmail.com in forum LuceneReplies: 0Last Post: 06-30-2010, 09:49 AM -
Drawing the sine curve
By bumblyb33 in forum Java 2DReplies: 7Last Post: 03-26-2009, 10:29 PM -
Demonstration of drawing points. It draws a sine wave
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks