2 Copyright (c) 2008 Vincent Geddes
3 Copyright (c) 2007-2008 Luca Bruno
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the 'Software'), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 DEALINGS IN THE SOFTWARE.
27 numerator: numerator denominator: denominator
28 ^ self basicNew numerator: numerator denominator: denominator!
49 gcd := numerator gcd: denominator.
51 numerator := numerator // gcd.
52 denominator := denominator // gcd.
55 ifTrue: [ ^ numerator ].
56 numerator = denominator
62 aNumber generality > self generality
63 ifTrue: [ ^ (aNumber coerce: self) + aNumber ].
68 numerator: (numerator * aNumber denominator) + (denominator * aNumber numerator)
69 denominator: denominator * aNumber denominator) reduce ]
72 numerator: numerator + (denominator * aNumber)
73 denominator: denominator) reduce ]!
77 aNumber generality > self generality
78 ifTrue: [ ^ (aNumber coerce: self) - aNumber ].
83 numerator: (numerator * aNumber denominator) - (denominator * aNumber numerator)
84 denominator: denominator * aNumber denominator) reduce ]
87 numerator: numerator - (denominator * aNumber)
88 denominator: denominator) reduce ]!
92 aNumber generality > self generality
93 ifTrue: [ ^(aNumber coerce: self) - aNumber ].
98 numerator: numerator * aNumber numerator
99 denominator: denominator * aNumber denominator) reduce ]
102 numerator: numerator * aNumber
103 denominator: denominator) reduce ]!
107 ^ self * aNumber reciprocal!
111 ^ Fraction numerator: denominator denominator: numerator!
115 ^ (self compare: aNumber) < 0!
119 ^ (self compare: aNumber) > 0!
123 ^ (self compare: aNumber) <= 0!
127 ^ (self compare: aNumber) >= 0!
131 ^ (self compare: aNumber) = 0!
135 ^ (self compare: aNumber) ~= 0!
143 aNumber generality > self generality
144 ifTrue: [ ^ (aNumber coerce: self) compare: aNumber ].
148 d := denominator * aNumber denominator.
149 a := d * self numerator.
150 b := d * aNumber numerator.
152 ifFalse: [ numerator compare: denominator * aNumber ]!
156 ^ numerator bitXor: denominator!
173 ^ numerator asFloat / denominator asFloat!
181 ^ Fraction numerator: aNumber asInteger
189 ^ numerator // denominator!
194 printOn: aStream base: anInteger
196 aStream nextPutAll: (numerator printStringBase: anInteger).
198 aStream nextPutAll: (denominator printStringBase: anInteger).
204 numerator: numeratorInteger denominator: denominatorInteger
205 numerator := numeratorInteger.
206 denominator := denominatorInteger!