On Thu, 27 Apr 2023 at 07:03, Rowan Tommins <rowan.coll...@gmail.com> wrote:

> You have to load the value somehow; bcmath accepts strings directly, so
> parses the values each time, which isn't very efficient; but php-decimal
> uses strings to construct objects, as does GMP. I guess the other option
> for a decimal would be a pair of integers (whole part and fractional part).

They all have to use strings because this is the only way in PHP to
represent a decimal value. If PHP supports decimal values natively,
there will be no need to use strings in variable assignments and
calculations. Only when printing, outputting, etc. So I'm thinking
that the new native type `decimal` will be the best solution. It will
allow the fastest calculations and assignments without conversions to
and from strings.


> Far from counter-intuitive, that's an essential feature of any decimal
> implementation - as soon as the value is in a floating point variable,
> it's *already too late* to make it a fixed-precision decimal.

As a user, I can't see any issues assigning a float value to a decimal
variable. If it is a float 0.1999999999, then I can't see any issues
with assigning it to a decimal and rounding it to 0.2. But I would
like to see these conversions as PHP internals without intermediate
string values. I'm okay if PHP emits some warnings here and suggest
explicit rounding if needed.


> If we made deeper changes to make it scalar
> (which would be A LOT of work), it would be there for everyone.

Agree. Decimal versions of math functions should also be introduced,
not only internals related to the new type. If we go with the Decimal
class approach, we still will be tight to the string type as the
intermediary storage. But I think technical details should not affect
the top-level discussion. I don't know how it works in the PHP
community, but I would love to see this proposal implemented in the
next major PHP version. It could be a great new feature.

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to