PostgreSQL DOUBLE PRECISION Data Type
Summary: in this tutorial, you will learn about the PostgreSQL DOUBLE PRECISION
data type and its features.
Introduction to the PostgreSQL double precision type
In PostgreSQL, the DOUBLE PRECISION
is an inexact, variable-precision numeric type.
Inexact means that PostgreSQL cannot exactly convert some values into an internal format and can only store them as approximations. Consequently, storing and querying a value might show a slight difference.
If your application requires exact storage and calculation, it’s recommended to use the numeric type instead.
Note that PostgreSQL double precision data type is an implementation of the IEEE Standard 754 for Floating-Point Arithmetic.
The following shows how to define a column with the DOUBLE PRECISION
type:
Alternatively, you can use the float8
or float
data type which is the same as DOUBLE PRECISION
:
A column of DOUBLE PRECISION
type can store values that have a range around 1E-307
to 1E+308
with a precision of at least 15 digits.
If you store a value that is out of the range, PostgreSQL will be unable to store it and raise an error.
If you store numbers with very high precision, PostgreSQL may round them to fit within the limitation of double precision. This may potentially lose some precision in the calculation.
If you store very small numbers close to zero, PostgreSQL may raise an underflow error due to the limitations of double precision data type, which may be unable to accurately represent such small values distinct from zero.
In practice, you’ll use the double precision type for storing scientific measurements.
PostgreSQL double precision type examples
Let’s take some examples of using the DOUBLE PRECISION
data type.
1) Basic double precision data type example
First, create a table called temperatures
to store temperature readings:
Second, insert some rows into the temperatures
table:
Output:
Third, calculate the average temperature of all locations:
Output:
2) Storing inexact values
First, create a table t
with the column c
of DOUBLE PRECISION
type:
Second, insert rows into the t
table:
Output:
Third, calculate the sum of values in the c column using the SUM()
function:
Output:
The output indicates that the sum of 0.1
, 0.1
, and 0.1
is not 0.3
but 0.30000000000000004
. This indicates that PostgreSQL cannot store the exact number 0.1
using the DOUBLE PRECISION
type.
2) Inserting too small numbers
The following statement attempts to insert a very small number into the c
column of the t
table:
It returns the following error:
The reason is that the number is too small and very close to zero. PostgreSQL cannot store it due to the limitation of the double precision type.
Summary
DOUBLE PRECISION
data type represents the inexact numbers.DOUBLE PRECISION
,FLOAT8
, orFLOAT
are synonyms.- Use
DOUBLE PRECISION
type to store inexact numbers andNUMERIC
type to store exact numbers.