Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in '##fx*?' primitive ? #202

Closed
bsaleil opened this issue Jun 15, 2016 · 2 comments
Closed

Bug in '##fx*?' primitive ? #202

bsaleil opened this issue Jun 15, 2016 · 2 comments

Comments

@bsaleil
Copy link
Contributor

bsaleil commented Jun 15, 2016

gsi trace:

> (##fx* 0 1)
0
> (##fx* 1 0)
0
> (##fx*? 0 1)
0
> (##fx*? 1 0)
Floating point exception

The same behavior appears each time the right operand is 0.

@gambiteer
Copy link
Collaborator

Not a bug. "Don't do that." ##fx* is an internal primitive that assumes that its right-hand argument is not zero.

For example, this code:

(define (times a b)
  (* a b))

is expanded by the compiler to

heine:~/lang/scheme/Arrays/srfi3/Binf-8> gsc -expansion crap
Expansion:

(define times
  (lambda (a b)
    (if ('#<procedure #2 ##eq?> * '#<procedure #3 *>)
        (if (and ('#<procedure #4 ##fixnum?> b)
                 ('#<procedure #4 ##fixnum?> a))
            (if ('#<procedure #5 ##eqv?> b 0)
                0
                (let ((temp.2 (if ('#<procedure #5 ##eqv?> b -1)
                                  ('#<procedure #6 ##fx-?> a)
                                  ('#<procedure #7 ##fx*?> a b))))
                  (if temp.2 temp.2 (* a b))))
            (if (and ('#<procedure #8 ##flonum?> b)
                     ('#<procedure #8 ##flonum?> a))
                ('#<procedure #9 ##fl*> a b)
                (* a b)))
        (* a b))))

@feeley
Copy link
Member

feeley commented Jun 15, 2016

As Brad has pointed out, it is an error to pass 0 as a second argument to ##fx*?. The reason is that it uses division to check if the multiplication overflowed. That is (##fx*? a b) computes r = a*b (modulo the word size), and then checks if r/b=a (if it does there was no overflow).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants