Hello everyone, I've been following the discussion threads and forming my
own opinion to share since I have done a bunch of financial stuff
throughout my career: I did the integers only at the application level and
DECIMAL(20,8) in the database due to handling Bitcoin, Litecoin, etc.

My feeling on the discussion is that it got seriously sidetracked from the
core tenet of what is actually needed from the PHP engine/core to
discussing developing/upgrading a library that can handle money, scientific
calculations, yada yada yada. Basically, in my view, the discussion has
been catastrophically scope-creeped to a point where nobody could agree on
anything and discuss things that were irrelevant to the initial scope.

To me, the BCMath library stuff is just that - a BCMath library. It's a
tool that can handle any size number. It's a specialized tool. And,
frankly, for the vast majority of use cases, it's complete overkill.

If we are talking about implementing a Decimal type into the language as a
first-class citizen alongside int/float/etc, do we really need it to handle
numbers outside 64-bit space? Ints have a size limit (64 bits), floats also
have a defined range. Why not have decimal be represented as 2 64-bit ints
at the engine level, and similarly to floating-point numbers, you can have
a php.ini setting where you can define the precision you want? Floats have
a default of 14 positions. Why not have a setting that defines precision
for the decimal type, set it to the same 14 positions, and you can have a
decimal type that has 114 bits for the integer part and 14 bits for the
floating-point part? In the vast majority of cases, for all practical
intents and purposes, that would be enough. For the rest - you have
ext/decimal, BCMath, GMP extensions (and by all means, improve those as
much as needed, make them as powerful as Python's math libs). This approach
has some major benefits: if done right, it's just another type that is
compatible with float, but does integer precision math, and having the
precision of 14 in the vast majority of needs is basically overkill
already. Ideally, you should be able to just replace your float and ints
with decimal type hints and just do the roundings/formatting via the usual
means of round/ceil/floor/number_format. Normal math just works. If any
part of the expression has a decimal type, the result is a decimal number.
The only sticking point I see is how to define a decimal type variable
since we do not have var/let for/const; we can only define types on class
properties and their constants. Do we add a function decimal(int|float
$value): decimal? Or do we need to do prep work to be able to define
variables with type? Another idea I have is to just do $decimal =
(decimal)10.054 when instantiating a variable. Actually, that's not that
uncommon to do it like that already when you want to ensure the result is
of a certain type, PSL library does a lot of that and I do quite like it.

Long story short, give people a tool that's simple and works, things like
scale and all that stuff we can just handle in userland code ourselves
because everyone has different needs, different scales, and so on. It's the
same as right now with integers - if you require an integer bigger than 64
bits, you use GMP/BCMath/etc. You are also not going to have fun with
databases and PDO because there are going to be some shenanigans there too.
Basically, at that point, you are running against various other PHP engine
limitations and when software has to be written with those considerations
in mind anyway in literally any language to begin with. Some are easier
than others.

Sorry for it being a bit long, I'm happy to clarify/expand on any parts you
have questions about.
-- 

Arvīds Godjuks
+371 26 851 664
arvids.godj...@gmail.com
Telegram: @psihius https://t.me/psihius

Reply via email to