• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.Math providing inaccurate information, I think  RSS feed

 
Robert Haynes
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm attempting to do a simple SIN / COS function however the results are inaccurate to that of a manual calculator and some not even close to what they should be.




Results:
SIN= 0.8939966636005579 COS= -0.4480736161291702 ^ 90
SIN= 0.10598751175115685 COS= -0.9943674609282015 ^ 91
SIN= -0.7794660696158047 COS= -0.626444447910339 ^ 92
SIN= -0.9482821412699473 COS= 0.31742870151970165 ^ 93
SIN= -0.24525198546765434 COS= 0.9694593666699876 ^ 94
SIN= 0.683261714736121 COS= 0.7301735609948197 ^ 95
SIN= 0.9835877454343449 COS= -0.18043044929108396 ^ 96
SIN= 0.3796077390275217 COS= -0.9251475365964139 ^ 97
SIN= -0.5733818719904229 COS= -0.8192882452914593 ^ 98
SIN= -0.9992068341863537 COS= 0.0398208803931389 ^ 99
...
SIN= 0.08836868610400143 COS= -0.9960878351411849 ^ 135
...
SIN= 0.07075223608034517 COS= 0.9974939203271522 ^ 176
SIN= 0.8775897877771157 COS= 0.47941231147032193 ^ 177
SIN= 0.8775753358042688 COS= -0.4794387656291727 ^ 178
SIN= 0.07072216723899125 COS= -0.9974960526543551 ^ 179
SIN= -0.8011526357338304 COS= -0.5984600690578581 ^ 180

The most obvious error is the results at 90, 135, and 180. Sin of 90 should be 1 and Cos 0. At 135, the absolute value should be equal with 70.710678. 180 degrees should be Sin 0 and Cos -1. I have double checked the results manually with calculator and online with result tables.

While the "i" is an integer in this case, the results using a double value are the same. Does anyone have thoughts on how to get accurate results?


Thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Robert!

If you read the docs closely, it should be obvious where you're making an incorrect assumption:

http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#cos(double)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I take the sin of 90 on my windows calculator, and subtract the value you give, the result is:

-9.4817305015957901199730630305321e-18

which is really just about 0, so I'd say they match precisely.

You need to look at the docs and see what exactly the 90 represents...it's not what you think it is.
 
Robert Haynes
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a windows calculator could merely be replicating the same issue / assumption as posted originally. The variance between your calculator and the results posted is not as accurate as just looking at it. A value of 1 vs. 0.89 is a significant difference even for statistical variances.

Imagine using the result to building something 100 feet long based on the calculation. 100 feet vs. 89 feet is a considerable difference.

With that, the assumption which was not an assumption...just unawareness is that the expected input as a double is degrees "in radians" rather then standard calculator degrees
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert Haynes wrote:
With that, the assumption which was not an assumption...just unawareness is that the expected input as a double is degrees "in radians" rather then standard calculator degrees


No, it absolutely 100% was an assumption. You assumed the parameter was in degrees. It's not an unreasonable assumption, but it definitely was an assumption. An important part of solving problems is to be aware of what our assumptions are, so that we can check their validity when things don't go as expected.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too, find it weird that the default for most $5 calculators are in degrees, which the default for most math libraries are in radians.

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert Haynes wrote:Using a windows calculator could merely be replicating the same issue / assumption as posted originally. The variance between your calculator and the results posted is not as accurate as just looking at it. A value of 1 vs. 0.89 is a significant difference even for statistical variances.

Imagine using the result to building something 100 feet long based on the calculation. 100 feet vs. 89 feet is a considerable difference.


Except what he's talking about is more like a difference betwee


if I counted my zeros right.

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:I too, find it weird that the default for most $5 calculators are in degrees, which the default for most math libraries are in radians.

Different audiences, I'd say. Once you get deeper into the maths, using radians makes far more sense than using degrees (just look at the Taylor expansion for cos and sin, for example). But your average person is far more familiar with degrees.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!