--- a/Fraction.st Wed Jul 31 12:24:07 2002 +0200
+++ b/Fraction.st Wed Jul 31 12:24:32 2002 +0200
@@ -14,7 +14,7 @@
Number subclass:#Fraction
instanceVariableNames:'numerator denominator'
- classVariableNames:'FractionOne FractionZero'
+ classVariableNames:'FractionOne FractionZero PrintWholeNumbers'
poolDictionaries:''
category:'Magnitude-Numbers'
!
@@ -39,15 +39,28 @@
"
Instances of Fraction represent fractional numbers consisting of
a numerator and denominator. Both are themselfes arbitrary precision
- integers. Fractions are usually created by dividing Integers using /
- (for exact division).
+ integers.
+ Fractions are usually created by dividing Integers using / (for exact division).
+ Notice, that all operations on fractions reduce their result; this means, that
+ the result of a fraction-operation may return an integer.
+ Aka:
+ (1 / 7) * 7 -> 1 (not 0.99999999...)
Mixed mode arithmetic:
- fraction op fraction -> fraction
+ fraction op fraction -> fraction/integer
fraction op fix -> fix; scale is fix's scale
- fraction op integer -> fraction
+ fraction op integer -> fraction/integer
fraction op float -> float
+
+ [classVariables:]
+ PrintWholeNumbers Booolean experimental:
+ controls how fractions which are greater than 1 are printed.
+ if true, print them as a sum of an integral and the fractional part.
+ (Large ones are easier to read this way)
+ (17/3) printString -> '(5+(2/3))'
+ for now, the default is false, for backward compatibility
+
[author:]
Claus Gittinger
@@ -708,11 +721,31 @@
"append a printed representation of the receiver to the
argument, aStream"
+ |t|
+
+ PrintWholeNumbers == true ifTrue:[
+ "/ experimental: print fractions which are greater than 1 as a sum of
+ "/ an integral and the fractional part. They are easier to read this way.
+ numerator > denominator ifTrue:[
+ aStream nextPut:$(.
+ t := numerator // denominator.
+ t printOn:aStream.
+ aStream nextPutAll:'+('.
+ (numerator - (t*denominator)) printOn:aStream.
+ aStream nextPut:$/.
+ denominator printOn:aStream.
+ aStream nextPutAll:'))'.
+ ^ self
+ ].
+ ].
+
aStream nextPut:$(.
numerator printOn:aStream.
aStream nextPut:$/.
denominator printOn:aStream.
aStream nextPut:$)
+
+ "Modified: / 31.7.2002 / 09:56:41 / cg"
! !
!Fraction methodsFor:'private'!
@@ -861,6 +894,6 @@
!Fraction class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.58 2002-07-18 13:39:47 stefan Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.59 2002-07-31 10:24:32 cg Exp $'
! !
Fraction initialize!