[squeak-dev] The Trunk: Kernel-nice.247.mcz
commits at source.squeak.org
commits at source.squeak.org
Sat Sep 19 20:22:36 UTC 2009
Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.247.mcz
==================== Summary ====================
Name: Kernel-nice.247
Author: nice
Time: 19 September 2009, 8:21:42 am
UUID: 7773d01a-b08f-ea41-9e0e-ab68531c4829
Ancestors: Kernel-nice.246
Fix for #printShowingDecimalPlaces:
The fix comes from http://bugs.squeak.org/view.php?id=5640 with an additional test for printing exceptional values (Inf/NaN).
The implementation relies on exact arithmetic (Fraction/LargeInteger) and thus avoid inexact Float rounding operations.
=============== Diff against Kernel-nice.246 ===============
Item was changed:
----- Method: Number>>printShowingDecimalPlaces: (in category 'printing') -----
printShowingDecimalPlaces: placesDesired
+ "Print the receiver showing precisely the given number of places desired. If placesDesired is positive, a decimal point and that many digits after the decimal point will always be shown. If placesDesired is zero, a whole number will be shown, without a decimal point."
- "Print the receiver showing precisely the given number of places desired. If placesDesired is positive, a decimal point and that many digits after the decimal point will always be shown. If placesDesired is zero, a whole number will be shown, without a decimal point. It now handles negative numbers between 0 and -1 and rounds correctly in more cases. This method probably could be optimized -- improvements welcomed. Category was/is 'converting' but should be 'printing' "
+ | rounder rounded frac sign integerString fractionString result |
- | precision rounded frac sign integerString fractionString result |
placesDesired <= 0 ifTrue: [^ self rounded printString].
+ rounder := 10 raisedToInteger: placesDesired.
+ rounded := self roundTo: rounder reciprocal.
- precision := Utilities floatPrecisionForDecimalPlaces: placesDesired.
- rounded := self roundTo: precision.
sign := rounded negative ifTrue: ['-'] ifFalse: [''].
+ integerString := rounded abs integerPart truncated printString.
+ frac := ((rounded abs fractionPart) * rounder) truncated.
+ fractionString := frac printString padded: #left to: placesDesired with: $0.
- integerString := rounded abs integerPart asInteger printString.
- frac := ((rounded abs fractionPart roundTo: precision) * (10 raisedToInteger: placesDesired)) asInteger.
- fractionString := frac printString padded: #right to: placesDesired with: $0.
result := sign , integerString , '.' , fractionString.
^result
"
23 printShowingDecimalPlaces: 2
23.5698 printShowingDecimalPlaces: 2
-234.567 printShowingDecimalPlaces: 5
23.4567 printShowingDecimalPlaces: 0
23.5567 printShowingDecimalPlaces: 0
-23.4567 printShowingDecimalPlaces: 0
-23.5567 printShowingDecimalPlaces: 0
100000000 printShowingDecimalPlaces: 1
+ 0.98 printShowingDecimalPlaces: 5
- 0.98 printShowingDecimalPlaces: 2
-0.98 printShowingDecimalPlaces: 2
2.567 printShowingDecimalPlaces: 2
-2.567 printShowingDecimalPlaces: 2
0 printShowingDecimalPlaces: 2
- Number categoryForSelector: #printShowingDecimalPlaces:
"!
Item was added:
+ ----- Method: Float>>printShowingDecimalPlaces: (in category 'printing') -----
+ printShowingDecimalPlaces: placesDesired
+ "This implementation avoids any rounding error caused by rounded or roundTo:"
+
+ self isFinite ifFalse: [^self printString].
+ ^self asTrueFraction printShowingDecimalPlaces: placesDesired!
More information about the Squeak-dev
mailing list
|