'double' / 'float' revised.. think about it.

php.internals

Ron Korving

21 years ago
Personal recent frustrating float issues made me think of the following... At the moment apparently the C-type double is used for PHP's float (or double if you please) storage. I believe it is the philosophy of PHP to make things easy for its users, and in this perspective I was thinking.. hasn't the time come to reimplement the PHP float type in a decimal-safe way? Of course it would be a small performance hit, but not too noticable I think, especially when utilizing simd instructions which a compiler may or may not already do. I think, especially with todays powerful systems, performance (although still a big concern) is no longer the only primary concern. Shouldn't PHP abandon the speed of the double in C on this one, and implement a custom implementation of the floating point type which is much more accurate, even at the cost of a few extra bytes and cycles? Userfriendlyness over performance. I think it would be one hell of a feature for PHP to be mathematically correct, a world in which 1.5555 is really 1.5555 and not something that approaches it, and where (for example) rounding it would result in expected behaviour. I think PHP would automagically be more suitable for various (financial for example) applications which could potentially result in more popularity for PHP as a whole. I know there are alternatives for accurate mathematics, but they (naturally) depend on strings which make them a lot slower than a native solution would be, and besides that, they are less nice to use compared to +,-,/,*,% operators. It's just an idea of course, and I'm curious if there are others who feel the same way about this. Ron

Hartmut Holzgraefe

21 years ago
Ron Korving wrote: [...]
> Shouldn't PHP abandon the speed of the double in C on this one
[...] It's not only a matter of speed but also of changing interfaces to all extensions using doubles (bundled, PECL and 3rd party or local ones) ...
-- Hartmut Holzgraefe <hartmut@php.net>

Ron Korving

21 years ago
Yeah, I figured that, but.. changes come as new major PHP releases come. Maybe this would be something for 5.2/3/4 or 6.0, right? :) Most likely 6.0 I guess. Personally, I just think it's a problem that really should be tackled when in time PHP6 is to be released. If it isn't done by then, waiting for PHP7 would just take too long ;) I think in 2010 you'd simply be too late. Ron "Hartmut Holzgraefe" <hartmut@php.net> wrote in message news:42094115.8010903@php.net...

Marcus Börger

21 years ago
Hello Ron, it doesn't matter how accurate your floating or whatever you call it is. Financial applications require native support which can be realized using bcmath. Whatever you think besides that - the problem is that any errors accumulate - that's the nature of float. If you would like to know whats IEEE 754 read this http://docs.sun.com/source/806-3568/ncg_goldberg.html best regards marcus Tuesday, February 8, 2005, 8:15:31 PM, you wrote:
> Personal recent frustrating float issues made me think of the following...
> At the moment apparently the C-type double is used for PHP's float (or > double if you please) storage. I believe it is the philosophy of PHP to make > things easy for its users, and in this perspective I was thinking.. hasn't > the time come to reimplement the PHP float type in a decimal-safe way? Of > course it would be a small performance hit, but not too noticable I think, > especially when utilizing simd instructions which a compiler may or may not > already do. I think, especially with todays powerful systems, performance > (although still a big concern) is no longer the only primary concern. > Shouldn't PHP abandon the speed of the double in C on this one, and > implement a custom implementation of the floating point type which is much > more accurate, even at the cost of a few extra bytes and cycles? > Userfriendlyness over performance. I think it would be one hell of a feature > for PHP to be mathematically correct, a world in which 1.5555 is really > 1.5555 and not something that approaches it, and where (for example) > rounding it would result in expected behaviour. I think PHP would > automagically be more suitable for various (financial for example) > applications which could potentially result in more popularity for PHP as a > whole.
> I know there are alternatives for accurate mathematics, but they (naturally) > depend on strings which make them a lot slower than a native solution would > be, and besides that, they are less nice to use compared to +,-,/,*,% > operators.
> It's just an idea of course, and I'm curious if there are others who feel > the same way about this.
> Ron
-- Best regards, Marcus mailto:helly@php.net

Ron Korving

21 years ago
That's my whole point Marcus, but the nature of the native floating point types is something you can get around by implementing your own floating point type, based on a base-10 system, in the same way that for example bcmath does it. So my suggestion is; stop using float and double, and make your own type. Small performance hit, and it will probably cost you about 1 byte extra per floating point variable, but then it will be accurate. Ron "Marcus Boerger" <helly@php.net> wrote in message news:719431098.20050209002446@marcus-boerger.de...
> Hello Ron, > > it doesn't matter how accurate your floating or whatever you call it is. > Financial applications require native support which can be realized using > bcmath. Whatever you think besides that - the problem is that any errors > accumulate - that's the nature of float. > > If you would like to know whats IEEE 754 read this > http://docs.sun.com/source/806-3568/ncg_goldberg.html > > best regards > marcus > > Tuesday, February 8, 2005, 8:15:31 PM, you wrote: > > > Personal recent frustrating float issues made me think of the
following...
> > > At the moment apparently the C-type double is used for PHP's float (or > > double if you please) storage. I believe it is the philosophy of PHP to
make
> > things easy for its users, and in this perspective I was thinking..
hasn't
> > the time come to reimplement the PHP float type in a decimal-safe way?
Of
> > course it would be a small performance hit, but not too noticable I
think,
> > especially when utilizing simd instructions which a compiler may or may
not
> > already do. I think, especially with todays powerful systems,
performance
> > (although still a big concern) is no longer the only primary concern. > > Shouldn't PHP abandon the speed of the double in C on this one, and > > implement a custom implementation of the floating point type which is
much
> > more accurate, even at the cost of a few extra bytes and cycles? > > Userfriendlyness over performance. I think it would be one hell of a
feature
> > for PHP to be mathematically correct, a world in which 1.5555 is really > > 1.5555 and not something that approaches it, and where (for example) > > rounding it would result in expected behaviour. I think PHP would > > automagically be more suitable for various (financial for example) > > applications which could potentially result in more popularity for PHP
as a
> > whole. > > > I know there are alternatives for accurate mathematics, but they
(naturally)
> > depend on strings which make them a lot slower than a native solution
would
> > be, and besides that, they are less nice to use compared to +,-,/,*,% > > operators. > > > It's just an idea of course, and I'm curious if there are others who
feel