Page 1 of 1
fundamental problems with floats in PHP
Posted: Thu Jun 26, 2008 9:39 am
by mindplay
I have written a
full report about the fundamental problems with handling of floating point values in PHP, in the hopes that this will stir some interest from the PHP development team.
My argument basically is, that doing locale-aware display-formatting of floats by default, is fundamentally wrong. My report details why, provides analysis of all related functions and methods, and proposes a solution.
I have also filed a
bug report on php.net - please feel free to comment and rate the report there.
Re: fundamental problems with floats in PHP
Posted: Thu Jun 26, 2008 11:15 am
by Kieran Huggins
Re: fundamental problems with floats in PHP
Posted: Thu Jun 26, 2008 11:25 am
by Benjamin
These issues are deeper than PHP.
In C++:
Converting a bigger floating-point type to a smaller floating-point type such as double to float can result in a loss of precision or out of range value.
Converting a floating-point type to integer will result in a loss of the fractional part and possible out of range value.
Converting a bigger integer type to a smaller integer type such as long to short will result in a possible out of range value and typically only the low-order bytes are copied.
C++ Guarantees that a float will have at least 32 bits and a double at least 48 bits.
Re: fundamental problems with floats in PHP
Posted: Thu Jun 26, 2008 11:51 am
by Ambush Commander
Look, if you need arbitrary precision, you're going to have to use bcmath or gmp. As for the locale problems, they certainly should be looked into, but I've certainly learned to mistrust casting from floats to strings, and back again; you have to tell PHP explicitly what to do.
Re: fundamental problems with floats in PHP
Posted: Thu Jun 26, 2008 5:25 pm
by dml
The locale thing is a bit assymetric, alright. If PHP is "helpful" enough to convert 123.456 to "123,456" if the computer is in Denmark, one would expect it to do the conversion in the other direction.
Roundtripping from floats to string representations and back again is difficult to do without losing information: it's a matter of converting between base 2 scientific notation and base 10 scientific notation. PHP seems to use IEEE doubles most of the time, which provide 53 bits of precision, the equivalent of between 15 and 16 digits. Google has a
patch to mysqldump for non-lossy roundtripping - by ensuring that floats are written out with 17 digits, the idea is that exactly the same value is read in again. It's worth quoting what they say about it:
This is done because conversions from double to decimal and then back to double are lossless (the initial double value is equal to the final double value) when the double to decimal conversion generates 17 digits of precision. This is generally true. There are some values for float and double for which this is not true (subnormal).