8.4. Real
A real is an IEEE 754 32-bit floating-point value. A real can be
represented by an f32 in MLIR.
8.4.1. Declaration
A real value is declared with the keyword real. If the variable is not
initialized explicitly, it is set to 0.0 (its zero value).
8.4.2. Literals
A real literal can be specified in several ways. A leading zero is
not necessary and can be inferred from a leading decimal point. Likewise,
a trailing zero is not necessary and can be inferred from a trailing
decimal point. However, at least one digit must be present in order to be
parsed. For example:
42.0
42.
4.2
0.42
.42
. // Illegal.
A real literal can also be created by any valid real or integer
literal followed by scientific notation indicated by the character e or
E and an optionally-signed integer literal (e.g. -3, +9).
Scientific notation multiplies the
first literal by \({10}^{x}\), e.g. \(4.2\mathrm{e}{-3}=4.2
\times10^{-3}\). For example:
4.2e-1
4.2e+9
4.2E5
42.e+7
.42e-7
42E6
8.4.3. Operations
Floating-point operations use the same operators as integer operations, with the differences described below.
The % operator is defined on real operands as the decimal remainder; for
example 5.5 % 2.0 == 1.5. The result takes the sign of the left operand (the
dividend), as with C fmod – for example -5.5 % 2.0 == -1.5. Because == on reals is exact IEEE 754 equality
(bit-for-bit), such an equation holds only when the operands and the result are
all exactly representable; approximate numeric equality is not provided and would
require a user-defined tolerance comparison.
Real values always use the IEEE 754 representation and semantics for
not-a-number (NaN), the signed infinities (Infinity), and signed
zeros. Real arithmetic therefore never raises a MathError: overflowing
the finite real range yields a signed Infinity, division or % by
0.0 yields a signed Infinity (or NaN for 0.0 / 0.0), and every
subsequent operation on Infinity and NaN operands follows IEEE 754.
Comparisons follow from this rule, exactly as in IEEE 754. With at least one
NaN operand, every affirmative comparison – ==, <, >,
<=, >= – evaluates to false, while the negative comparison
!= evaluates to true. A NaN is unordered with respect to every
value, including an Infinity and including another NaN; so when x
is NaN, x == x is false and x != x is true. Comparisons
that involve only finite values and the infinities behave as ordinary IEEE 754
comparisons; for example 1.0 / 0.0 compares greater than every finite
real, and +Infinity compares equal to +Infinity.
Exponentiation (^) likewise follows IEEE 754: a negative base raised to a
fractional exponent – for example (-2.0)^0.5 – is not a MathError but
yields NaN.
Operator precedence and associativity are specified once, for all types, in the table of operator precedence.
8.4.4. Type Casting and Implicit Casts
To see the types that real may be cast and/or implicitly cast to, see
the sections on Type Casting and Implicit Casts
respectively.