Directed rounding
Martin von Gagern
gagern at ma.tum.de
Sat Jun 20 20:29:22 UTC 2015
Hello,
I'm getting the impression that the next version of EcmaScript after
2015 (congratulations to that, by the way) will support quite advanced
numerical computations, with SIMD and all that. To complement that, I'd
wish for support for directed rounding. I'd like to have intrinsics to
perform elementary arithmetic operations with controlled rounding, in
particular rounding to positive and negative infinity, but for the sake
of completeness also towards and away from zero. Note that this is about
rounding the float mantissa as part of an arithmetic operation, and not
rounding to integers as Math.round does it.
The applications I have in mind could be summarized as certified
numerical computation results. When a numerical computation is being
used as the basis for some decision making, that decision may be
affected by rounding errors. One way to deal with the situation is by
performing the computations in question using interval arithmetic. If
the final result contains zero, its sign is unknown, so the decision
cannot be reliably made, and the application can react appropriately.
One example of why this is needed could be the CGAL library. It's a C++
library of geometric operations, like e.g. convex hull. Many of these
operations can only work if the underlying number type allows for exact
evaluation of predicates, which can be achieved by doing reasonably fast
interval arithmetic in most cases, but slow arbitrary-precision rational
arithmetic if numerical precision is insufficient. It would be nice if
similar code could be written in ES as well.
I don't want to ask for a ready-to-use interval arithmetic library in ES
(although I wouldn't mind if you decided that this would be preferable).
Such a library could be implemented in application code. But performing
interval arithmetic usually relies on directed rounding, to ensure that
the interval bounds will expand as required. This is supported by
hardware, and it would be nice if that hardware support could be made
available to the application, since reproducing the same in software is
hardly efficient (but can be done for the sake of compatibility on
low-end devices).
What I envision is one object per rounding mode, which contains methods
for elementary arithmetic operations: at least addition, subtraction,
multiplication and division. Additional operations could be added to
that, e.g. a fused multiply and add, or a square root, or even some
trigonometric functions or anything else you might find in the Math
object. But I'd say that these additional methods shoul only be
available if the hardware supports the operations in question. That way
the burden for the ES implementation is low, since it doesn't have to
implement a bunch of correctly-rounding special functions, and the
application can make informed decisions about how to choose between
different possible computations in order to maximize efficiency.
If you want to see a code example, something like this should become
possible for applications:
const down = Math.directedRounding("-Infinity");
const up = Math.directedRounding("+Infinity");
function interval(lo, hi) { this.lo = lo; this.hi = hi; }
interval.prototype.add = function(that) {
return new interval(down.add(this.lo, that.lo),
up.add(this.hi, that.hi));
}
It would be nice to have similar functionality for floating-point SIMD
types as well. Not sure whether these should be methods of the SIMD
constructor or of the directed rounding objects envisioned above.
I don't know if any of this has been suggested before, but if it has, I
couldn't find it. If there is some interest in following this up, I'll
be glad to help in whatever way I can to get this properly specified.
I'd welcome any useful pointers though, as I haven't followed the ES
specification process closely, and haven't contributed to it at all.
Greetings,
Martin von Gagern
More information about the es-discuss
mailing list