Re: [q-lang-users] Query about Type Tests (Complex and Rational).
Brought to you by:
agraef
From: John C. <co...@cc...> - 2006-06-15 14:32:47
|
Rob Hubbard scripsit: > I think I need clarification on > (1) When one type should be a subtype of another. > (2) What the semantics of type test function (such as iscomplex) is > supposed to be. Well, FWIW I'll tell you how Scheme does it. (For Schemers, I'm oversimplifying some and blurring some distinctions between R5RS and R6RS-draft.) In Scheme, the predicates "number?", "complex?", "real?", "rational?", and "integer?" refer to the mathematical concepts, not the representations. Thus on "normal" implementations, "number?" and "complex?" are the same thing, and "integer?" returns true for any integral value, even if the underlying representation is floating-point. "Real?" and "rational?" are not quite the same, because the former conventionally includes IEEE infinities whereas the latter does not. Cutting across this hierarchy or "tower", there are also the predicates "exact?" and "inexact?". On "normal" implementations, standard machine floating-point representations are inexact; machine integers ("fixnums"), arbitrary-precision integers ("bignums"), and pairs whose numerator and denominator are integers ("ratnums") are exact. Note that a complex number is only real if its imaginary part, expressed or implied, is an *exact* 0, because 0.0 actually represents every number between the smallest representable positive float and its negation. So 1 is exact and integer (and therefore also rational, real, and complex); 1.0 is inexact and integer (and rational and real and complex); 1.1 is inexact and rational (and real and complex); 11/10 is exact and rational (and real and complex); 1+2i is exact and complex; 1.0+0i is inexact and real (and complex); 1.0+0.0i is inexact and complex. Though not part of the standard, one can write some predicates that enquire about representations, assuming that the shortest representation is always used. It's impossible to write "fixnum?" in portable Scheme, because it depends on the size of machine integers. The others can be written for "normal" Scheme implementations as follows (not Scheme syntax): bignum? = integer? and exact? and not fixnum? ratnum? = rational? and exact? and not integer? compnum? = complex? and not real? Some Schemes use different representations for exact and inexact compnums, where the former is a pair of pointers and the latter is a pair of unboxed floats. I hope this is helpful in clarifying everyone's thinking. Feel free to ask questions about unclarities. -- And through this revolting graveyard of the universe the muffled, maddening beating of drums, and thin, monotonous whine of blasphemous flutes from inconceivable, unlighted chambers beyond Time; the detestable pounding and piping whereunto dance slowly, awkwardly, and absurdly the gigantic tenebrous ultimate gods -- the blind, voiceless, mindless gargoyles whose soul is Nyarlathotep. (Lovecraft) John Cowan|co...@cc...|ccil.org/~cowan |