tags:

views:

488

answers:

6
+2  Q: 

C++ Cosine Problem

Hello,

I have the following code using C++:

double value = .3;
double result = cos(value);

When I look at the values in the locals window for "value" it shows 0.2999999999

Then, when I get the value of "result" I get: 0.95533648912560598

However, when I run cos(.3) on the computers calculator I get: .9999862922474

So clearly there is something that I am doing wrong.

Any thoughts on what might be causing the difference in results?

I am running Win XP on an Intel processor.

Thanks

+3  A: 

When I look at the values in the locals window for "value" it shows 0.2999999999

Long story short, your calculator uses decimal arithmetic, while your C++ code uses binary arithmetic (double is a binary floating-point number). Decimal number 0.3 cannot be represented exactly as a binary floating-point number. Read What Every Computer Scientist Should Know About Floating-Point Arithmetic, that will explain all implications in more detail.

Pavel Minaev
Thank you for this information!
Jason Heine
+18  A: 

The difference in results is because:

Your computer's calculator is returning the cosine of an angle specified in degrees. The C++ cos() function is returning cosine of an angle specified in radians.

The .2999999999 is due to the way floating point numbers are handled in computers. .3 cannot be represented exactly in a double. For details, I recommend reading What Every Computer Scientist Should Know about Floating Point Arithmetic.

Reed Copsey
AHH HA!! I feel like a dork now. Thank you so much!! This is my "dur" moment for today.
Jason Heine
Why the downvotes?
Reed Copsey
+10  A: 

cos(.3 radians) = 0.95533...

cos(.3 degrees) = 0.99998...

Bill
+2  A: 

Your calculator is using degrees. For example:

>>> import math
>>> math.cos (.3)
0.95533648912560598
>>> math.cos (.3 * math.pi / 180)  # convert to degrees
0.99998629224742674
eduffy
+4  A: 

cos(0.3) = 0.99998629224742679269138848004408 using degrees

cos(0.3) = 0,95533648912560601964231022756805 using radians

suszterpatt
+2  A: 

C++ does not exactly represent floating point numbers due to the insane amount of storage that would be required to get the infinite precision necessary. For a demonstration of this, try the following:

double ninth = 1.0/9.0;
double result = 9.0 * ninth;

This should yield a value in result of .99999999999

So, in essence, you need to compare floating point values within a small epsilon (I tend to use 1e-7). You can do a strict bit-by-bit comparison, but this consists of converting the memory used by the floating point to an array of characters of length sizeof(float), then comparing the characters.

Another thing to check would be whether or not you are using degrees. The computer's calculator uses degrees for its cosine calculation (notice how the result from the calculator is .99999..., which is very close to 1. The cosine of zero is 1 exactly), whereas the cosine function offered in <math> is in radians. Try multiplying your value by PI/180.0 and seeing if the result is more inline with your expectations.

MBillock