chicken-hackers
[Top][All Lists]
Advanced

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

Re: [Chicken-hackers] Bug in number->string


From: Thomas Bushnell BSG
Subject: Re: [Chicken-hackers] Bug in number->string
Date: Sat, 16 Jan 2010 11:43:15 -0800

It's documented that number->string is not correct with floating point
numbers, though not this bad.  

The problem is that the conversion code in runtime.c uses the printf
format codes %o and %x for bases 8 and 16, which (unlike %d) take a C
unsigned integer.  This should be easy to fix.

For base 2, the to_binary function is simply incorrect, also assuming
the argument is unsigned.  Again, should be easy to fix.

string->number uses strtol for integer conversion, which deals with
signs appropriately, so (string->number -1 2) and friends does just
fine.

I have intended for a while to write correct r5rs implementations of the
numeric conversion routines, but haven't gotten to it.  Perhaps I
should.

Thomas


On Sat, 2010-01-16 at 07:03 -0600, Peter Danenberg wrote:
> There's appears to be a bug in the way number->string handles
> negatives in binary, octal and hexadecimal mode; for instance:
> 
>   (string->number (number->string -1 2) 2) => 1.84467440737096e+19
> 
> According to r5rs 6.2.6[1], the following should hold true:
> 
>   (let ((number number)
>         (radix radix))
>     (eqv? number
>           (string->number (number->string number
>                                           radix)
>                           radix)))
> 
> however:
> 
>   (eqv? -1 (string->number (number->string -1 2) 2)) => #f
>   (eqv? -1 (string->number (number->string -1 8) 8)) => #f
>   (eqv? -1 (string->number (number->string -1 10) 10)) => #t
>   (eqv? -1 (string->number (number->string -1 16) 16)) => #f
> 
> Footnotes: 
> [1]  
> http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_idx_350
> 
> 
> 
> _______________________________________________
> Chicken-hackers mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/chicken-hackers






reply via email to

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