Floating-point types
View as Markdownreal info
| Detail | Info |
|---|---|
| Size | 4 bytes |
| Aliases | float4 |
| Catalog name | pg_catalog.float4 |
| OID | 700 |
| Range | Approx. 1E-37 to 1E+37 with 6 decimal digits of precision |
double precision info
| Detail | Info |
|---|---|
| Size | 8 bytes |
| Aliases | float,float8, double |
| Catalog name | pg_catalog.float8 |
| OID | 701 |
| Range | Approx. 1E-307 to 1E+307 with 15 decimal digits of precision |
Syntax
<int> [.<frac>]
| Syntax element | Description |
|---|---|
<int>
|
An integer value. |
.<frac>
|
Optional. Fractional decimal digits. |
Details
Literals
Materialize assumes untyped numeric literals containing decimal points are
numeric; to use float, you must explicitly cast them as we’ve
done below.
Special values
Floating-point numbers have three special values, as specified in IEEE 754:
| Value | Aliases | Represents |
|---|---|---|
NaN |
Not a number | |
Infinity |
Inf, +Infinity, +Inf |
Positive infinity |
-Infinity |
-Inf |
Negative infinity |
To input these special values, write them as a string and cast that string to the desired floating-point type. For example:
SELECT 'NaN'::real AS nan
nan
-----
NaN
The strings are recognized case insensitively.
Aggregate precision
To support incremental updates, including retractions, Materialize accumulates
sum over real and double precision values in a fixed-point
representation. Aggregates computed from sum inherit its behavior: avg,
stddev, stddev_pop, stddev_samp, variance, var_pop, and var_samp.
Aggregates that do not sum their inputs, such as min and max, return exact
input values.
When these aggregates run in a dataflow, for example in an index, a
materialized view, a subscription, or a SELECT query that reads from a source
or table, each input value is truncated toward zero to a multiple of
2-24 (approximately 6E-8) before it is added:
- Values with a magnitude smaller than 2-24 contribute
0. - Values with fractional parts that are not multiples of 2-24 lose
precision, for example
0.1contributes0.09999996423721313. - The result is incorrect if the magnitude of the final sum reaches 2103 (approximately 1E+31).
Queries that Materialize evaluates entirely during planning, such as
aggregates over constant VALUES lists, use floating-point addition and are
not subject to this truncation.
If your application requires exact sums of fractional values, use
numeric, which is not subject to this truncation.
Valid casts
In addition to the casts listed below, real and double precision values can be cast
to and from one another. The cast from real to double precision is implicit and the cast from double precision to real is by assignment.
From real
You can cast real or double precision to:
To real
You can cast to real or double precision from the following types:
Examples
SELECT 1.23::real AS real_v;
real_v
---------
1.23