Mantis - Squeak
Viewing Issue Advanced Details
3493 Kernel minor always 04-21-06 22:48 01-08-13 21:44
nicolas cellier  
 
normal  
new 3.9  
open  
none    
none  
0003493: printing a float in base 16 is broken sometimes
See this example:

    "this should be the maximum floating point value in IEEE 754 double precision"
    (Number readFrom: ((String new: 100) writeStream
        nextPutAll: '2r1.';
        next: 52 put: $1;
        nextPutAll: 'e1023';
        contents)) printStringBase: 16.

"will print as 16r1.0e255"

    (Number readFrom: ((String new: 100) writeStream
        nextPutAll: '2r1.';
        next: 52 put: $1;
        nextPutAll: 'e1020';
        contents)) printStringBase: 16.

"will print as 16r2.0e255, twice the first number when it should be 1/8..."

Well, in base (2 raisedTo: Integer anyPositive), printString should be exact...
First number is exactly
    16rF.FFFFFFFFFFFF8e255
and last is exactly
    16r1.FFFFFFFFFFFFFe255
if exponent is written in base 10,
or same mantissa with exponent eFF in base 16...
Note that absPrintExactlyOn:base: will give correct answer.

Rounding should better not be used, because printed number would answer Infinity when read back... But if it where used correctly, then the answers would be:
    16r1.0e256
and
    16r2.0e255

related to 0006696assigned andreas ScaledDecimal can read in base 16 but cannot print in base 16 
related to 0006695closed andreas Float storeStringBase: does not store the radix 
 Kernel-Numbers-PrintFloatBase16Patch.1.cs [^] (3,302 bytes) 04-22-06 00:07
 FloatPrintBase16-M3493-Test.1.cs [^] (1,691 bytes) 09-24-07 23:04
 FloatPrintStringBase2-M3493-Test.1.cs [^] (886 bytes) 09-25-07 01:04
 FloatPrintStringBase2-M3493-Test.2.cs [^] (882 bytes) 08-31-08 00:07

Notes
(0004768)
nicolas cellier   
04-22-06 00:05   
I debugIt, and it is in Float>>absPrintOn:base:

Of course it is Infinity that is striking silently...
As usual, I would prefer an exception and a debugger opened to a silent bug...
Well, there is a protection exp=1023 ifTrue: [...], but 1 line too late...

So i propose a patch, hoping it does not break anything else...
But unfortunately it does not deal with this second bug (not mine):

    (Number readFrom: ((String new: 100) writeStream
        nextPutAll: '2r1.';
        next: 52 put: $1;
        nextPutAll: 'e1023';
        contents)) printStringBase: 2.

    "will answer 2r2.0e1024"

Ouch! tricky tc1 and tc2 logical conditions...
Correct answer would be 2r1.0e1024 if rounding is allowed (but then result cannot be read back, it would overflow and answer Infinity).
(0004772)
ducasse   
04-22-06 13:38   
Could we have some tests :)
(0012553)
nicolas cellier   
08-31-08 00:07   
FloatPrintStringBase2-M3493-Test.1.cs is incorrect.
use FloatPrintStringBase2-M3493-Test.2.cs instead.
(0014290)
nicolas cellier   
01-08-13 21:44   
In squeak 4.4, printStringBase: is correct, but the bad behaviour can still be triggered with these snippets:
 
String streamContents: [:str | (Number readFrom: ((String new: 100) writeStream
        nextPutAll: '2r1.';
        next: 52 put: $1;
        nextPutAll: 'e1023';
        contents)) absPrintOn: str base: 16].

String streamContents: [:str | (Number readFrom: ((String new: 100) writeStream
        nextPutAll: '2r1.';
        next: 52 put: $1;
        nextPutAll: 'e1023';
        contents)) absPrintOn: str base: 16].

String streamContents: [:str | (Number readFrom: ((String new: 100) writeStream
        nextPutAll: '2r1.';
        next: 52 put: $1;
        nextPutAll: 'e1023';
        contents)) absPrintOn: str base: 2].