1

I am struggling with a simple calculation problem.

By theory for an arbitrary angle:

If

is

enter image description here

If I implement this in python or Matlab:

import numpy as np

alpha = -89.999961

alpha_rad =  np.deg2rad(alpha)
result = np.arccos(np.sin(alpha_rad)**2 + np.cos(alpha_rad)**2)

print('%.16f' % result)

leads to

0.0000000149011612

whereas

alpha = -89.9999601

leads to

0.0000000000000000

It is also practically 0 using -89.999962°, but it is again 1.49011612e-08 for alpha = -89.9999°

Does anybody know the reason for this and which angles will lead to results bigger than 0. I am not an big expert in numerical mathematics, but the spacing of floating numbers is much smaller (2.220446049250313e-16). I want to multiply the result with a large number so it would be great, if the result is 0 in terms of floating numbers spacing.

Any help and explanation is very welcome!

No Body
  • 154
  • 1
  • 1
  • 6
  • 2
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Mike Doe Oct 07 '19 at 13:40
  • 2
    Actually inaccuracy is a relatively minor problem. More alarming is that the argument to arccos could be greater than 1, and (in C at least) arccos will then return nan. In a wee test on 10000 angles equally spaced round the circle, there were 322 angles that yielded nan, As No Body says, it's best to avoid arrcos. – dmuir Oct 08 '19 at 16:14

2 Answers2

1

It's the same in Java too and in other programming languages. The fractional part of a floating point number is finite so there are computational errors in resolving certain values. So in some cases you will need to round to get the expected result.

Here is a Java example of the same issue.

      double[] angles = { 23.4, 22, 78.3, 92.4
      };

      for (double a : angles) {
         double val = Math.sin(a) * Math.sin(a) + Math.cos(a) * Math.cos(a);
         System.out.println(Math.acos(val) + " " + a);
         System.out.println("-------------------------------");
      }
   }

If you search for subjects like dealing with floating point errors you may get some insight into this as well as how to handle it.

WJS
  • 36,363
  • 4
  • 24
  • 39
  • Thank you for your quick response. What would be a suitable criterion to decide when I need to round? I tested all the angles you wrote leading to numerical 0. – No Body Oct 07 '19 at 13:40
  • Deciding when to round requires a knowledge of your data and what's important. I would think that rounding .0000001 to 0 would be acceptable in many cases. But if you're trying, for example, to do parallax calculations in astronomy, that small value could be significant. – WJS Oct 07 '19 at 13:51
  • That's the point, small values are also significant so it might be necessary to use higher precision which makes my problem quite computational expensive or I will live with a lower precision. Thank you! – No Body Oct 07 '19 at 14:06
1

solves the problem for arbitrary angles. Cosines is known for numerical problems (e.g. https://www.nayuki.io/page/numerically-stable-law-of-cosines)

No Body
  • 154
  • 1
  • 1
  • 6