Decimal arithmetic

Is there any interest in having decimal arithmetic in Racket?

Decimals are similar to rational numbers (rationals whose denominator is a power or ten).

There are basically two approaches one can imagine:

  1. arbitrary-precision decimals
  2. fixed bit-width decimals

For (1), Racket already has them in the sense that you can read decimals exactly with the #e prefix. We don't have (2), which I understand along the lines of the decimal128 IEEE standard, where you get about 34 significant digits and an exponent of about 6000. The fixed bit width means that you can't represent everything, of course, but still, you can represent quite a lot. There are a few C++ libraries out there that support these big 128-bit decimals. Indeed, there's eleven support for them out-of-the-box in GCC, but not yet in Clang/LLVM as of writing.

Curious to hear from the math/number fand whether this is an interesting idea or whether this is unnecessary, too complicated, not enough value.


1 Like

A few thoughts about this:

  1. Usually arbitrary-precision decimal arithmetic is not the same as Racket's exact rational arithmetic; in particular usually division is not implemented exactly.
  2. For limited-precision decimal, the question is always what are the advantages and disadvantages vs using binary floats. For IEEE decimal128, there are a few possible advantages:
    a. Twice the width, allowing for more precision than Racket's 64-bit floats. But see double-flonums in the math library for more precision.
    b. Decimal vs binary, allowing representing things like 0.3 exactly, which can be helpful for currency.
  3. How valuable would it be to have this as a pure-Racket library (easy to implement), a package that bundled some C++ code, or something that was built-in to the Racket runtime?
1 Like

We have arbitrary precision floating point numbers in math/bigfloat.
Maybe decimal arithmetic could adopt a similar API?

1 Like