# [R6RS] Draft of arithmetic SRFI

William D Clinger will
Wed Aug 3 13:50:43 EDT 2005

```Mike Sperber wrote:
> > One of the main rationales for the fixnum primitives is as a basis
> > for implementing the full numeric tower.  When implementing bignums,
> > I think it's easier to deal with arithmetic modulo hi-lo+1 than to
> > have to guard against overflow exceptions.
>
> I actually don't think that's true.  At least the reference
> implementation takes great care never to wrap.  One place where it
> really hurts is with generic arithmetic: When adding two fixnums, I
> basically have to always convert to bignums and then coerce back if I
> don't have an overflow signal---that's expensive.

Your code for integer addition basically looks like this:

(define (integer+ a b)
(bignum+ (integer->bignum a)
(integer->bignum b)))

That code can be bummed as follows:

(define (integer+ a b)
(if (and (fixnum? a) (fixnum? b))
(let ((c (fx+ a b)))
(cond ((not (negative? a))
(if (fx<= b c)
c
(bignum+ (integer->bignum a) (integer->bignum b))))
((negative? a)
(if (fx< c b)
c
(bignum+ (integer->bignum a) (integer->bignum c))))
(else c)))
(bignum+ (integer->bignum a) (integer->bignum b))))

The bummed code would not work if fx+ were to signal an error on
overflow.  With the error-signalling semantics, it would be hard
to do better than your original code, which I think is just too
slow.

Matthew Flatt wrote:
> I'd prefer that all "unspecified"s be resolved.

I hope you were talking only about the record SRFI, Matthew, and
were not stating a general philosophy that you'd like to apply to
the entire R6RS.  If you were stating a general philosophy, then
I need to explain a few facts of life.

Will
```