13 " |
13 " |
14 |
14 |
15 "{ Package: 'stx:libbasic' }" |
15 "{ Package: 'stx:libbasic' }" |
16 |
16 |
17 Fraction subclass:#FixedPoint |
17 Fraction subclass:#FixedPoint |
18 instanceVariableNames:'scale' |
18 instanceVariableNames:'scale' |
19 classVariableNames:'PI_1000' |
19 classVariableNames:'PI_1000' |
20 poolDictionaries:'' |
20 poolDictionaries:'' |
21 category:'Magnitude-Numbers' |
21 category:'Magnitude-Numbers' |
22 ! |
22 ! |
23 |
23 |
24 FixedPoint comment:' |
24 FixedPoint comment:' |
25 Description: This class implements infinite precision fixed-point numbers. |
25 Description: This class implements infinite precision fixed-point numbers. |
26 It doesn''t really do anything too interesting except creating instances, converting, and printing, |
26 It doesn''t really do anything too interesting except creating instances, converting, and printing, |
187 FixedPoint readFrom:(ReadStream on:'foobar') |
187 FixedPoint readFrom:(ReadStream on:'foobar') |
188 FixedPoint readFrom:(ReadStream on:'foobar') onError:nil |
188 FixedPoint readFrom:(ReadStream on:'foobar') onError:nil |
189 " |
189 " |
190 |
190 |
191 "Modified: / 25.10.1997 / 15:30:29 / cg" |
191 "Modified: / 25.10.1997 / 15:30:29 / cg" |
192 ! |
|
193 |
|
194 readFrom:aStringOrStream onError:exceptionBlock |
|
195 "return an instance of me as described on the string or stream, aStringOrStream. |
|
196 If an error occurs during conversion, return the result |
|
197 from evaluating exceptionBlock" |
|
198 |
|
199 | aStream sign integerPart fractionStream char fractionPart scale | |
|
200 |
|
201 aStream := aStringOrStream readStream. |
|
202 |
|
203 aStream peek == $- ifTrue:[ |
|
204 sign := -1. |
|
205 aStream next. |
|
206 ] ifFalse:[ |
|
207 sign := 1 |
|
208 ]. |
|
209 |
|
210 (aStream atEnd or:[aStream peek isLetter]) ifTrue: [^ exceptionBlock value]. |
|
211 |
|
212 integerPart := (aStream upTo:$.) asNumber. |
|
213 (aStream atEnd or: [aStream peek isLetter]) ifTrue: [ |
|
214 fractionPart := 0. |
|
215 scale := 1. |
|
216 ] ifFalse:[ |
|
217 fractionStream := ReadWriteStream on:(String new: 10). |
|
218 [ |
|
219 char := aStream next. |
|
220 char ~~ nil and:[char isDigit] |
|
221 ] whileTrue:[ |
|
222 fractionStream nextPut:char |
|
223 ]. |
|
224 |
|
225 scale := fractionStream positionStartingAt0. |
|
226 fractionStream reset. |
|
227 fractionPart := Number readFrom:fractionStream. |
|
228 ]. |
|
229 |
|
230 ^ self basicNew |
|
231 setNumerator:(integerPart * (10 raisedTo:scale) + fractionPart) * sign |
|
232 scale:scale |
|
233 |
|
234 " |
|
235 FixedPoint readFrom:'123.456' |
|
236 FixedPoint readFrom:'-123.456' |
|
237 FixedPoint readFrom:'123' |
|
238 FixedPoint readFrom:'-123' |
|
239 FixedPoint readFrom:'-123.abcd' onError:[47.5] |
|
240 FixedPoint readFrom:'-1a.bcd' onError:[47.5] |
|
241 FixedPoint readFrom:'foot' onError:['bad fixedpoint'] |
|
242 " |
|
243 |
|
244 "Created: / 25.10.1997 / 15:28:59 / cg" |
|
245 "Modified: / 25.10.1997 / 15:31:47 / cg" |
|
246 ! ! |
192 ! ! |
247 |
193 |
248 !FixedPoint class methodsFor:'constants'! |
194 !FixedPoint class methodsFor:'constants'! |
249 |
195 |
250 pi |
196 pi |
369 ! |
316 ! |
370 |
317 |
371 + aNumber |
318 + aNumber |
372 "return the sum of the receiver and the argument, aNumber. |
319 "return the sum of the receiver and the argument, aNumber. |
373 Redefined to care for the scale if the argument is another fixPoint number. |
320 Redefined to care for the scale if the argument is another fixPoint number. |
374 The resulting scale will be the maximum of the receivers and the |
321 The results scale will be the maximum of the receivers and the |
375 arguments scale." |
322 arguments scale." |
376 |
323 |
377 |n d| |
324 |n d| |
378 |
325 |
379 (aNumber isInteger or:[aNumber isFraction]) ifTrue:[ |
326 (aNumber isInteger or:[aNumber isFraction]) ifTrue:[ |
568 The results scale is the maximum of the receivers scale and the arguments |
515 The results scale is the maximum of the receivers scale and the arguments |
569 scale." |
516 scale." |
570 |
517 |
571 aNumber isInteger ifTrue:[ |
518 aNumber isInteger ifTrue:[ |
572 ^ self class |
519 ^ self class |
573 numerator:numerator |
520 numerator:numerator |
574 denominator:(denominator * aNumber) |
521 denominator:(denominator * aNumber) |
575 scale:scale |
522 scale:scale |
576 ]. |
523 ]. |
577 |
524 |
578 aNumber isFraction ifTrue:[ |
525 aNumber isFraction ifTrue:[ |
579 ^ self class |
526 ^ self class |
580 numerator:(numerator * aNumber denominator) |
527 numerator:(numerator * aNumber denominator) |
635 Transcript show:'float (inexact): '; showCR:(1 / 0.9999999). |
582 Transcript show:'float (inexact): '; showCR:(1 / 0.9999999). |
636 " |
583 " |
637 ! |
584 ! |
638 |
585 |
639 negated |
586 negated |
640 "have to redefine it from Fraction" |
587 "redefined from Fraction to preserve scale" |
641 |
588 |
642 ^ self class |
589 ^ self class |
643 numerator:(numerator negated) |
590 numerator:(numerator negated) |
644 denominator:denominator |
591 denominator:denominator |
645 scale:scale |
592 scale:scale |
646 ! |
593 ! |
647 |
594 |
648 reciprocal |
595 reciprocal |
649 "have to redefine it from Fraction" |
596 "redefined from Fraction to preserve scale" |
650 |
597 |
651 numerator == 1 ifTrue:[^ denominator]. |
598 numerator == 1 ifTrue:[^ denominator]. |
652 ^ self class |
599 ^ self class |
653 numerator:denominator |
600 numerator:denominator |
654 denominator:numerator |
601 denominator:numerator |