[Teradata SQL] Compare float, double (floating point) and also the difference between decimal (fixed-point) of the difference between decimal and float double Decfloat

1 Introduction

float: single precision floating called number having 4 bytes, 32-bit approximation of a real number. Values ​​ranging -3.4E38 ~ 3.4E38 (7 valid bits)

double: referred to as a double precision floating point, byte containing the number of 8, 64-bit approximation of a real number. Numerical range -1.7E308 ~ 1.7E308 (15 or 16 significant bits)

decimal: decimal number is called, you can define your own it is a compact decimal places decimal. 128bit, numerical range -7.9 × 10 ^ 28 ~ 7.9 × 10 ^ 28, used in the calculation in the bank account. (28 bits or 29 active)

decimal (A, B) Description: 
decimal data type can store up to 38 digits, all numbers can be placed to the right of the decimal point. specify a maximum number of left and right of the decimal point specified decimal digits that can be stored, the maximum precision of 38. b specifies the maximum number of decimal places to the right of the decimal digits that can be stored. Decimals must be from
0 value between a. The default number of decimal places is 0 .
a float F =  345 .98756f; - Results are shown as 345.9876, only 7 significant bits for rounding the last digit. 

Double D = 345 .975423578631442d; - Results are shown as 345.975423578631 show only 15 significant digits, rounding the last one. 

- Note: float and double multiplying operation, the digital error will not overflow, there will be loss of accuracy. 

decimal dd = 345.545454879 ..... - can support 28 of the last rounding. 

- NOTE : When decimal type operation, due to numerical overflow error.

2. Use the scene

2.1 Any money associated with the need to use Decimal.

  • Decimal is accurate storage
  • float, double storage approximate, not exact

If both requirements for accuracy, but the number of decimal places stored fixed value, using decimal (numeric), the advantage that the number of decimal places can be customized, high precision.

As a special case, with such numerical ranges only huge float (real) type, and generally do not advocate the use of this type.

2.2 Do not use double-precision single-precision (to save memory, speed up computations)

Difference and double precision float is double high, 15 significant digits, float seven significant digits. But double consume twice as much memory a float, double the computing speed is much slower than float.  

2.3 search condition in the WHERE clause (and in particular = <> operators) should be avoided float or real columns. Preferably use restrictions do float and real column> or <comparison.

float data type itself is an inexact representation, that is, you put a data into, out of, when there may be a little bit error, and this error is little point in doing comparative data when it will result in data inconsistent.

3. Conversion Description

Conversion from decimal or numeric to float or real can cause loss of precision.

From int, smallint, float, real conversion to decimal or numeric can cause overflow. Mainly due to the large number of significant digits of the decimal, but the data than the range indicated by a small float and double.

4.DECFLOAT type

DECFLOAT decimal floating-point number is called, the new data type is introduced in DB2 V9.5, suitable for processing the exact decimal operations. It has a DECIMAL type of accuracy, but also has performance advantages of floating point numbers (no need to specify the number of decimal places), particularly suitable for use in the relevant application to handle monetary values.

DECFLOAT 16 and 34 provide two kinds of precision floating-point data types. They were expressed as DECFLOAT (16) and DECFOAT (34). If not specified accuracy, DECFLOAT default DECFLOAT (34). Precision data are both stored in 8 byte and 16-byte space.

CREATE TABLE EMPLOYEE (
SALARY DEC(14,2),
BONUS DECFLOAT(16),
COMMISSION DECFLOAT(34)
);
INSERT INTO EMPLOYEE VALUES (
123456.78,
1234567890.12,
123456789012345678901234.56
);

If we insert greater precision data, and for BONUS COMMISSION two columns, we do not need to make any changes, and for the SALARY column, it would need to trim the data so that the data meet the precision of DECIMAL columns specified. The results are as follows:

INSERT INTO EMPLOYEE VALUES (
123456.7891,
1234567890.1234,
123456789012345678901234.5678
);

 

 

 

Sql difference of decimal, float, double type

Compare float, double (floating point) and also the difference between decimal (fixed-point) of

Decfloat Baidu Encyclopedia

DB2 Data Types

Guess you like

Origin www.cnblogs.com/badboy200800/p/10968991.html