[comp.lang.smalltalk] STV bug in printFraction:

trr@rayssd.ray.com (Terry R. Raymond) (04/28/88)

I discovered a bug in the STV printFraction method.  Notice the results of the
following two expressions.  The negative result is in error.

-14661/6250 printFraction: 3   '-3.654'
14661/6250 printFraction: 3    '2.345'

The following is the method with a correction.

printFraction: numberFractionDigits
        "Answer a string, the ASCII representation
         of the receiver truncated to numberFractionDigits
         decimal places."
   | stream fraction integer num |
    numberFractionDigits < 0
        ifTrue: [self error: 'Negative digit count'].
    stream := WriteStream on: (String new: 16).
    num := self.
    self < 0
        ifTrue: [ num := self negated.
                  stream nextPut: $- ].
    (integer := num // 1) printOn: stream.
    stream nextPut: $..
    fraction := num - integer.
    integer := 0.
    numberFractionDigits timesRepeat: [
        fraction := (fraction - integer) * 10.
        (integer := fraction // 1) printOn: stream].
    ^stream contents
-- 
Terry Raymond
Raytheon Submarine Signal Division; Portsmouth RI; (401)-847-8000 x5597
smart mailer or arpanet: trr@rayssd.ray.com
old dumb mailer or uucp: {cbosgd,gatech,ihnp4,linus!raybed2} !rayssd!trr