>
> I recognize some of the patterns from OpenJDK's BigDecimal source. :)


Indeed, Brick\Math was largely inspired by Java's implementation!

The major difference to me is scale vs precision, ie. number of significant
> digits vs number of digits behind the decimal point. Not sure which is
> better, just noticing the difference.


Brick\Math does not have a concept of precision or "significant digits".
It only cares about scale, and has unlimited precision.

Its aim is to always return an exact result, unless rounding is explicitly
requested.
The scale is automatically adjusted for common operations such as plus(),
minus() and multipliedBy().
For dividedBy(), you have to explicitly specify the requested scale of the
result and an optional rounding mode; if no rounding mode is provided, and
the result does not fit in this scale, you get an exception.
If you don't know the scale but do know that the division yields a number
with a finite scale, you can use the exactlyDividedBy(), which will either
return an exact result with the correct scale, or throw an exception.

This is the first difference that strikes me with your current
implementation:
0.1 / 7 == 0.01428571428571428571428571429

Because the result is an infinite repeating decimal, in my opinion, your
Decimal class should not allow such a division without explicitly
specifying a scale and a rounding mode.
In other words, I would expect an exception here.

To exactly represent the result of this division, another concept such as
Brick\Math's BigRational can be used instead.

Ben

Reply via email to