[fpc-devel] MySQL 5.1 and Double (trouble)

Ludo Brands ludo.brands at free.fr
Mon Jul 11 17:14:02 CEST 2011


> >>> How do you get the 40734.825668912003 value?
> >> Fields.FieldByName(Value).AsFloat();
> > 
> > Floattostr limits precision to 15 digits (40734.8256689120). When 
> > updating the mysql database it converts the float to a string and 
> > looses the last 2 digits. The last digits are returned by 
> gdb or the 
> > str procedure with precision >15, but won't be displayed in data 
> > controls.
> 
> It doesn't make sense to display more digits than stored in 
> the binary variable. 

I agree. I just tried to explain what happens behind the scenes. 

> There is no loss, only rounding errors in the 
> conversion from 
> binary to decimal text.
> 

There is a small loss. 2^52 = 4503599627370496 which is almost 16 digits. 15
digits can be represented by a 50 bits mantissa. So you're losing 2 bits in
precision by truncating to 15 digits. 
I wrote earlier that the mysql client returned 40734.825668912 when set to
40734.825668912039. That is mysql truncating to 15 characters the string
representation of the double for displaying. But when I do a select t_double
- 40734.825668 I do get 9.1204....e-07. In case of fpc setting the same
value select t_double - 40734.825668 returns 9.1200....e-07. And that is
because fpc converts to a string when updating the value losing the last 2
bits. 

Ludo





More information about the fpc-devel mailing list