Decimal floating-point constants always include at
least one digit, and at least a period . or
an exponent (consisting of e or
E, and followed by an optional sign, then at
least one decimal digit). Without the period and the
exponent, such a sequence would interpreted as an
integer
constant. The exponent multiplies the represented
value by a power of 10. 0.0
,
0.
and .0
all represent zero. 1e3
represents
1000, 1.2e3
represents 1200, and
1.2e-3
represents 0·0012.
From C99,
hexadecimal floating-point constants are permitted,
prefixed by 0x or 0X, and use
p or P to introduce the
exponent, e.g., 0x1EFp+12
. The
exponent is required, to avoid the token being mistaken
for an integer constant. It is always expressed
in decimal, and multiplies the represented value by a
power of 2.
Floating-point constants may be suffixed by
f or F to give them the type
float
,
while l or L gives them the
type long double
.
Otherwise, FP constants are of type double
.
Floating-point constants are always non-negative, and
do not include a sign. -3.0
is a
unary-expression formed from a sign
-
and a floating-point constant
3.0
, but it is still a constant
expression.
- constant
floating-constant
- floating-constant
decimal-floating-constant
-
hexadecimal-floating-constant
from C99 - decimal-floating-constant
fractional-constant exponent-partopt floating-suffixopt
digit-sequence exponent-part floating-suffixopt
- fractional-constant
digit-sequenceopt . digit-sequence
digit-sequence .
- exponent-part
e signopt digit-sequence
E signopt digit-sequence
- digit-sequence
digit
digit-sequence digit
- digit
-
any of
0 1 2 3 4 5 6 7 8 9
- hexadecimal-floating-constant
hexadecimal-prefix hexadecimal-fractional-constant binary-exponent-part floating-suffixopt
hexadecimal-prefix hexadecimal-digit-sequence binary-exponent-part floating-suffixopt
- hexadecimal-fractional-constant
hexadecimal-digit-sequenceopt . hexadecimal-digit-sequence
hexadecimal-digit-sequence .
- binary-exponent-part
p signopt digit-sequence
P signopt digit-sequence
- hexadecimal-digit-sequence
hexadecimal-digit
hexadecimal-digit-sequence hexadecimal-digit
- hexadecimal-digit
-
any of
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F
- sign
+
-
- floating-suffix
f
l
F
L
Floating-point zero
Mathematically, zero does not have a sign.
However, in C, a floating-point zero may have
distinct positive and negative representations, but C
normally treats them as equal. C treats positive and
negative zero as equal, so you can't test for
negative zero with ==
, but you
can test for a magnitude of zero first, and then
check the sign with signbit
. Note, though, that
negative zero does not have to be distinct from
positive zero in all environments, and tests to
detect it will fail in those cases.
fpclassify
returns
FP_
for a positive or negative
zero argument.
Infinity
The floating-point types may permit
representations of infinity, and possibly distinct
values for positive and negative infinity.
<math.h>
defines
INFINITY
as a
constant that means infinity if available, or a very
large number if not.
#include <math.h>
int isfinite(real-floating-type x);
int isinf(real-floating-type x);
The macro isinf
yields non-zero for arguments with infinite
magnitiude, while isfinite
yields non-zero for arguments with finite magnitude.
fpclassify
returns FP_
for arguments with infinite magnitude, and
FP_
,
FP_
or
FP_
for arguments with
finite magnitude.
Normal and subnormal numbers
With normal floating-point values, the significand/mantissa begins with 1, and so has no leading zeros. Very small numbers may be represented with subnormal values, where the significand does have leading zeros.
The macros FLT_
,
DBL_
and LDBL_
indicate whether the types float
,
double
and long double
support subnormal representations respectively,
yielding 1
for yes,
0
for no, and -1
if undetermined.
#include <math.h>
int isnormal(real-floating-type x);
The macro
isnormal
will return non-zero if its
argument is normal, and zero otherwise. The macro
fpclassify
returns
FP_
for subnormal
arguments, and
FP_
for normal arguments.
Not-a-number (NaN)
The floating-point types may include the concept of not-a-number, or NaN. NaNs are special values that can propagate through mathematical operations silently (they are quiet NaNs), or raise an exception when they first occur (signalling NaNs). The C implementation may support both, either or none.
<math.h>
might
define NAN
,
a constant for float
,
represent a quiet NaN. The existence of this macro
firmly indicates that quiet NaNs are supported in at
least float
.
Quiet NaNs may also be generated from strings by
nan
, nanf
and
nanl
.
#include <math.h>
int isnan(real-floating-type x);
The macro isnan
yields non-zero when given a quiet NaN argument. The
macro
fpclassify
yields
FP_
when given a quiet NaN
argument.