[Pkg] The Trunk: Kernel-nice.560.mcz
commits at source.squeak.org
commits at source.squeak.org
Tue Mar 29 20:02:20 UTC 2011
Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.560.mcz
==================== Summary ====================
Name: Kernel-nice.560
Author: nice
Time: 29 March 2011, 10:01:57.74 pm
UUID: 0115aa6d-4357-864e-9984-a1a9a3a2c062
Ancestors: Kernel-ul.559
Provides faster printShowingDecimalPlaces:
Also accelerate ScaledDecimal printing a bit more.
=============== Diff against Kernel-ul.559 ===============
Item was added:
+ ----- Method: Fraction>>printOn:showingDecimalPlaces: (in category 'printing') -----
+ printOn: aStream showingDecimalPlaces: placesDesired
+ "Same as super, but provides a faster implementation by inlining some Fraction protocol thus avoiding intermediate Fraction creation."
+
+ | roundedFractionPart integerPart scaling |
+ placesDesired <= 0
+ ifTrue: [self rounded printOn: aStream]
+ ifFalse:
+ [scaling := 10 raisedToInteger: placesDesired.
+ integerPart := numerator abs quo: denominator.
+ roundedFractionPart := (numerator abs - (integerPart * denominator)) * scaling * 2 + denominator quo: denominator * 2.
+ roundedFractionPart = scaling
+ ifTrue:
+ [integerPart := integerPart + numerator sign.
+ roundedFractionPart := 0].
+ numerator negative ifTrue: [aStream nextPut: $-].
+ integerPart printOn: aStream.
+ aStream nextPut: $..
+ roundedFractionPart printOn: aStream base: 10 length: placesDesired padded: true].!
Item was added:
+ ----- Method: Fraction>>printTruncatedOn:showingDecimalPlaces: (in category 'printing') -----
+ printTruncatedOn: aStream showingDecimalPlaces: placesDesired
+ "Print a representation of the receiver on aStream in decimal notation with prescribed number of places after decimal separator.
+ Print as if the receiver was truncated to requested precision."
+
+ | truncatedFractionPart integerPart scaling |
+ placesDesired <= 0
+ ifTrue: [self truncated printOn: aStream]
+ ifFalse:
+ [scaling := 10 raisedToInteger: placesDesired.
+ integerPart := numerator abs quo: denominator.
+ truncatedFractionPart := (numerator abs - (integerPart * denominator)) * scaling quo: denominator.
+ numerator negative ifTrue: [aStream nextPut: $-].
+ integerPart printOn: aStream.
+ aStream nextPut: $..
+ truncatedFractionPart printOn: aStream base: 10 length: placesDesired padded: true].!
Item was added:
+ ----- Method: Integer>>printOn:showingDecimalPlaces: (in category 'printing') -----
+ printOn: aStream showingDecimalPlaces: placesDesired
+ "Same as super, but provides a faster implementation because fraction part and rounding are trivial."
+
+ self printOn: aStream base: 10.
+ placesDesired <= 0
+ ifFalse:
+ [aStream nextPut: $..
+ 0 printOn: aStream base: 10 length: placesDesired padded: true].!
Item was added:
+ ----- Method: Number>>printOn:showingDecimalPlaces: (in category 'printing') -----
+ printOn: aStream showingDecimalPlaces: placesDesired
+ "Print a representation of the receiver on aStream in decimal notation with prescribed number of places after decimal separator."
+
+ | rounder rounded roundedFractionPart |
+ placesDesired <= 0 ifTrue: [^ self rounded printOn: aStream].
+ rounder := 10 raisedToInteger: placesDesired.
+ rounded := self roundTo: rounder reciprocal.
+ rounded negative ifTrue: [aStream nextPut: $-].
+ rounded := rounded abs.
+ rounded integerPart truncated printOn: aStream.
+ aStream nextPut: $..
+ roundedFractionPart := (rounded fractionPart * rounder) truncated.
+ roundedFractionPart printOn: aStream base: 10 length: placesDesired padded: true!
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."
+ ^String new: placesDesired + 10 streamContents: [:aStream |
+ self printOn: aStream showingDecimalPlaces: placesDesired]
- | rounder rounded frac sign integerString fractionString result |
- placesDesired <= 0 ifTrue: [^ self rounded printString].
- rounder := 10 raisedToInteger: placesDesired.
- rounded := self roundTo: rounder reciprocal.
- 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.
- 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
2.567 printShowingDecimalPlaces: 2
-2.567 printShowingDecimalPlaces: 2
0 printShowingDecimalPlaces: 2
"!
Item was changed:
----- Method: ScaledDecimal>>printFractionAsDecimalOn: (in category 'printing') -----
printFractionAsDecimalOn: stream
+ "Please note: this is different from printOn:showingDecimalPlaces: because it prints truncated."
+ fraction printTruncatedOn: stream showingDecimalPlaces: scale!
- | aFraction integerPart fractionPart |
- fraction negative
- ifFalse: [ aFraction := fraction ]
- ifTrue: [
- aFraction := fraction negated.
- stream nextPut: $- ].
- integerPart := aFraction truncated.
- integerPart printOn: stream.
- scale = 0 ifTrue: [ ^self ].
- stream nextPut: $..
- fractionPart := ((aFraction - integerPart) * (10 raisedToInteger: scale)) truncated.
- fractionPart
- printOn: stream
- base: 10
- length: scale
- padded: true!
Item was added:
+ ----- Method: ScaledDecimal>>printOn:showingDecimalPlaces: (in category 'printing') -----
+ printOn: aStream showingDecimalPlaces: placesDesired
+ "Same as super, except the fraction knows better how to do it."
+
+ fraction printOn: aStream showingDecimalPlaces: placesDesired!
More information about the Packages
mailing list