5

I am using math.acos() function :

math.acos(1.0000000000000002)

This throws a math domain error. Can someone tell the reason? I am getting this value calculated before and here this value gives error but if I remove 2 at the end it does not throw error. I did not get the reason to this.

shalki
  • 800
  • 3
  • 12
  • 31
  • 5
    The cosine of an angle is always in the range `[-1.0, 1.0]`, so the inverse function is only defined for inputs in that range. You're giving `acos` a value larger than `1`: there's no possible (real) angle whose cosine is greater than `1`. – Mark Dickinson Jun 20 '15 at 14:29
  • What is your expected result, mathmatically? – Yu Hao Jun 20 '15 at 14:31
  • 5
    This specific problem comes up often in numerical programming. Voting to reopen. – David Hammen Jun 20 '15 at 14:55
  • 2
    I don't get why this question is closed. My colleague had this exact question today, and it's stated more or less how he asked it. – Phlucious Jul 26 '17 at 23:35
  • The cosine of an angle in a right triangle is the relation between the length of the closest leg and the hypotenuse. Since the hypotenuse is always greater than any of the legs, the cosine of an angle is always less than one. Arc-cos, or "acos", is the inverse function. You're telling it you have a triangle where the hypotenuse is shorter than the closest leg, and now asking python what angle that would give. Python throws a fit, since what you are asking is impossible! :-) – avl_sweden Nov 30 '19 at 15:18
  • 1
    I encountered this issue when calculating `math.acos(numpy.dot(a,b))` where `numpy.dot` returned `1.0000000000000002` instead of 1 (due to floating point precision). See [this post](https://stackoverflow.com/questions/20457038/how-to-round-to-2-decimals-with-python) for solutions to this. – DaBooba May 21 '20 at 21:21

2 Answers2

12

You are trying to do acos of a number for which the acos does not exist.

Acos - Arccosine , which is the inverse of cosine function.

The value of input for acos range from -1 <= x <= 1 .

Hence , when trying to do - math.acos(1.0000000000000002) you are getting the error.

If you try higher numbers, you will keep getting the same error - math.acos(2) - leads to - ValueError: math domain error

Anand S Kumar
  • 82,977
  • 18
  • 174
  • 164
2

Inverse cosine is defined only between -1 and 1, inclusive. The arc-cosine of 1.0000000000000002 has no mathematical or semantic meaning other than "does not exist" or "undefined".

Of course, since inverse cosine of 1 does exist, acos(1) doesn't throw any error.

nanofarad
  • 38,481
  • 4
  • 83
  • 110