[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: (= 4 4.0)



Since you say you cannot coerce to exact integers if bignums are not
available, it seems likely that you are using only two numeric
representations, fixnum and flonum, and your floating point precision
is such that all exact integers are representable as inexact integers.
I think this is a perfectly reasonable set of design choices for the
purposes of some implementations, and it isn't very difficult to make
such a system conform to the ANSI Scheme standard.

Presumably QUOTIENT et al are already checking to ensure that they are
being passed exact integers, so performance is not an issue.  The issue
must be the hassle of writing extra code.

I would recommend the following approach, which will probably take you
about half an hour to implement.  Write a procedure that takes an
integer x (whether exact or inexact) and returns (INEXACT->EXACT x)
if x is representable as an exact integer but reports a violation of
an implementation restriction is x is not representable as an exact
integer.  In your system you can probably write this procedure as

(define (integer->exact x)
  (if (exact? x)
      x
      (let ((y (inexact->exact x)))
        (if (= x (exact->inexact y))
            y
            (error "Violation of implementation restriction 247" x)))))

Rewrite the non-exact case for QUOTIENT et al to call INTEGER->EXACT
and retry the operation, converting the result to inexact.  For example,

    (define (quotient m n)
      (if (and (exact? m) (exact? n))
          ...
          (error "Violation of implementation restriction 1094" x y)))

becomes

    (define (quotient m n)
      (if (and (exact? m) (exact? n))
          ...
          (exact->inexact
           (quotient (integer->exact m) (integer->exact n)))))

I don't think the half hour or so that it takes to do this for a
dozen or so procedures is too high a price to pay for the benefit
of having a real Scheme system that can run programs whose inexact
integers are restricted to the range of your exact integers.

I would think that the ANSI Scheme specification for NUMBER->STRING
and STRING->NUMBER would be a greater hassle than this, but I don't
think that specification creates an undue burden either.

William Clinger

PS: The mixed exactness case is similar for the arithmetic predicates
such as = and <, but if every exact number is representable as an
inexact number then you can use the same trick except you convert
to inexact before retrying the predicate.  You probably are doing
this already, in fact, which makes me wonder why you think it is
such a hassle to support inexact integers.