[r6rs-discuss] Why are fx+ and fx* restricted to two arguments
I am posting this as an individual member of the Scheme
community. I am not speaking for the R6RS editors.
bear quoting me, going on about fx+ and fx*:
> >... consider the implementor's
> >responsibility to raise an exception if the
> >result is not expressible as a fixnum, while not
> >raising an exception if it is.
>
> Clarification: these routines do not raise exceptions
> on roundoff errors (ie, when the result is not expressible
> as a fixnum of the same width as the arguments). When you
> say "not expressible" you mean only the case where an
> underflow or overflow error is encountered. Is this
> Correct?
There are no roundoff errors with fixnum arithmetic.
If the arguments are fixnums, they raise an exception
if and only if the mathematically correct result is
not representable as a fixnum, i.e. an underflow or
overflow occurs.
> > (let ((i (least-fixnum))
> > (j (greatest-fixnum)))
> > (list
> > (fx+ i i i i i i i i i i j j j j j j j j j j)
> > (fx* i i i i i i i i i 0 i i i i i i i i i)))
> >
> >would not raise an exception ...
>
> I fear I have misunderstood something. The addition
> expression encounters an overflow error and returns
> an infinity.
No. We are talking about fixnum arithmetic. Fixnums
are a proper subset of the exact integers. There is
no fixnum infinity.
> Does it not raise an exception? The
> multiplication expression, given either associative rule,
> encounters underflow while multiplying the i's. Does
> it not raise an exception before it even attempts to
> multiply by the exact zero?
The specification of fx+ and fx* states:
These procedures return the sum or product of
their arguments, provided that sum or product
is a fixnum. An exception with condition type
&implementation-restriction is raised if that
sum or product is not a fixnum.
That specification makes sense for an arbitrary
number of arguments, and my hypothetical example
showed one consequence of extending their domains
to arbitrary numbers of arguments. According to
the specification quoted, whether an exception is
raised depends solely upon the ultimate sum or
product, irrespective of any intermediate results.
As the rationale observes, this would require
extra precision for intermediate results (although
not as much as is claimed by the rationale; that's
likely to be just a careless claim of mine that
somehow got propagated into the draft report).
One could, of course, rewrite the specification so
that an exception is raised when an intermediate
result overflows, even if the mathematically correct
ultimate result would be a fixnum. That would be
a more complex specification, as it would have to
specify some operational details that the current
wording does not. IMO it would also reduce the
correspondence between + and fx+ and between * and
fx*. Despite those evils, if the Scheme community
insists upon generalizing fx+ and fx* to arbitrary
numbers of arguments, I think it would be better
to change the specification by introducing those
operational details and breaking the correspondence
than by requiring extra precision for intermediate
results.
Will
Received on Mon Feb 19 2007 - 19:42:10 UTC
This archive was generated by hypermail 2.3.0
: Wed Oct 23 2024 - 09:15:01 UTC