Racket: gcd bug in BC

Created on 6 Nov 2020  Â·  7Comments  Â·  Source: racket/racket

What version of Racket are you using?
7.9 [bc]

What program did you run?

#lang racket/base 
;; RacketBC returns a negative number, contrary to the documentation. 
;; RacketCS correctly returns a positive number. 
(define num -4611686018427387904)
(gcd num num)
(gcd num 0)

Please include any other relevant details

This bug brought to you by Xsmith.

bug racket-bc

Most helpful comment

I tried to figure out how old this bug is, but I was only able to find source as far back as v103 (September 6, 2000) — so, at least 20 years old.

All 7 comments

Great fixnum-boundary catch.

I tried to figure out how old this bug is, but I was only able to find source as far back as v103 (September 6, 2000) — so, at least 20 years old.

It looks like it was there in the original import of the runtime into CVS: https://github.com/racket/old-plt/commit/3a0f0320d6b793ce1e5bf05aff04f77f67a0141c

I found the original libscheme source; looks like it was there too: https://github.com/iosdevzone/libscheme/blob/master/libscheme-0.5/scheme_number.c#L595

I don't think the original libscheme implementation supported bignums or handled any kind of fixnum overflow, so it was more of a general limitation than a specific gcd bug there.

Version 53 seems to give the right answer, surprisingly:

[samth@huor:~/.../users/samth/PLT racket-7.4] wine MzScheme.exe 
Welcome to MzScheme version 53, Copyright (c) 1995-98 PLT (Matthew Flatt)
> (define num -4611686018427387904)
> (gcd num num)
4611686018427387904
> (gcd num 0)
4611686018427387904
> (exit)

You want to try the most negative fixnum, which is different on a 32-bit build.But I still got the right result from v53 when I tried that yesterday.

Was this page helpful?
0 / 5 - 0 ratings