The GNU C Library: Floating Point Classes

Next: , Previous: , Up: Arithmetic   [Contents][Index]


20.4 Floating-Point Number Classification Functions

ISO C99 defines macros that let you determine what sort of floating-point number a variable holds.

Macro: int fpclassify (float-type x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This is a generic macro which works on all floating-point types and which returns a value of type int. The possible values are:

FP_NAN

The floating-point number x is “Not a Number” (see Infinity and NaN)

FP_INFINITE

The value of x is either plus or minus infinity (see Infinity and NaN)

FP_ZERO

The value of x is zero. In floating-point formats like IEEE 754, where zero can be signed, this value is also returned if x is negative zero.

FP_SUBNORMAL

Numbers whose absolute value is too small to be represented in the normal format are represented in an alternate, denormalized format (see Floating Point Concepts). This format is less precise but can represent values closer to zero. fpclassify returns this value for values of x in this alternate format.

FP_NORMAL

This value is returned for all other values of x. It indicates that there is nothing special about the number.

fpclassify is most useful if more than one property of a number must be tested. There are more specific macros which only test one property at a time. Generally these macros execute faster than fpclassify, since there is special hardware support for them. You should therefore use the specific macros whenever possible.

Macro: int iscanonical (float-type x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

In some floating-point formats, some values have canonical (preferred) and noncanonical encodings (for IEEE interchange binary formats, all encodings are canonical). This macro returns a nonzero value if x has a canonical encoding. It is from TS 18661-1:2014.

Note that some formats have multiple encodings of a value which are all equally canonical; iscanonical returns a nonzero value for all such encodings. Also, formats may have encodings that do not correspond to any valid value of the type. In ISO C terms these are trap representations; in the GNU C Library, iscanonical returns zero for such encodings.

Macro: int isfinite (float-type x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This macro returns a nonzero value if x is finite: not plus or minus infinity, and not NaN. It is equivalent to

(fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE)

isfinite is implemented as a macro which accepts any floating-point type.

Macro: int isnormal (float-type x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This macro returns a nonzero value if x is finite and normalized. It is equivalent to

(fpclassify (x) == FP_NORMAL)
Macro: int isnan (float-type x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This macro returns a nonzero value if x is NaN. It is equivalent to

(fpclassify (x) == FP_NAN)
Macro: int issignaling (float-type x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This macro returns a nonzero value if x is a signaling NaN (sNaN). It is from TS 18661-1:2014.

Macro: int issubnormal (float-type x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This macro returns a nonzero value if x is subnormal. It is from TS 18661-1:2014.

Macro: int iszero (float-type x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This macro returns a nonzero value if x is zero. It is from TS 18661-1:2014.

Another set of floating-point classification functions was provided by BSD. The GNU C Library also supports these functions; however, we recommend that you use the ISO C99 macros in new code. Those are standard and will be available more widely. Also, since they are macros, you do not have to worry about the type of their argument.

Function: int isinf (double x)
Function: int isinff (float x)
Function: int isinfl (long double x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function returns -1 if x represents negative infinity, 1 if x represents positive infinity, and 0 otherwise.

Function: int isnan (double x)
Function: int isnanf (float x)
Function: int isnanl (long double x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function returns a nonzero value if x is a “not a number” value, and zero otherwise.

NB: The isnan macro defined by ISO C99 overrides the BSD function. This is normally not a problem, because the two routines behave identically. However, if you really need to get the BSD function for some reason, you can write

(isnan) (x)
Function: int finite (double x)
Function: int finitef (float x)
Function: int finitel (long double x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function returns a nonzero value if x is finite or a “not a number” value, and zero otherwise.

Portability Note: The functions listed in this section are BSD extensions.


Next: , Previous: , Up: Arithmetic   [Contents][Index]