-1

I have a method to convert degrees to radians...

public static double ToRadians(double degAngle) 
{
    return Math.PI * degAngle / 180;
}

...and it works pretty well. But when I try Math.Cos(ToRadians(90));, it returns 6.12303176911189E-17. Why does this happen?

  • 1
    Did you noticed the `E-17`? That's because of the rounding. The floatingpoint doesn't not have an endless precision. – Jeroen van Langen Feb 06 '20 at 18:35
  • 2
    What is the expected value? How far off is it? Is it within expected float inprecision? https://www.youtube.com/watch?v=PZRI1IfStY0 – Christopher Feb 06 '20 at 18:36
  • The expected value of `cos(90 degrees)` is `0` (cosine is the X component of the unit-circle, which at ninety degrees is zero). The answer you're getting is due to floating point precision errors. 6E-17 is very close to zero. –  Feb 06 '20 at 18:36
  • See https://stackoverflow.com/questions/588004/is-floating-point-math-broken –  Feb 06 '20 at 18:38
  • 1
    It is the expected result based on the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.math.cos?view=netframework-4.8) same thing happens in [other language](https://stackoverflow.com/questions/13260296/python-cos90-and-cos270-not-0). I would assume that you can never pass degree 90 to that method since it would need to much precision in radian as parameter. – the_lotus Feb 06 '20 at 18:40
  • Indeed it's right there in the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.math.cos?view=netframework-4.8#examples): `Math.Cos(90 deg) == 6.1230317691118863E-017`. Perhaps [How to check if Math.cos(angle) = 0](https://stackoverflow.com/q/28700168/3744182) or [Accuracy of Math.Sin() and Math.Cos() in C#](https://stackoverflow.com/q/3249710/3744182) answer your question. – dbc Feb 06 '20 at 18:42
  • I got `1.57079632679` but not using that... – Trevor Feb 06 '20 at 18:57
  • @AllisonE I use `var r = Math.Acos(ToRadians(-0.0));` `-0.0` represents 90 degrees which outputs `1.5707963267948966` half... – Trevor Feb 06 '20 at 19:06

2 Answers2

1

It's not because you aren't passing pi/2 as a parameter but something close to it.

Math.PI * 90 / 180 will not return the exact radian for degree 90, only something very close since Math.PI doesn't have infinite precision. To get an exact value of 0 returned by Math.Cos, you would need the pass the exact value in radian. You can only get a value very close to zero. The documentation show this is the proper value.

the_lotus
  • 12,668
  • 3
  • 36
  • 53
  • I am still getting the same answer. Or did I use the wrong value? Is the exact value of 90 degrees not `1.5707963267948966192313216916916398... ` in radian? – Emmanuel Allison Feb 06 '20 at 19:04
  • @AllisonE you can't get an exact value in radian for 90 degree. Only something very close like in your comment. That's why the result is something very close to zero. – the_lotus Feb 06 '20 at 19:08
  • 1
    @AllisonE It would take infinite digits to perfectly represent 90 degrees in radians, because PI is itself transcendental. –  Feb 06 '20 at 19:08
1

90 degrees is approximately 1.5707963267948966... radians. I say "approximately" because PI is a transcendental number, but in double-precision floating point values, we are limited to 64-bits. That means the resulting calculation will be off by some margin of error. That same error occurs again when you take the cosine (a transcendental function). The resulting value cannot be perfectly represented in double-precision, so you receive an approximation. The correct answer is zero. The answer you have is 0.00000000000000061230317691118863, which is very close to zero.

If you were to do this math by hand, using 3.14 as the value of PI, you will get an answer that is close to, but not exactly, zero, for the same reason: you are using approximations. Using 3.1415926 will give you a closer answer, but that value of PI is still an approximation. The computer is using another approximation, one limited to 64-bits of precision, which comes out to roughly 16 digits of PI.

This issue will occur in any computing system that uses IEEE-754 floating point math. See Is floating point math broken? for more info.

Also take a look at this blog post, which goes into greater detail in how PI is represented in computers.