C++ sqrt returns -1.#IND000000000000
Asked Answered
S

6

6

In specific:

Im doing some math operations, and the application keeps crashing because a double that is widely used happens to get the value: -1.#IND000000000000 when "some" numbers are sqrt'ed... What is this? Indefinite? Infinite? Too big to fit? Not a perfect Square Root? Is there any way to solve this? Thanks in advance! EDIT: How can i check if a double has this value ? I tried: if (x == 0x-1.#IND000000000000) and other variations but did not work. Is it possible to check to see if a variable has this value ?

Sinistrocular answered 14/1, 2011 at 6:12 Comment(4)
are some numbers less than 0 perhaps? Tell me sir, what is the sqrt of -1Dioptric
Clearly EnabrenTane meant, "What is the IEEE 754 sqrt of -1.0?"Begrudge
@Noah Roberts: i is hard to represent as a floating point number. Maybe it could return a char :-)Abampere
I'm willing to bet the cause isn't sqrt(-1) but sqrt(-1E-20) or so. This happens when two approxiamtely equal terms almost cancel, e.g. sqrt(100.0/3.0 - (50.0/3.0 + 50.0/3.0)). Due to limited FP precision, this argument isn't exactly 0.0 and may be rounded to a negative number.Gateshead
G
10

Actually, the string -1.#IND000000000000 is not a value, returned by a function, but it is one of a common representations of a QNaN, Quiet Not-a-Number, the special IEEE-754 value representing the invalid number, that won't cause the exception (there are also SNaN, Signalling NaN, which would cause the floating point exception, if enabled). The common cause of this is calling a function with argument out of it's domain.

Gujral answered 14/1, 2011 at 6:31 Comment(1)
Yes, many certain bit layout's represent the same "value of NaN".Gujral
B
5

NaNs (such as the "indeterminate" you have and "infinity") can be detected by checking x == x (it's false for NaN and true for any finite number).

Begrudge answered 16/1, 2011 at 1:14 Comment(0)
A
2

The value #IND000000000000 represents an invalid numeric value. This is often called a QNaN (Quiet Not-A-Number) value because it represents an indeterminate type but does not cause calculations to fail.

Chances are, the elusive numbers whose square root you're attempting to determine are negative numbers, for which that value is not defined. See Wikipedia for more information.

Solving it will first require you to determine where the problem is occurring. That means you either need to step through yourself it with a debugger and watch the values of the variables, or post your code so that we can do the same.

Amarette answered 14/1, 2011 at 6:30 Comment(0)
Q
1

If the argument is negative, a domain error occurs, setting the global variable errno to the value EDOM.

Quechuan answered 14/1, 2011 at 6:25 Comment(0)
T
0

Without using a complex type, sqrt of any value below 0 is meant to cause an exception.

Teddy answered 14/1, 2011 at 6:17 Comment(0)
S
0

It's a NaN (Not A Number). It means you've called sqrt with a negative argument.

Cheers & hth.,

Shechem answered 14/1, 2011 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.