What to do 1. # INF00, -1. # IND00 and -1. # IND mean?

I messed around with some C code using a float and I get 1. # INF00, -1. # IND00 and -1. # IND when I try to print floats on the screen. What do these meanings mean?

I believe 1. # INF00 means positive infinity, but what about -1. # IND00 and -1. # IND? I also sometimes saw this value: 1. $ NaN, which is not a number, but what causes these strange values โ€‹โ€‹and how can they help me when debugging?

I am using MinGW , which I assume is using IEEE 754 for floating point numbers.

Can anyone list all of these invalid values โ€‹โ€‹and what do they mean?

+53
c ++ c
Dec 07 '08 at 19:00
source share
3 answers

From the IEEE floating point exception in C ++ :

This page will answer the following questions.

  • My program just printed 1. # IND or 1. # INF (on Windows) or nan or inf (on Linux). What happened?
  • How do you know if a number is really a number, not NaN or infinity?
  • How can I find out more runtime information about NaN types and infinity?
  • Do you have sample code to show how this works?
  • Where can I find out more?

These questions are related to floating point exceptions. If you get some strange non-numeric output in which you expect a number, you either exceeded the final limits of floating point arithmetic, or asked to get the result undefined. To keep things simple, I will work with a double floating point type. Similar remarks are retained for float types.

Debugging 1. # IND, 1. # INF, nan and inf

If your operation will generate a larger positive number than can be stored in double, the operation will return 1. # INF on Windows or inf on Linux. Similarly, your code will return -1. # INF or -inf if the result is a negative number that is too large to hold in double. Dividing a positive number by zero creates positive infinity and dividing a negative number by zero creates negative infinity. The sample code at the end of this page will demonstrate some operations that produce infinity.

Some operations do not have mathematical meaning, for example, they take the square root of a negative number. (Yes, this operation makes sense in the context of complex numbers, but double represents a real number, and therefore there is no double representation of the result). The same is true for the logarithms of negative numbers. Both sqrt (-1.0) and log (-1.0) return NaN, a generic term for "number" that is "not a number". Windows displays NaN as -1. # IND ("IND" for "indefinite"), while Linux displays nan. Other operations that return NaN include 0/0, 0 * โˆž, and โˆž / โˆž. See the sample code below.

In short, if you get 1. # INF or inf, find overflow or division by zero. If you get 1. # IND or nan, look for illegal transactions. Maybe you just have a mistake. If it is more subtle and you have something that is difficult to calculate, see the section โ€œAvoid Overflows, Underutilization, and Loss of Accuracyโ€. This article provides tricks for calculating results that have an intermediate overflow when calculating directly.

+59
Dec 07 '08 at 19:21
source share

For those of you in the .NET environment, the following method may be convenient for filtering non-numbers (this example is in VB.NET, but it probably looks like C #):

If Double.IsNaN(MyVariableName) Then MyVariableName = 0 ' Or whatever you want to do here to "correct" the situation End If 

If you try to use a variable with a NaN value, you will get the following error:

The value was too large or too small for a decimal number.

+3
Aug 10 2018-12-12T00:
source share

Your question "what is it" has already been answered above.

As for debugging (your second question), but in developing libraries where you want to check for special input values, the following functions may be useful in Windows C ++:

_isnan (), _isfinite () and _fpclass ()

On Linux / Unix, you should find isnan (), isfinite (), isnormal (), isinf (), fpclassify () useful (and you may need to link to libm using the -lm flag of the compiler).

+2
Jul 11 '16 at 16:43
source share



All Articles