fundamental problems with floats in PHP

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
mindplay
Forum Commoner
Posts: 25
Joined: Sat Sep 16, 2006 1:32 pm

fundamental problems with floats in PHP

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: fundamental problems with floats in PHP

Post by Kieran Huggins »

would http://www.php.net/set_locale affect those results?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: fundamental problems with floats in PHP

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: fundamental problems with floats in PHP

Post 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.
dml
Forum Contributor
Posts: 133
Joined: Sat Jan 26, 2008 2:20 pm

Re: fundamental problems with floats in PHP

Post 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).
Post Reply