11
votes

I'm trying something that I thought should be reasonably simple. I have an angle, a position and a distance and I want to find the X,Y co-ordinates from this information.

With an example input of 90 degrees I convert the value to radians with the following code:

public double DegreeToRadian(float angle)
{
  return Math.PI * angle / 180.0;
}

This gives me 1.5707963267949 radians Then when I use

Math.Cos(radians)

I end up with an an answer of: 6.12303176911189E-17

What the heck is going on? The cosine of 90 degrees should be 0, so why am I getting such a deviance... and more importantly how can I stop it?

7
That definitely rounds to 0. Use a format specifier when you convert the value to a string so the user sees it the way they expect.Cody Gray
Ok, I take the point that the lack of precision is due to the lack of precision in the floating point types, but how does something like Windows Calculator manage to get the answer dead on, does it just cheat and use a lookup table?elaverick
@elaverick - who says the Windows Calculator is using .Net doubles (it isn't), or any floating point type (it isn't). And who says that it's outputting the precise results of a calculation rather than applying sane rounding rules?Damien_The_Unbeliever
Calculator was completely rewritten a few years ago to use arbitrary-precision arithmetic in response to precisely such bugs.Cody Gray
Your value of pi is off by a few parts in 10 to the 17, so it shouldn't be a surprise that the result is also off by a few parts in 10 to the 17. Pi is rounded off to 16 digits or so.Eric Lippert

7 Answers

14
votes

Let me answer your question with another one: How far do you think 6.12303176911189E-17 is from 0? What you call deviance is actually due to the way floating point numbers are internally stored. I would recommend you reading the following article. In .NET they are stored using the IEEE 754 standard.

7
votes

See answers above. Remember that 6.12303176911189E-17 is 0.00000000000000006 (I may have even missed a zero there!) so it is a very, very small deviation.

6
votes

you should use rounding

var radians = Math.PI * degres / 180.0;
var cos = Math.Round(Math.Cos(radians), 2);
var sin = Math.Round(Math.Sin(radians), 2);

the result would be: sin: 1 cos: 0

3
votes

Read up on floating point arithmetic. It is never and can never be exact. Never compare exactly to anything, but check whether the numbers differ by a (small) epsilon.

2
votes

The other posts are correct about the practical matter of working with floating point implementations which return results with small errors. However, it would be nice if floating point library implementations would preserve the basic identity of well-known functions:

Math.Sin(Math.PI) should equal 0,
Math.Cos(Math.PI) should equal -1,
Math.Sin(Math.PI/2) should equal 1,
Math.Cos(Math.PI/2) should equal 0, etc.

You would expect that a floating point library would respect these and other trigonometric identities, whatever the minor errors in its constant values (e.g. Math.PI).

The fact that you're getting a small error from Math.Cos(Math.PI/2) indicates that the implementation is calculating the result, rather than pulling it from a table. A better implementation of Math.Cos and the other transcendental functions could be more accurate for specific identities.

I'm sure in the case of C#, this behavior is expected, and so Microsoft couldn't change it without affecting existing code. If getting the precise result for specific trigonometric identities matters to you, you might wrap the native floating point functions with some code that checks for well-known inputs.

1
votes

Since the result of the calculation is really close to 0 (zero), you could just use rounding:

Math.Round(result, 4): // 4 decimals, e.g.: 2.1234

So, calculation of sin/cos from radian:

const double Deg = Math.PI / 180;
double sin = Math.Round(Math.Sin(yourRadianValue * Deg), 4);
double cos = Math.Round(Math.Cos(yourRadianValue * Deg), 4); // 0.0000...06 becomes 0

Which if yourRadianValue = 90, returns sin = 1 and cos = 0.

1
votes

As noticed by @b1tw153, it'd be great if exact values were returned for multiples of PI/2. And that's exactly what Microsoft did in their System.Numerics library; if you examine the source code for Matrix3x2.CreateRotation, you'll note they handle n * PI/2 cases manually: https://github.com/Microsoft/referencesource/blob/master/System.Numerics/System/Numerics/Matrix3x2.cs#L325