Fraction.st
author Claus Gittinger <cg@exept.de>
Thu, 25 Apr 1996 19:02:53 +0200
changeset 1295 83f594f05c52
parent 1264 8d916aa63bce
child 1555 316491c1b216
permissions -rw-r--r--
documentation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1989 by Claus Gittinger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
Number subclass:#Fraction
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    14
	instanceVariableNames:'numerator denominator'
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    15
	classVariableNames:'FractionOne FractionZero'
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    16
	poolDictionaries:''
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    17
	category:'Magnitude-Numbers'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    20
!Fraction class methodsFor:'documentation'!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    21
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    22
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    23
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    24
 COPYRIGHT (c) 1989 by Claus Gittinger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
    25
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    26
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    27
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    30
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    36
documentation
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    37
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    38
    Instances of Fraction represent fractional numbers consisting of
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    39
    a numerator and denominator. Both are themselfes arbitrary precision
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
    40
    integers. Fractions are usually created by dividing Integers using /
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    41
    (for exact division).
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    42
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    43
    [author:]
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    44
        Claus Gittinger
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    45
"
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    46
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    47
a27a279701f8 Initial revision
claus
parents:
diff changeset
    48
!Fraction class methodsFor:'initialization'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    49
a27a279701f8 Initial revision
claus
parents:
diff changeset
    50
initialize
302
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
    51
    FractionZero isNil ifTrue:[
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
    52
	FractionZero := self numerator:0 denominator:1.
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
    53
	FractionOne := self numerator:1 denominator:1
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
    54
    ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    55
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
    56
a27a279701f8 Initial revision
claus
parents:
diff changeset
    57
!Fraction class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    58
a27a279701f8 Initial revision
claus
parents:
diff changeset
    59
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
    60
    "create and return a new fraction with value 0"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    61
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
    62
    ^ self numerator:0 denominator:1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    63
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    64
a27a279701f8 Initial revision
claus
parents:
diff changeset
    65
numerator:num denominator:den
a27a279701f8 Initial revision
claus
parents:
diff changeset
    66
    "create and return a new fraction with numerator num and denominator den"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    67
a27a279701f8 Initial revision
claus
parents:
diff changeset
    68
%{  /* NOCONTEXT */
369
claus
parents: 359
diff changeset
    69
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
    70
    /* this check allows subclassing .. */
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    71
    if (self == Fraction) {
835
8bd6f4aa8130 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    72
	if (__CanDoQuickAlignedNew(sizeof(struct __fraction))) {
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
    73
	    OBJ newFraction;
293
31df3850e98c *** empty log message ***
claus
parents: 259
diff changeset
    74
	    int spc;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    75
835
8bd6f4aa8130 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    76
	    __qCheckedAlignedNew(newFraction, sizeof(struct __fraction));
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 835
diff changeset
    77
	    __InstPtr(newFraction)->o_class = self;
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 835
diff changeset
    78
	    __FractionInstPtr(newFraction)->f_numerator = num;
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 835
diff changeset
    79
	    __FractionInstPtr(newFraction)->f_denominator = den;
369
claus
parents: 359
diff changeset
    80
	    if (! __bothSmallInteger(num, den)) {
379
5b5a130ccd09 revision added
claus
parents: 369
diff changeset
    81
		spc = __qSpace(newFraction);
5b5a130ccd09 revision added
claus
parents: 369
diff changeset
    82
		__STORE_SPC(newFraction, num, spc);
5b5a130ccd09 revision added
claus
parents: 369
diff changeset
    83
		__STORE_SPC(newFraction, den, spc);
369
claus
parents: 359
diff changeset
    84
	    }
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
    85
	    RETURN ( newFraction );
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
    86
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    87
    }
a27a279701f8 Initial revision
claus
parents:
diff changeset
    88
%}
a27a279701f8 Initial revision
claus
parents:
diff changeset
    89
.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    90
    ^ self basicNew setNumerator:num denominator:den
a27a279701f8 Initial revision
claus
parents:
diff changeset
    91
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
    92
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    93
!Fraction class methodsFor:'constants'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    94
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    95
unity
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    96
    "return the neutral element for multiplication"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    97
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    98
    ^ FractionOne
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    99
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   100
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   101
zero
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   102
    "return the neutral element for addition"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   103
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   104
    ^ FractionZero
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   105
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   106
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   107
!Fraction class methodsFor:'queries'!
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   108
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   109
isBuiltInClass
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   110
    "return true if this class is known by the run-time-system.
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   111
     Here, true is returned for myself, false for subclasses."
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   112
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   113
    ^ self == Fraction
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   114
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   115
    "Modified: 23.4.1996 / 15:59:10 / cg"
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   116
! !
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   117
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   118
!Fraction methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   119
a27a279701f8 Initial revision
claus
parents:
diff changeset
   120
denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   121
    "return the denominator"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   122
a27a279701f8 Initial revision
claus
parents:
diff changeset
   123
    ^ denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   124
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   125
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   126
numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   127
    "return the numerator"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   128
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   129
    ^ numerator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   130
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   131
a27a279701f8 Initial revision
claus
parents:
diff changeset
   132
!Fraction methodsFor:'arithmetic'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   133
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   134
* aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   135
    "return the product of the receiver and the argument, aNumber"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   136
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   137
    |n d|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   138
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   139
    (aNumber isMemberOf:SmallInteger) ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   140
	^ (self class numerator:(numerator * aNumber)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   141
		    denominator:denominator) reduced
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   142
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   143
    aNumber isFraction ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   144
	n := numerator * aNumber numerator.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   145
	d := denominator * aNumber denominator.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   146
	^ (self class numerator:n denominator:d) reduced
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   147
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   148
    (aNumber isMemberOf:Float) ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   149
	^ (numerator * aNumber) / denominator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   150
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   151
    ^ aNumber productFromFraction:self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   152
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   153
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   154
+ aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   155
    "return the sum of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   156
a27a279701f8 Initial revision
claus
parents:
diff changeset
   157
    |n d|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   158
a27a279701f8 Initial revision
claus
parents:
diff changeset
   159
    (aNumber isMemberOf:SmallInteger) ifTrue:[
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   160
	^ (self class numerator:(numerator + (denominator * aNumber))
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   161
		    denominator:denominator) reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   162
    ].
259
   163
    aNumber isFraction ifTrue:[
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   164
	n := aNumber numerator.
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   165
	d := aNumber denominator.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   166
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   167
	"save a multiplication if possible"
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   168
	denominator == d ifTrue:[
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   169
	    ^ (self class numerator:(numerator + n) denominator:d) reduced
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   170
	].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   171
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   172
	^ (self class numerator:((numerator * d) + (n * denominator))
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   173
		    denominator:(denominator * d)) reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   174
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   175
    (aNumber isMemberOf:Float) ifTrue:[
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   176
	^ aNumber + (numerator asFloat / denominator asFloat)
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   177
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   178
    ^ aNumber sumFromFraction:self
a27a279701f8 Initial revision
claus
parents:
diff changeset
   179
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   180
a27a279701f8 Initial revision
claus
parents:
diff changeset
   181
- aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   182
    "return the difference of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   183
a27a279701f8 Initial revision
claus
parents:
diff changeset
   184
    |n d|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   185
a27a279701f8 Initial revision
claus
parents:
diff changeset
   186
    (aNumber isMemberOf:SmallInteger) ifTrue:[
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   187
	^ (self class numerator:(numerator - (denominator * aNumber))
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   188
		    denominator:denominator) reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   189
    ].
259
   190
    aNumber isFraction ifTrue:[
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   191
	n := aNumber numerator.
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   192
	d := aNumber denominator.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   193
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   194
	"save a multiplication if possible"
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   195
	denominator == d ifTrue:[
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   196
	    ^ (self class numerator:(numerator - n) denominator:d) reduced
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   197
	].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   198
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   199
	^ (self class numerator:((numerator * d) - (n * denominator)) 
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   200
		    denominator:(denominator * d)) reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   201
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   202
    (aNumber isMemberOf:Float) ifTrue:[
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   203
	^ (numerator asFloat / denominator asFloat) - aNumber
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   204
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   205
    ^ aNumber differenceFromFraction:self
a27a279701f8 Initial revision
claus
parents:
diff changeset
   206
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   207
a27a279701f8 Initial revision
claus
parents:
diff changeset
   208
/ aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   209
    "return the quotient of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   210
a27a279701f8 Initial revision
claus
parents:
diff changeset
   211
    |n d|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   212
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   213
    (aNumber isMemberOf:SmallInteger) ifTrue:[
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   214
	^ (self class numerator:numerator
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   215
		    denominator:(denominator * aNumber)) reduced
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   216
    ].
259
   217
    aNumber isFraction ifTrue:[
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   218
	n := numerator * aNumber denominator.
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   219
	d := denominator * aNumber numerator.
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   220
	^ (self class numerator:n denominator:d) reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   221
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   222
    (aNumber isMemberOf:Float) ifTrue:[
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   223
	^ numerator / (denominator * aNumber)
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   224
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   225
    ^ aNumber quotientFromFraction:self
a27a279701f8 Initial revision
claus
parents:
diff changeset
   226
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   227
a27a279701f8 Initial revision
claus
parents:
diff changeset
   228
// aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   229
    "return the integer quotient of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   230
a27a279701f8 Initial revision
claus
parents:
diff changeset
   231
    self negative ifTrue:[
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   232
	 ^ ((numerator * aNumber denominator) // (denominator * aNumber numerator)) - 1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   233
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   234
    ^ (numerator * aNumber denominator) // (denominator * aNumber numerator)
a27a279701f8 Initial revision
claus
parents:
diff changeset
   235
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   236
a27a279701f8 Initial revision
claus
parents:
diff changeset
   237
negated
a27a279701f8 Initial revision
claus
parents:
diff changeset
   238
    "optional - could use inherited method ..."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   239
a27a279701f8 Initial revision
claus
parents:
diff changeset
   240
    ^ self class numerator:(numerator negated)
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   241
	       denominator:denominator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   242
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   243
a27a279701f8 Initial revision
claus
parents:
diff changeset
   244
reciprocal
a27a279701f8 Initial revision
claus
parents:
diff changeset
   245
    "optional - could use inherited method ..."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   246
a27a279701f8 Initial revision
claus
parents:
diff changeset
   247
    numerator == 1 ifTrue:[^ denominator].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   248
    ^ self class numerator:denominator
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   249
	       denominator:numerator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   250
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   251
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   252
!Fraction methodsFor:'coercing & converting'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   253
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   254
asFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   255
    "return a float with (approximately) my value"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   256
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   257
    ^ (numerator asFloat) / (denominator asFloat)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   258
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   259
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   260
asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   261
    "return the receiver as fraction - thats itself"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   262
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   263
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   264
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   265
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   266
asInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   267
    "return an integer with my value - will usually truncate"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   268
a27a279701f8 Initial revision
claus
parents:
diff changeset
   269
    ^ numerator // denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   270
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   271
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   272
asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   273
    "return an integer with my value - will usually truncate"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   274
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   275
    ^ self asInteger asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   276
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   277
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   278
asShortFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   279
    "return a shortFloat with (approximately) my value"
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   280
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   281
    ^ (numerator asShortFloat) / (denominator asShortFloat)
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   282
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   283
    "Created: 17.4.1996 / 12:21:08 / cg"
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   284
!
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   285
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   286
coerce:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   287
    "return aNumber converted into receivers type"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   288
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   289
    ^ aNumber asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   290
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   291
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   292
generality
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   293
    "return the generality value - see ArithmeticValue>>retry:coercing:"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   294
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   295
    ^ 60
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   296
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   297
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   298
!Fraction methodsFor:'comparing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   299
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   300
< aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   301
    "return true if the receiver is less
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   302
     than aNumber, false otherwise."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   303
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   304
    (aNumber isMemberOf:SmallInteger) ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   305
	^ numerator < (denominator * aNumber)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   306
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   307
    aNumber isFraction ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   308
	^ (numerator * aNumber denominator) < (denominator * aNumber numerator)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   309
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   310
    ^ aNumber lessFromFraction:self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   311
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   312
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   313
= aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   314
    "return true, if the argument represents the same numeric value
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   315
     as the receiver, false otherwise"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   316
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   317
    (aNumber isMemberOf:SmallInteger) ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   318
	(denominator = 1) ifFalse:[^ false].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   319
	^ numerator = aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   320
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   321
    aNumber isFraction ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   322
	(numerator = aNumber numerator) ifFalse:[^ false].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   323
	^ denominator = aNumber denominator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   324
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   325
    ^ self retry:#= coercing:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   326
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   327
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   328
> aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   329
    "return true if the receiver is greater
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   330
     than aNumber, false otherwise."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   331
    "optional - could use inherited method ..."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   332
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   333
    (aNumber isMemberOf:SmallInteger) ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   334
	^ numerator > (denominator * aNumber)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   335
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   336
    aNumber isFraction ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   337
	^ (numerator * aNumber denominator) > (denominator * aNumber numerator)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   338
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   339
    ^ self retry:#> coercing:aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   340
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   341
a27a279701f8 Initial revision
claus
parents:
diff changeset
   342
!Fraction methodsFor:'double dispatching'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   343
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   344
differenceFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   345
    "sent when a float does not know how to subtract the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   346
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   347
    ^ (aFloat * denominator - numerator) / denominator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   348
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   349
a27a279701f8 Initial revision
claus
parents:
diff changeset
   350
differenceFromInteger:anInteger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   351
    "sent when an integer does not know how to subtract the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   352
a27a279701f8 Initial revision
claus
parents:
diff changeset
   353
    ^ (self class numerator:((anInteger * denominator) - numerator)
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   354
		denominator:denominator) reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   355
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   356
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   357
lessFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   358
    "sent when an integer does not know how to compare to the receiver, a fraction"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   359
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   360
    ^ (denominator * anInteger) < numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   361
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   362
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   363
productFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   364
    "sent when a float does not know how to multiply the receiver, a fraction"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   365
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   366
    ^ aFloat * numerator / denominator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   367
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   368
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   369
productFromInteger:anInteger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   370
    "sent when an integer does not know how to multiply the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   371
a27a279701f8 Initial revision
claus
parents:
diff changeset
   372
    ^ (self class numerator:(anInteger * numerator)
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   373
		denominator:denominator) reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   374
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   375
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   376
quotientFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   377
    "sent when a float does not know how to divide by the receiver, a fraction"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   378
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   379
    ^ (aFloat * denominator) / numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   380
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   381
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   382
quotientFromInteger:anInteger
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   383
    "sent when an integer does not know how to divide by the receiver, a fraction"
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   384
328
claus
parents: 325
diff changeset
   385
    ^ (self class numerator:(anInteger * denominator)
claus
parents: 325
diff changeset
   386
		denominator:numerator) reduced
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   387
!
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   388
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   389
sumFromFloat:aFloat
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   390
    "sent when a float does not know how to add the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   391
a27a279701f8 Initial revision
claus
parents:
diff changeset
   392
    ^ (aFloat * denominator + numerator) / denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   393
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   394
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   395
sumFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   396
    "sent when an integer does not know how to add the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   397
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   398
    ^ (self class numerator:(numerator + (anInteger * denominator))
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   399
		denominator:denominator) reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   400
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   401
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   402
!Fraction methodsFor:'printing & storing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   403
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   404
printOn:aStream
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   405
    "append a printed representation of the receiver to the
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   406
     argument, aStream"
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   407
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   408
    aStream nextPut:$(.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   409
    numerator printOn:aStream.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   410
    aStream nextPut:$/.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   411
    denominator printOn:aStream.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   412
    aStream nextPut:$)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   413
! !
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   414
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   415
!Fraction methodsFor:'private'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   416
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   417
reduced
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   418
    "reduce the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   419
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   420
    |gc|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   421
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   422
    denominator == 1 ifTrue:[^ numerator].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   423
    numerator == 1 ifTrue:[^ self].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   424
    numerator == 0 ifTrue:[^ 0].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   425
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   426
    gc := numerator gcd:denominator.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   427
    (gc == 1) ifFalse:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   428
	numerator := numerator // gc.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   429
	denominator := denominator // gc
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   430
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   431
    (numerator < 0) ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   432
	(denominator < 0) ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   433
	    numerator := numerator negated.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   434
	    denominator := denominator negated
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   435
	]
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   436
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   437
    (denominator == 1) ifTrue:[^ numerator].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   438
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   439
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   440
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   441
setNumerator:num denominator:den
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   442
    "set both numerator and denominator"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   443
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   444
    numerator := num.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   445
    denominator := den
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   446
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   447
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   448
!Fraction methodsFor:'queries'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   449
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   450
isFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   451
    "return true, if the receiver is some kind of fraction;
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   452
     true is returned here - the method is redefined from Object."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   453
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   454
    ^ true
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   455
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   456
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   457
!Fraction methodsFor:'testing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   458
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   459
negative
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   460
    "return true if the receiver is negative"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   461
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   462
    (numerator < 0) ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   463
	^ (denominator < 0) not
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   464
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   465
    ^ (denominator < 0)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   466
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   467
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   468
!Fraction methodsFor:'truncation and rounding'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   469
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   470
rounded
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   471
    "return the receiver rounded to the nearest integer as integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   472
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   473
    self negative ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   474
	^ (self + (1/2)) truncated - 1
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   475
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   476
    ^ (self + (1/2)) truncated
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   477
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   478
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   479
truncated
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   480
    "return the receiver truncated towards zero as Integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   481
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   482
    ^ numerator // denominator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   483
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   484
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   485
!Fraction class methodsFor:'documentation'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   486
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   487
version
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
   488
    ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.30 1996-04-25 16:59:34 cg Exp $'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   489
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   490
Fraction initialize!