Floating Point Operations
We support standard IEEE 754 floating point types.
double is a 64-bit standard FP value. Accepted synonyms are
float8 and float64.
float is a 32-bit standard FP value. Accepted synonyms are
float4, and float32.
Floating point values include special values, such as NaN (not a
number), -Infinity, and -Infinity. An alternative spelling for
-Infinity is -inf, and an alternative spelling for Infinity is
inf, and an alternative spelling for ‘NaN’ is ‘nan’. When written
as SQL literals, these values have to be surrounded by simple quotes:
'inf'. Please note that these strings are case-sensitive and spaces
are ignored.
Infinity plus any finite value equals Infinity, as does Infinity plus
Infinity. Infinity minus Infinity yields NaN.
NaN (not a number) value is used to represent undefined results.
An operation with a NaN input yields NaN. The only exception
is when the operation’s output does not depend on the NaN value:
an example is NaN raised to the zero power yields one.
In sorting order NaN is considered greater than all other values.
The legal operations are + (plus, unary and binary), - (minus,
unary and binary), * (multiplication), / (division), %
(modulus).
Modulus happens as follows:
For: mod = x % y
- if
x >= 0andy > 0then:x - (floor(x / y) * y) - if
x >= 0andy < 0then:x % abs(y) - if
x < 0andy > 0then:- abs(x) % y - if
x < 0andy > 0then:- abs(x) % abs(y)
Division by zero returns Infinity, (or NaN in case of 0e0 / 0e0).
Modulus by zero return NaN.
Casting a string to a floating-point value will produce the value
0 when parsing fails.
Casting a value that is out of the supported range to a floating
point type will produce a value that is inf or -inf.
Casting a floating-point value to string, float is rounded off
to 6 decimal places and double is rounded off to 15 decimal places.
Please note that numeric values with a decimal point have the
decimal type by default. To write a floating-point literal you have
to include the e for exponent using the following grammar:
digits.digits[e[+-]digits]
[digits].digits[e[+-]digits]
Alternatively, you can use an explicit cast:
REAL '1.23' -- string style
1.23::REAL -- PostgreSQL style