eXtremeSQL Floating-Point Data

eXtremeSQL provides the SQL data types FLOAT, REAL, DOUBLE, NUMERIC and DECIMAL to manage floating-point values.

For an overview see page SQL Data Types

The table in page SQL Data Types shows the correspondence between these SQL types and their underlying eXtremeDB storage types as well as the ranges of values possible for each. As can be seen in this table, REAL and DOUBLE are synonymous, as are NUMERIC and DECIMAL. Note that NUMERIC fields should be declared with the appropriate precision and width for the values they will contain.

Note that NUMERIC fields are implemented internally as integers. As explained in page Base Data Types, the actual storage space occupied is determined by the specified width. The Metatable column FieldSize displays the storage space occupied and the Precision displays the number of digits to the right of the decimal point; however, the Width reported in Metatable queries (-1) for floating-point fields is not relevant.

 

The following statements demonstrate some floating-point declarations, their storage and precision:

     
    create table t( f float, d double, r real, n numeric, 
            dec decimal, n62 numeric(6,2));
     
    select FieldName, FieldTypeName, FieldSize, Precision, Width from Metatable 
    where TableName = 't';
    FieldName       FieldTypeName   FieldSize       Precision       Width
    ------------------------------------------------------------------------------
    f       Real4   4       -1      -1
    d       Real8   8       -1      -1
    r       Real8   8       -1      -1
    n Numeric 8       0       -1
    dec     Numeric 8       0       -1
    n62     Numeric 4       2       -1
 

The highlighted fields show that REAL and DOUBLE declarations are equivalent, as are NUMERIC and DECIMAL. Note that the Precision for FLOAT, DOUBLE and REAL fields is undefined (-1), and, unless explicitly specified in their declaration, the same applies to NUMERIC and DECIMAL fields. Thus, values stored in these fields can be of any precision that will fit the FieldSize. However, if width and precision are declared for NUMERIC fields, the values stored will be rounded to the specified precision. Note that, if the width specified in the NUMERIC declaration is too small for the values inserted into the field, unpredictable values can result.

Floating-Point Type Conversion

As explained in page Data Type Conversion, when floating-point fields are used in expressions, the value of the field will be converted to a Real8 or Numeric to successfully perform statements. Also, when performing insert or update operations, eXtremeSQL may coerce values to the appropriate type which can lead to loss of precision. For example:

     
    create table n( i1 int(1));
    insert into n(i1) values(16.78);
    insert into n(i1) values(3.141);
     
    select i1 from n;
    i1
    --------------------------------------
    17
            
    3
            
     

Note that the float values 16.78 and 3.141 are first rounded to the nearest integer value before inserting into the integer(1) field i1 with the consequent loss of precision.