chicken-users
[Top][All Lists]
Advanced

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

[Chicken-users] number->string bug


From: Kevin Beranek
Subject: [Chicken-users] number->string bug
Date: Tue, 6 Jan 2009 21:20:32 -0600
User-agent: Mutt/1.5.17+20080114 (2008-01-14)

I've stumbled upon some interesting behavior of number->string.  It
appears that there was previously a ticket filed for a similar bug in
version 2.6 (http://trac.callcc.org/ticket/160).  What I've been
observing doesn't seem to be exactly the same but it is perhaps related
because I've only noticed it with base 2 as was the case with the
previous bug.

Below is a test case followed by some sample output.  One interesting
thing to note about the output is that (number->string 99 2) produced
two different values (2 and 75).  Also of note is that I've tried
various constructions of the following code with let statements at
different levels, using let vs. set! in different places and different
methods of looping but the bug remains in every combination I've tried.
Behavior is also unchanged between csi and csc.

Just to be clear about what the code below is intended to do, it is
randomly constructing values in a given range, converting them to a
binary string via number->string, determining if the string is valid by
checking that all characters in the string are either #\0 or #\1 and if
not it prints the offending value along with the generated string.

CODE:

(let ((i 0) (val 0) (binary-val 0) (invalid #f) (j 0) (c 0))

  (do ((i 0 (+ i 1))) ((= i 5000))
    (set! val (random 100))
    (set! binary-val (number->string val 2))
    (set! invalid #f) 

    (do ((j 0 (+ j 1))) ((= j (string-length binary-val)))
      (set! c (string-ref binary-val j))
      (if (not (or (char=? c #\0) (char=? c #\1)))
        (set! invalid #t)))
    
    (if invalid
      (printf "(number->string ~a 2) = ~a\n" val binary-val))))

OUTPUT:

(number->string 99 2) = 2
(number->string 51 2) = 99
(number->string 8 2) = 51
(number->string 30 2) = 8
(number->string 2 2) = 30
(number->string 75 2) = 2
(number->string 99 2) = 75


-- 
Kevin Beranek




reply via email to

[Prev in Thread] Current Thread [Next in Thread]