eXtremeSQL provides the SQL data types
FLOAT,REAL,DOUBLE,NUMERICandDECIMALto 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,
REALandDOUBLEare synonymous, as areNUMERICandDECIMAL. Note thatNUMERICfields should be declared with the appropriateprecisionandwidthfor the values they will contain.
Note that
NUMERICfields are implemented internally as integers. As explained in page Base Data Types, the actual storage space occupied is determined by the specifiedwidth. The Metatable columnFieldSizedisplays the storage space occupied and thePrecisiondisplays the number of digits to the right of the decimal point; however, theWidthreported 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 -1The highlighted fields show that
REALandDOUBLEdeclarations are equivalent, as areNUMERICandDECIMAL. Note that thePrecisionforFLOAT,DOUBLEandREALfields is undefined (-1), and, unless explicitly specified in their declaration, the same applies toNUMERICandDECIMALfields. Thus, values stored in these fields can be of any precision that will fit theFieldSize. However, ifwidthandprecisionare declared forNUMERICfields, the values stored will be rounded to the specifiedprecision. Note that, if thewidthspecified in theNUMERICdeclaration 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
Real8orNumericto successfully perform statements. Also, when performinginsertorupdateoperations, 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 3Note that the float values
16.78and3.141are first rounded to the nearest integer value before inserting into theinteger(1)fieldi1with the consequent loss of precision.