Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

C# Math.Acos How prevent NaN

Writer Andrew Henderson

I'm trying to make a code to answers a Math Function that includes Acos "Cos-1(A)", where A is a double -10.0000 <= A <= 10.0000, I had try several numbers in that range, and most of those give me a NaN as return that make all the operation Fails.

Edited: As the documentation said, with Math.Acos is I put a number out of the range -1<=d<=1 will return a NaN, I were attempting to make a function that make the same, step by step to allow those values.

1

10 Answers

As MSDN says, the argument to the Math.Acos function must be in range [-1,1]. Otherwise, the result will be NaN.

0

Your expectation that it should return something for those values is incorrect.

The arccosine function answers the question "what angle has this cosine?". But there is no value that you can give to the cosine function to yield a result less than -1 or greater than 1. We say that its "range is [-1, 1]".

Since there's no angle with a cosine value outside this range, it is not possible to give you an angle back when you ask Acos for one. So instead it gives you the answer of NaN.

The documentation for Math.Acos says this too:

Parameters:

d: (System.Double) a number representing a cosine, where d must be greater than or equal to -1, but less than or equal to 1.

Cos-1 isn't defined for values outside of [-1 .. 1]

It has nothing to do with the Math.Acos function - it comes down to trigonometry. The result of the cos function can never be more than 1 or less than -1. Therefore the inverse of the cos function can not take an argument that is more than 1 or less than -1.

Taking a quick look at the documentation will show that the values the parameter takes should be:

A number representing a cosine, where d must be greater than or equal to -1, but less than or equal to 1.

Obviously, Math.Acos return NaN for A<-1 or A>1. Just rule out thes values before calling the function.

acos allows only values in range [-1; 1], so -10 naturally gives you NaN.

The range of acos is -1 to 1 because of restrictions in the actual trigonometry math, not an issue with the function or code. Whenever using acos() you should make sure to range-check your values or have some other mechanism for making sure values going to it are always between -1 to 1. Maybe the values you're passing it need to be normalized first?

0

You can try this:

Whole.Ang[n] = (Math.Pow(Whole.Long[n], 2) + Math.Pow(Whole.Long[n - 1], 2) - Math.Pow(Line1, 2) / (2 * Whole.Long[n] * Whole.Long[n - 1]);
if (Whole.Ang[n] > Math.PI / 2)
{ Whole.Ang[n] = Math.Asin(Math.PI / 2 - Whole.Ang[n]); Whole.Ang[n] = Math.Round(Whole.Ang[n] * 180 / Math.PI, 0) * 2; Whole.Ang[n] = (90 - Whole.Ang[n] * 2) + Whole.Ang[n]; Console.WriteLine("第" + (n + 1) + "个角为:" + Whole.Ang[n]); continue;
}
else
{ Whole.Ang[n] = Math.Acos(Whole.Ang[n]); Whole.Ang[n] = Math.Round(Whole.Ang[n] * 180 / Math.PI, 0); Console.WriteLine("第" + (n + 1) + "个角为:" + Whole.Ang[n]); continue;
}
1

I ran into this problem. My bug was caused by the precision of floats (in javascript). It was trying to take Math.acos(1.000...). Just cap the range:

function acos(n) { if (n > 1) { return Math.acos(1) } else if (n < -1) { return Math.acos(-1) } else { return Math.acos(n)
}

The simple answer no one seems to be providing is this:

  1. multiply your coordinates by 0.1 to bring them within the proper range.
  2. perform trig function (arc-functions return an angle & angles don't care how long their legs are)
  3. after and if you convert back to coordinate pair (sin, cos) multiply each ordinate by 10 to return them to proper scale

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy