Sorashi
Sorashi

Reputation: 931

Sqrt method doesn't return number

I've got a method that computes two offsets for something else in my code. I wrote these offsets to the output and the result was: Isn't number. When I tryed to assign these values to another variables, these variables' value was 0.

This is my code:

int w = 1200;
int h = 1824;
double offsetX = System.Math.Sqrt (-((9 * w * w) / 16));
double offsetY = System.Math.Sqrt (((9 * h * h) / 16) + ((9 * w * w) / 16) - offsetX);

I don't know if it is a mathematical or a programming problem. Why does it return this weird value?

Upvotes: 0

Views: 2383

Answers (1)

Soner Gönül
Soner Gönül

Reputation: 98750

-((9 * w * w) / 16) expression returns a negative value which is -810000.

That's why this method returns NaN.

From the documentation of Math.Sqrt;

d parameter  | Return value
Negative     | NaN

From Wikipedia;

The square of any positive or negative number is positive, and the square of 0 is 0. Therefore, no negative number can have a real square root. However, it is possible to work with a more inclusive set of numbers, called the complex numbers, that does contain solutions to the square root of a negative number.

Upvotes: 2

Related Questions