Subject: Re: Scheme and Rational Numbers
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 2 Feb 2001 13:50:38 GMT
Newsgroups: comp.lang.scheme
Message-ID: <95edve$158f2$1@fido.engr.sgi.com>
David Rush  <kumo@bellsouth.net> wrote:
+---------------
| As far as I know, Scheme is the *only* computer language that has taken
| this bull by its horns and developed a fairly sensible approach to it.
+---------------

One of the few, perhaps, but certainly not the "only". Common Lisp's
numeric class/type hierarchy treats rationals much like Scheme's numeric
tower, and in fact the ANSI standard explores the subtleties somewhat
more than the Scheme report(s).

Where Common Lisp & Scheme mainly differ is that Scheme's numeric types
form a strict "tower" with a (nearly) orthogonal notion of "exactness",
whereas Common Lisp has a somewhat richer tree-structured hierarchy of
numeric types with some kinds of numbers being disjoint types, rather than
sub-/super-types of each other, and exactness (or not) being defined in rules
for arithmetic of and on various types. See <URL:http://www.xanalys.com/
software_tools/reference/HyperSpec/Body/chap-12.html> "Numbers". I didn't
see a single overall type/class diagram anywhere, but the following
(extracted from various sections) implicitly defines the tree:

	- The type number contains objects which represent mathematical
	  numbers. The types real and complex are disjoint subtypes of number. 
	- The type real includes all numbers that represent mathematical
	  real numbers, though there are mathematical real numbers (e.g.,
	  irrational numbers) that do not have an exact representation in
	  Common Lisp.
	- The types rational and float are disjoint subtypes of type real.
	- The types integer and ratio are disjoint subtypes of type rational.
	  The canonical representation of a rational is as an integer if its
	  value is integral, and otherwise as a ratio. 
	- The types fixnum and bignum form an exhaustive partition of type
	  integer. 
	- A fixnum is an integer whose value is between most-negative-fixnum
	  and most-positive-fixnum inclusive. The type fixnum is required
	  to be a supertype of (signed-byte 16). 
	- A ratio is a number representing the mathematical ratio of two
	  non-zero integers, the numerator and denominator, whose greatest
	  common divisor is one, and of which the denominator is positive
	  and greater than one. 
	- A float is a mathematical rational (but not a Common Lisp rational)
	  of the form s*f*b^e-p, where [details omitted]...
	- The types short-float, single-float, double-float, and long-float
	  are subtypes of type float. Any two of them must be either disjoint
	  types or the same type; if the same type, then any other types
	  between them in the above ordering must also be the same type.
	- The type complex includes all mathematical complex numbers other
	  than those included in the type rational. Complexes are expressed
	  in Cartesian form with a real part and an imaginary part, each of
	  which is a real. The real part and imaginary part are either both
	  rational or both of the same float type. The imaginary part can be
	  a float zero, but can never be a rational zero, for such a number
	  is always represented by Common Lisp as a rational rather than a
	  complex.

Common Lisp also has the types signed-byte, unsigned-byte, and bit, with
the type hierarchy for the latter being:

	bit, unsigned-byte, signed-byte, integer, rational, real, number, t

It also has interval types [such as "(integer 0 1)", which is how "bit"
is actually defined].


-Rob

-----
Rob Warnock, 31-2-510		rpw3@sgi.com
SGI Network Engineering		http://reality.sgi.com/rpw3/
1600 Amphitheatre Pkwy.		Phone: 650-933-1673
Mountain View, CA  94043	PP-ASEL-IA