Home Explore Blog CI



postgresql

9th chunk of `doc/src/sgml/datatype.sgml`
58c37c067f1f5f523b960931df208d506084a141f55e8e4f0000000100000fa1
 </para>
   </sect2>


   <sect2 id="datatype-float">
    <title>Floating-Point Types</title>

    <indexterm zone="datatype-float">
     <primary>real</primary>
    </indexterm>

    <indexterm zone="datatype-float">
     <primary>double precision</primary>
    </indexterm>

    <indexterm>
     <primary>float4</primary>
     <see>real</see>
    </indexterm>

    <indexterm>
     <primary>float8</primary>
     <see>double precision</see>
    </indexterm>

    <indexterm zone="datatype-float">
     <primary>floating point</primary>
    </indexterm>

    <para>
     The data types <type>real</type> and <type>double precision</type> are
     inexact, variable-precision numeric types. On all currently supported
     platforms, these types are implementations of <acronym>IEEE</acronym>
     Standard 754 for Binary Floating-Point Arithmetic (single and double
     precision, respectively), to the extent that the underlying processor,
     operating system, and compiler support it.
    </para>

    <para>
     Inexact means that some values cannot be converted exactly to the
     internal format and are stored as approximations, so that storing
     and retrieving a value might show slight discrepancies.
     Managing these errors and how they propagate through calculations
     is the subject of an entire branch of mathematics and computer
     science and will not be discussed here, except for the
     following points:
     <itemizedlist>
      <listitem>
       <para>
        If you require exact storage and calculations (such as for
        monetary amounts), use the <type>numeric</type> type instead.
       </para>
      </listitem>

      <listitem>
       <para>
        If you want to do complicated calculations with these types
        for anything important, especially if you rely on certain
        behavior in boundary cases (infinity, underflow), you should
        evaluate the implementation carefully.
       </para>
      </listitem>

      <listitem>
       <para>
        Comparing two floating-point values for equality might not
        always work as expected.
       </para>
      </listitem>
     </itemizedlist>
    </para>

    <para>
     On all currently supported platforms, the <type>real</type> type has a
     range of around 1E-37 to 1E+37 with a precision of at least 6 decimal
     digits. The <type>double precision</type> type has a range of around
     1E-307 to 1E+308 with a precision of at least 15 digits. Values that are
     too large or too small will cause an error. Rounding might take place if
     the precision of an input number is too high. Numbers too close to zero
     that are not representable as distinct from zero will cause an underflow
     error.
    </para>

    <para>
     By default, floating point values are output in text form in their
     shortest precise decimal representation; the decimal value produced is
     closer to the true stored binary value than to any other value
     representable in the same binary precision. (However, the output value is
     currently never <emphasis>exactly</emphasis> midway between two
     representable values, in order to avoid a widespread bug where input
     routines do not properly respect the round-to-nearest-even rule.) This value will
     use at most 17 significant decimal digits for <type>float8</type>
     values, and at most 9 digits for <type>float4</type> values.
    </para>

    <note>
     <para>
      This shortest-precise output format is much faster to generate than the
      historical rounded format.
     </para>
    </note>

    <para>
     For compatibility with output generated by older versions
     of <productname>PostgreSQL</productname>, and to allow the output
     precision to be reduced, the <xref linkend="guc-extra-float-digits"/>
     parameter can be used to select rounded decimal output instead. Setting a
     value of 0 restores the previous default of rounding the value to 6
     (for <type>float4</type>) or 15

Title: Floating-Point Types
Summary
The real and double precision data types in PostgreSQL are inexact, variable-precision numeric types that follow the IEEE Standard 754 for Binary Floating-Point Arithmetic. They are subject to rounding errors and may not store values exactly, which can lead to discrepancies. These types are not suitable for exact storage and calculations, and comparing two floating-point values for equality may not always work as expected. The range and precision of these types vary by platform, and output is typically presented in a shortened decimal representation.