Bug 50751

Critical

GemStone/S 64 Bit

3.7, 3.6.6, 3.6.5, 3.6.4, 3.6.3, 3.6.2, 3.6.1, 3.6, 3.5.8, 3.5.7, 3.5.6, 3.5.5, 3.5.4, 3.5.3, 3.5.2, 3.5.1, 3.5, 3.4.x, 3.3.x, 3.2.x

3.7.1, 3.6.7

Strings with leading '-0.' lose sign when converted using asNumber or Number fromString/Stream:

Converting a string with a decimal number that is less than zero and greater than -1, such as '-0.1', using an unspecific conversion asNumber or Number fromString: or fromStream:, results in a positive value, the absolute value of the correct result.

This applies to CharacterCollection >> asNumber, Number class >> fromString:, and Number class >> fromStream:.

This does not affect asFloat, nor BinaryFloat>> fromStream:/String:.

Workaround

The patch below fixes issues with asNumber, but not Number class methods.

category: 'Converting'
method: CharacterCollection
asNumber

"patch for bug 50751"
"Returns the receiver converted to a kind of number.  If the receiver contains
 all digits (with optional radix notation), returns a kind of Integer.  If the
 receiver has a slash, returns a Fraction.  Otherwise conversion to a Float is
 attempted.  An error may result if the receiver does not contain the proper
 format for a kind of Number."

 | str strm sign res |
 str := self trimWhiteSpace.
 str size == 0 ifTrue: [ ^0 ].
 (str at: 1) == $- ifTrue:[
   strm := ReadStreamPortable on: (str copyFrom: 2 to: str size).
   sign := -1 .
 ] ifFalse:[
   strm := ReadStreamPortable on: str .
 ].
 (self indexOf: $/ startingAt: 1) ~~ 0 ifTrue:[
    res := Fraction fromStream: strm .
 ] ifFalse:[
   res := Number fromStream: strm .
 ].
 sign ifNotNil:[ res := res * sign ].
 ^ res
%

Last updated: 11/2/23