Scheme numbers

TorgoX on 2004-09-04T23:52:40

Dear Log,

Scheme's implementation of numbers is rich like a Romanov! Among its bric-à-Brak, there is the notion of exactness:

«It is necessary, however, to distinguish between numbers that are represented exactly and those that may not be. For example, indexes into data structures must be known exactly, as must some polynomial coeffcients in a symbolic algebra system. On the other hand, the results of measurements are inherently inexact, and irrational numbers may be approximated by rational and therefore inexact approximations. In order to catch uses of inexact numbers where exact numbers are required, Scheme explicitly distinguishes exact from inexact numbers. This distinction is orthogonal to the dimension of type.

6.2.2. Exactness
Scheme numbers are either exact or inexact. A number is exact if it was written as an exact constant or was derived from exact numbers using only exact operations. A number is inexact if it was written as an inexact constant, if it was derived using inexact ingredients, or if it was derived using inexact operations. Thus inexactness is a contagious property of a number.

If two implementations produce exact results for a computation that did not involve inexact intermediate results, the two ultimate results will be mathematically equivalent. This is generally not true of computations involving inexact numbers since approximate methods such as floating point arithmetic may be used, but it is the duty of each implementation to make the result as close as practical to the mathematically ideal result.

Rational operations such as + should always produce exact results when given exact arguments. If the operation is unable to produce an exact result, then it may either report the violation of an implementation restriction or it may silently coerce its result to an inexact value. See section 6.2.3.

With the exception of inexact->exact, the operations described in this section must generally return inexact results when given any inexact arguments. An operation may, however, return an exact result if it can prove that the value of the result is unaffected by the inexactness of its arguments. For example, multiplication of any number by an exact zero may produce an exact zero result, even if the other argument is inexact.»

It's an interesting idea. But I can't say I've ever run into a situation where that's what I needed.

In fact, numbers are the place where Scheme seems to stop being minimalist. ("Convenience for the rich, virtue for the poor?")


I R ENGINEER! TOOT TOOT

schwern on 2004-09-05T02:59:12

Sounds like it might vaguely be useful when calculating with sigfigs.

Scheme numbers

jordan on 2004-09-07T16:06:44

  • It's an interesting idea. But I can't say I've ever run into a situation where that's what I needed.

The people who made the Scheme standards are interested in things like Denotational Semantics and proving program correctness.

It's a lot easier to make rigorous statements about a language if you can be guaranteed that transformations like division and multiplication are truly inverse operations. You want to know that (n/3)*3 = n and not (n + .0000000000001476).

I think that some computer science types also believe that exactness in the language can lead to helpful program optimizations, but I'm not entirely on firm ground here (like I'm not on firm ground in this whole comment, but that never stopped me from posting before!).