What libraries are missing?

Related to calculating decimals / reals without floating point error, there was a neat PLDI 2020 paper called Towards an API for the real numbers, which gives a set of primitives that can be calculated exactly, to arbitrary precision. This includes not only simple arithmetic, but also trig functions, exponentials / logarithms, e, pi, etc. (this algorithm is used to power Android’s calculator app)

I recently implemented it in OCaml. While I haven’t packaged it on opam yet, I do have a demo online (thanks to js_of_ocaml).

3 Likes

First of all, this is a new paper by the Hans Boehm? Very cool.

I have a question about the paper. The abstract mentions: ‘previous attempts to provide accurate and understandable results for such applications using the recursive reals were great steps in the right direction, but they do not suffice. Comparing recursive reals diverges if they are equal.’ What is this referring to? As far as I can tell it’s not talking about fixed-point decimals like Java’s BigDecimal?

For your demo, I am trying the expression: (1.356 + 1.355) / 2.0, and getting this error: (Failure "conversion from int64 to int failed: 1603281467343897 is out of range"). Am I doing something wrong?

Most real numbers cannot be represented by a single numerical value. So, the usual representation of real numbers is with functions accuracy -> value. For example, you ask the function for a rational number that approximates the real number with a given accuracy. You can then define addition, multiplication, and so on, over these functions. But it is fundamentally impossible to define equality. If the two input functions represent the same real numbers, the equality operator will never return.

1 Like

Thanks for pointing out this example. My test suite only runs on desktop where this doesn’t trigger an error (it only shows up in js_of_ocaml, which uses the JavaScript Number type to represent ints), so I hadn’t caught it. This particular example now works, but it’s certainly possible there are others (I haven’t yet removed all exception-triggering conversions from the code).

1 Like