Question of an example for storing value of FLOAT type

Viewed 74

I read a book and an example about FLOAT value,

Data type : FLOAT

Original Value : 1234567.89

Value stored : 1234570 

Byte used : 4

My wondering point is that I think if FLOAT can save up to 7 digits then why storing Value is 1234570 not 1234568,

then why this example round up this value from units(I mean first digit to the left of decimal point).

Thank you



======== edit ==================================

My book is Murach's MySQL, and I'm sorry to say that, this was my fault , actually This book is not telling that "FLOAT can save up to 7 digits" like I said above,

I think I was misunderstanding the meaning of this quote

The FLOAT type can be used to store a single-precision number, which provides for numbers with up to 7 significant digits. - p238 from Murach's MySQL 3rd edition

============================ add test result ============

I've just test about FLOAT and DOUBLE type, same input and different Output

FLOAT shows always 0 at the last number.

DOUBLE is keeping the number.

I'm curious as you can see, Float does not round number up at 65.00 (second row from '====='). I can not yet figure out why this is happened.. enter image description here

1 Answers

If some source of information said that FLOAT can “save up to 7 digits,” then it is wrong.

When the IEEE-754 binary32 format is used, a number is represented using binary digits, not decimal digits. There is no exact correspondence between decimal digits and binary digits. The precision of the binary32 format is sufficient that any six-digit decimal number within range can be converted to binary32 and, when converted back to a six-digit decimal number, the result will be equal. (Here, a six-digit decimal number means a numeral with six significant digits, such as 123456000, 1.23456•10−13, and so on.)

Many longer decimal numbers can be converted to binary32 and back without change, but this depends on them coinciding with the numbers representable in the format.

For 1234567.89, the closest number representable in the binary32 format is 1234567.875. This is the value that should result from converting it to the binary32 format. If “1234570” was printed, I expect that was the result of rounding to six significant digits for display, so that the true value was not shown. Another possibility is that fewer than the 24 bits supported in the binary32 significand were used.

Related