Karl Ramberg uploaded a new version of Kernel to project Etoys:
http://source.squeak.org/etoys/Kernel-kfr.8.mcz ==================== Summary ==================== Name: Kernel-kfr.8 Author: kfr Time: 5 December 2012, 5:07:26 pm UUID: bb9aaf47-e754-a74d-8e68-1dd799000714 Ancestors: Kernel-bf.7 Fix long standing bug with printing small numbers ie 1.2245678e-16 Copied methods from Squeak 4.4 =============== Diff against Kernel-bf.7 =============== 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." - "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. If the nature of the receiver is such that e-notation should be used, that is done." + ^String new: placesDesired + 10 streamContents: [:aStream | + self printOn: aStream showingDecimalPlaces: placesDesired] - | precision rounded frac sign integerString fractionString result aString aFloat decPt between myAbs | - self isNaN ifTrue: [^ 'NaN']. - aString := (aFloat := self asFloat) printString. - aFloat isInfinite ifTrue: [^ aFloat printString]. - myAbs := aFloat abs. - - (aString indexOf: $e ifAbsent: [nil]) ifNotNilDo: - [:ePosition | ((myAbs < 1.0e-15) or: [myAbs > 1.0e15]) ifTrue: - [decPt := aString indexOf: $. ifAbsent: [^ aString]. - between := aString copyFrom: (decPt + 1) to: (ePosition - 1). - ^ String streamContents: [:aStream | - aStream nextPutAll: (aString copyFrom: 1 to: decPt). - aStream nextPutAll: between. - aStream nextPutAll: (aString copyFrom: ePosition to: aString size)]]]. - - "The remainder of this method is courtesy of Frank Sergeant, Dec/06" - placesDesired <= 0 ifTrue: [^ self rounded printString]. - precision _ Utilities floatPrecisionForDecimalPlaces: placesDesired. - rounded _ self roundTo: precision. - sign := rounded negative ifTrue: ['-'] ifFalse: ['']. - integerString := rounded abs integerPart asInteger printString. - frac := ((rounded abs fractionPart roundTo: precision) * (10 raisedToInteger: placesDesired)) asInteger. - 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 -0.98 printShowingDecimalPlaces: 2 2.567 printShowingDecimalPlaces: 2 -2.567 printShowingDecimalPlaces: 2 0 printShowingDecimalPlaces: 2 - Float infinity printShowingDecimalPlaces: 5 - 2345.67890123 printShowingDecimalPlaces: 5 - 23456789.0012345 printShowingDecimalPlaces: 3 "! _______________________________________________ etoys-dev mailing list [hidden email] http://lists.squeakland.org/mailman/listinfo/etoys-dev |
Free forum by Nabble | Edit this page |