FLOAT

CockroachDB supports various inexact, floating-point number data types with up to 17 digits of decimal precision.

They are handled internally using the standard double-precision (64-bit binary-encoded) IEEE754 format.

Names and Aliases

NameAliases
FLOATNone
REALFLOAT4
DOUBLE PRECISIONFLOAT8

Syntax

A constant value of type FLOAT can be entered as a numeric literal.For example: 1.414 or -1234.

The special IEEE754 values for positive infinity, negative infinityand NaN (Not-a-Number) cannot beentered using numeric literals directly and must be converted using aninterpreted literal or anexplicit conversionfrom a string literal instead.

The following values are recognized:

SyntaxValue
inf, infinity, +inf, +infinity+∞
-inf, -infinity-∞
nanNaN (Not-a-Number)

For example:

  • FLOAT '+Inf'
  • '-Inf'::FLOAT
  • CAST('NaN' AS FLOAT)

Size

A FLOAT column supports values up to 8 bytes in width, but the total storage size is likely to be larger due to CockroachDB metadata.

Examples

  1. > CREATE TABLE floats (a FLOAT PRIMARY KEY, b REAL, c DOUBLE PRECISION);
  1. > SHOW COLUMNS FROM floats;
  1. +-------------+------------------+-------------+----------------+-----------------------+-------------+
  2. | column_name | data_type | is_nullable | column_default | generation_expression | indices |
  3. +-------------+------------------+-------------+----------------+-----------------------+-------------+
  4. | a | FLOAT | false | NULL | | {"primary"} |
  5. | b | REAL | true | NULL | | {} |
  6. | c | DOUBLE PRECISION | true | NULL | | {} |
  7. +-------------+------------------+-------------+----------------+-----------------------+-------------+
  8. (3 rows)
  1. > INSERT INTO floats VALUES (1.012345678901, 2.01234567890123456789, CAST('+Inf' AS FLOAT));
  1. > SELECT * FROM floats;
  1. +----------------+--------------------+------+
  2. | a | b | c |
  3. +----------------+--------------------+------+
  4. | 1.012345678901 | 2.0123456789012346 | +Inf |
  5. +----------------+--------------------+------+
  6. (1 row)
  7. # Note that the value in "b" has been limited to 17 digits.

Supported casting and conversion

FLOAT values can be cast to any of the following data types:

TypeDetails
INTTruncates decimal precision and requires values to be between -2^63 and 2^63-1
DECIMALCauses an error to be reported if the value is NaN or +/- Inf.
BOOL0 converts to false; all other values convert to true
STRING

See also

Data Types

Was this page helpful?
YesNo