Search code examples
ctrigonometrymath.h

How do I normalise the input value for acosf or asinf?


I need to obtain an inverse cosine and inverse sine from the result of a series of other calculations. If my input value is outside the range -1 to 1 then acosf or asinf return nan, which is expected given the definition of the function.

How should I adjust the value so that it falls within the acceptable range, but still gives the correct answer? In the spreadsheet I am using to check my calculations this normalisation is happening behind the scenes so nan is not returned.

I think I have to do something like, for acosf, add cos(pi) until the value falls into the range, but I'd like some confirmation or advice please.


Solution

  • There is no normalization process. If the answer is outside [-1,+1], then the preceding calculations are broken.

    One exception is if floating-point inaccuracies have lead to a value such as 1.00001. In cases such as these, it is probably safe to clamp the input to the valid range.