Number>>printOn:base:.

ohshima at is.titech.ac.jp ohshima at is.titech.ac.jp
Fri Nov 6 18:45:24 UTC 1998


  Hi.

  I found the results of
{SmallInteger,LargeNegativeInteger}>>printOn:base: are
different from the Smalltalk's standard number format, when
the base is not equal to ten and the value is negative.
(Precisely, the "result" is stored in the first argument.)

  I think the proposition

  aNumber = (aNumber printStringBase: n) asNumber

should be true, but the evaluation of right hand side
results in an error ('invalid radix').

  Fixing 'printOn:base:' is a bit difficult without
modifying the protocol.  For convinient. I think
'Number class>>readFrom' should be as follows:

--------------------
readFrom: stringOrStream 
	"Answer a number as described on aStream.  The number may
	include a leading radix specification, as in 16rFADE"
	| value base aStream sign |
	aStream _ (stringOrStream isMemberOf: String)
		ifTrue: [ReadStream on: stringOrStream]
		ifFalse: [stringOrStream].
	(aStream nextMatchAll: 'NaN') ifTrue: [^ Float nan].
	sign _ (aStream peekFor: $-) ifTrue: [-1] ifFalse: [1].
	(aStream nextMatchAll: 'Infinity') ifTrue: [^ Float infinity * sign].
	base _ 10.
	value _ Integer readFrom: aStream base: base.
	(aStream peekFor: $r)
		ifTrue: 
			["<base>r<integer>"
			(base _ value) < 2 ifTrue: [^self error: 'Invalid radix'].
			sign _ ((sign = -1) xor: (aStream peekFor: $-)) ifTrue: [-1] ifFalse: [1].
			value _ Integer readFrom: aStream base: base].
	^ self readRemainderOf: value from: aStream base: base withSign: sign.
--------------------

                                             OHSHIMA Yoshiki
                Dept. of Mathematical and Computing Sciences
                               Tokyo Institute of Technology 

P.S.
  There is a bug in TestStream>>nextPutAll:.  'string'
should be 'asString'.

P.P.S.
  There must be 

	base = 10 ifFalse: [aStream print: base; nextPut: $r].

in somewhere near the top of 'Float>>absPrintOn:base:' if
'-2r100.01' is acceptable as its result.





More information about the Squeak-dev mailing list