Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Math.Acos How prevent NaN

Tags:

c#

math

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.

like image 802
OsbSoto Avatar asked Jan 22 '12 14:01

OsbSoto


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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

like image 111
Smi Avatar answered Oct 10 '22 01:10

Smi


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.

like image 39
John Feminella Avatar answered Oct 10 '22 01:10

John Feminella