Fraction.st
author Claus Gittinger <cg@exept.de>
Mon, 28 Jul 1997 17:08:53 +0200
changeset 2790 495b8ea6a4cb
parent 2789 e3e8707d26b4
child 2792 2ae1f2e3d11f
permissions -rw-r--r--
*** empty log message ***
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
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
    20
!Fraction class methodsFor:'documentation'!
88
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
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
    43
    Mixed mode arithmetic:
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
    44
        fraction op fraction    -> fraction
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
    45
        fraction op fix         -> fix; scale is fix's scale
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
    46
        fraction op integer     -> fraction
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
    47
        fraction op float       -> float
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
    48
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    49
    [author:]
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    50
        Claus Gittinger
1556
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    51
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    52
    [see also:]
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    53
        Number
1893
c66af5c46272 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1892
diff changeset
    54
        Float Integer FixedPoint 
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    55
"
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    56
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    57
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
    58
!Fraction class methodsFor:'initialization'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    59
a27a279701f8 Initial revision
claus
parents:
diff changeset
    60
initialize
302
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
    61
    FractionZero isNil ifTrue:[
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
    62
	FractionZero := self numerator:0 denominator:1.
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
    63
	FractionOne := self numerator:1 denominator:1
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
    64
    ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    65
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
    66
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
    67
!Fraction class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    68
a27a279701f8 Initial revision
claus
parents:
diff changeset
    69
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
    70
    "create and return a new fraction with value 0"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    71
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
    72
    ^ self numerator:0 denominator:1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    73
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    74
a27a279701f8 Initial revision
claus
parents:
diff changeset
    75
numerator:num denominator:den
a27a279701f8 Initial revision
claus
parents:
diff changeset
    76
    "create and return a new fraction with numerator num and denominator den"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    77
a27a279701f8 Initial revision
claus
parents:
diff changeset
    78
%{  /* NOCONTEXT */
369
claus
parents: 359
diff changeset
    79
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
    80
    /* this check allows subclassing .. */
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    81
    if (self == Fraction) {
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    82
	if (__bothSmallInteger(num, den)) {
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    83
	    if (__CanDoQuickAlignedNew(sizeof(struct __fraction))) {
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    84
	        OBJ newFraction;
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    85
	        int spc;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    86
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    87
	        __qCheckedAlignedNew(newFraction, sizeof(struct __fraction));
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    88
	        __InstPtr(newFraction)->o_class = self;
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    89
		if (__intVal(den) < 0) {
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    90
		    __FractionInstPtr(newFraction)->f_numerator = __MKSMALLINT(- __intVal(num));
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    91
		    __FractionInstPtr(newFraction)->f_denominator = __MKSMALLINT(- __intVal(den));
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    92
		} else {
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    93
	            __FractionInstPtr(newFraction)->f_numerator = num;
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    94
	            __FractionInstPtr(newFraction)->f_denominator = den;
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    95
		}
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
    96
	        RETURN ( newFraction );
369
claus
parents: 359
diff changeset
    97
	    }
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
    98
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    99
    }
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   100
%}.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   101
    ^ (self basicNew setNumerator:num denominator:den) reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   102
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   103
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   104
!Fraction class methodsFor:'constants'!
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   105
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   106
pi
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   107
    "return the constant pi as Fraction"
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   108
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   109
    ^ self 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   110
        numerator:  31415926535897932384626434
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   111
        denominator:10000000000000000000000000
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   112
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   113
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   114
     Fraction pi         
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   115
     Fraction pi asFloat
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   116
     Float pi            
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   117
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   118
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   119
    "Modified: 5.11.1996 / 11:11:44 / cg"
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   120
!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   121
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   122
unity
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   123
    "return the neutral element for multiplication (1 / 1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   124
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   125
    ^ FractionOne
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   126
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   127
    "Modified: 18.7.1996 / 12:26:06 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   128
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   129
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   130
zero
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   131
    "return the neutral element for addition (0 / 1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   132
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   133
    ^ FractionZero
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   134
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   135
    "Modified: 18.7.1996 / 12:26:12 / cg"
701
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
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   138
!Fraction class methodsFor:'queries'!
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   139
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   140
isBuiltInClass
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   141
    "return true if this class is known by the run-time-system.
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   142
     Here, true is returned for myself, false for subclasses."
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   143
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   144
    ^ self == Fraction
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   145
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   146
    "Modified: 23.4.1996 / 15:59:10 / cg"
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   147
! !
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   148
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   149
!Fraction methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   150
a27a279701f8 Initial revision
claus
parents:
diff changeset
   151
denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   152
    "return the denominator"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   153
a27a279701f8 Initial revision
claus
parents:
diff changeset
   154
    ^ denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   155
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   156
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   157
numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   158
    "return the numerator"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   159
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   160
    ^ numerator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   161
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   162
a27a279701f8 Initial revision
claus
parents:
diff changeset
   163
!Fraction methodsFor:'arithmetic'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   164
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   165
* aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   166
    "return the product of the receiver and the argument, aNumber"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   167
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   168
    |n d|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   169
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   170
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   171
        ^ (self class numerator:(numerator * aNumber)
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   172
                    denominator:denominator) reduced
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   173
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   174
    aNumber isFraction ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   175
        aNumber isFixedPoint ifFalse:[  "/ the value was corrent, but the scale is lost
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   176
            n := numerator * aNumber numerator.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   177
            d := denominator * aNumber denominator.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   178
            ^ (self class numerator:n denominator:d) reduced
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   179
        ]
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   180
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   181
    (aNumber isMemberOf:Float) ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   182
        ^ (numerator * aNumber) / denominator
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   183
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   184
    ^ aNumber productFromFraction:self
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   185
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   186
    "Modified: 5.11.1996 / 19:31:50 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   187
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   188
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   189
+ aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   190
    "return the sum of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   191
a27a279701f8 Initial revision
claus
parents:
diff changeset
   192
    |n d|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   193
a27a279701f8 Initial revision
claus
parents:
diff changeset
   194
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   195
        ^ (self class numerator:(numerator + (denominator * aNumber))
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   196
                    denominator:denominator) reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   197
    ].
259
   198
    aNumber isFraction ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   199
        aNumber isFixedPoint ifFalse:[  "/ the value was corrent, but the scale is lost
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   200
            n := aNumber numerator.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   201
            d := aNumber denominator.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   202
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   203
            "save a multiplication if possible"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   204
            denominator == d ifTrue:[
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   205
                n := numerator + n
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   206
            ] ifFalse:[
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   207
                n := (numerator * d) + (n * denominator).
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   208
                d := denominator * d.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   209
            ].
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   210
            ^ (self class numerator:n denominator:d) reduced
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   211
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   212
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   213
    (aNumber isMemberOf:Float) ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   214
        ^ aNumber + (numerator asFloat / denominator asFloat)
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   215
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   216
    ^ aNumber sumFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   217
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   218
    "Modified: 5.11.1996 / 19:31:41 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   219
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   220
a27a279701f8 Initial revision
claus
parents:
diff changeset
   221
- aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   222
    "return the difference of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   223
a27a279701f8 Initial revision
claus
parents:
diff changeset
   224
    |n d|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   225
a27a279701f8 Initial revision
claus
parents:
diff changeset
   226
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   227
        ^ (self class numerator:(numerator - (denominator * aNumber))
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   228
                    denominator:denominator) reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   229
    ].
259
   230
    aNumber isFraction ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   231
        aNumber isFixedPoint ifFalse:[  "/ the value was corrent, but the scale is lost
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   232
            n := aNumber numerator.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   233
            d := aNumber denominator.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   234
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   235
            "save a multiplication if possible"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   236
            denominator == d ifTrue:[
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   237
                n := numerator - n
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   238
            ] ifFalse:[
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   239
                n := (numerator * d) - (n * denominator).
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   240
                d := denominator * d
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   241
            ].
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   242
            ^ (self class numerator:n denominator:d) reduced
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   243
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   244
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   245
    (aNumber isMemberOf:Float) ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   246
        ^ (numerator asFloat / denominator asFloat) - aNumber
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   247
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   248
    ^ aNumber differenceFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   249
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   250
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   251
     (1/3) - (1/9)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   252
     (1/9) - (1/3)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   253
     (999/1000) - (1/1000)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   254
     (999/1000) - (1/1000000)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   255
     (999000/1000000) - (1/1000000)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   256
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   257
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   258
    "Modified: 5.11.1996 / 19:31:32 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   259
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   260
a27a279701f8 Initial revision
claus
parents:
diff changeset
   261
/ aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   262
    "return the quotient of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   263
a27a279701f8 Initial revision
claus
parents:
diff changeset
   264
    |n d|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   265
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   266
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   267
        ^ (self class numerator:numerator
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   268
                    denominator:(denominator * aNumber)) reduced
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   269
    ].
259
   270
    aNumber isFraction ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   271
        aNumber isFixedPoint ifFalse:[  "/ the value was corrent, but the scale is lost
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   272
            n := numerator * aNumber denominator.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   273
            d := denominator * aNumber numerator.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   274
            ^ (self class numerator:n denominator:d) reduced
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   275
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   276
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   277
    (aNumber isMemberOf:Float) ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   278
        ^ numerator / (denominator * aNumber)
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   279
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   280
    ^ aNumber quotientFromFraction:self
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   281
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   282
    "Modified: 5.11.1996 / 19:31:23 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   283
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   284
a27a279701f8 Initial revision
claus
parents:
diff changeset
   285
// aNumber
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   286
    "return the integer quotient of dividing the receiver by aNumber with
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   287
     truncation towards negative infinity."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   288
a27a279701f8 Initial revision
claus
parents:
diff changeset
   289
    self negative ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   290
         ^ ((numerator * aNumber denominator) // (denominator * aNumber numerator)) - 1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   291
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   292
    ^ (numerator * aNumber denominator) // (denominator * aNumber numerator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   293
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   294
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   295
     0.5 // 1
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   296
     -0.5 // 1
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   297
     (1/2) // 1
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   298
     (1/2) negated // 1
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   299
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   300
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   301
    "Modified: 5.11.1996 / 11:47:14 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   302
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   303
a27a279701f8 Initial revision
claus
parents:
diff changeset
   304
negated
a27a279701f8 Initial revision
claus
parents:
diff changeset
   305
    "optional - could use inherited method ..."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   306
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   307
    ^ self class 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   308
        numerator:(numerator negated)
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   309
        denominator:denominator
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   310
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   311
    "Modified: 5.11.1996 / 10:29:11 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   312
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   313
a27a279701f8 Initial revision
claus
parents:
diff changeset
   314
reciprocal
a27a279701f8 Initial revision
claus
parents:
diff changeset
   315
    "optional - could use inherited method ..."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   316
a27a279701f8 Initial revision
claus
parents:
diff changeset
   317
    numerator == 1 ifTrue:[^ denominator].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   318
    ^ self class 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   319
        numerator:denominator
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   320
        denominator:numerator
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   321
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   322
    "Modified: 5.11.1996 / 10:29:22 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   323
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   324
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   325
!Fraction methodsFor:'coercing & converting'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   326
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   327
asFixedPoint
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   328
    "return the receiver as fixedPoint number.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   329
     Q: what should the scale be here ?"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   330
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   331
    ^ FixedPoint numerator:numerator denominator:denominator scale:2
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   332
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   333
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   334
     (1/2) asFixedPoint
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   335
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   336
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   337
    "Created: 5.11.1996 / 15:15:54 / cg"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   338
!
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   339
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   340
asFixedPoint:scale
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   341
    "return the receiver as fixedPoint number, with the given number
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   342
     of post-decimal-point digits."
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   343
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   344
    ^ FixedPoint numerator:numerator denominator:denominator scale:scale
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   345
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   346
    "
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   347
     (1/2) asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   348
     (1/3) asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   349
     (1/3) asFixedPoint:5 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   350
     (2/3) asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   351
     (2/3) asFixedPoint:5 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   352
    "
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   353
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   354
    "Created: 5.11.1996 / 15:15:54 / cg"
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   355
    "Modified: 10.1.1997 / 19:54:50 / cg"
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   356
!
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   357
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   358
asFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   359
    "return a float with (approximately) my value"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   360
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   361
    ^ (numerator asFloat) / (denominator asFloat)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   362
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   363
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   364
asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   365
    "return the receiver as fraction - thats itself"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   366
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   367
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   368
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   369
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   370
asInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   371
    "return an integer with my value - will usually truncate"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   372
a27a279701f8 Initial revision
claus
parents:
diff changeset
   373
    ^ numerator // denominator
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
asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   377
    "return an integer with my value - will usually truncate"
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
    ^ self asInteger asLargeInteger
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
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   382
asShortFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   383
    "return a shortFloat with (approximately) my value"
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   384
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   385
    ^ (numerator asShortFloat) / (denominator asShortFloat)
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   386
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   387
    "Created: 17.4.1996 / 12:21:08 / cg"
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   388
!
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   389
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   390
coerce:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   391
    "return aNumber converted into receivers type"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   392
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   393
    ^ aNumber asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   394
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   395
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   396
generality
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   397
    "return the generality value - see ArithmeticValue>>retry:coercing:"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   398
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   399
    ^ 60
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   400
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   401
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   402
!Fraction methodsFor:'comparing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   403
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   404
< aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   405
    "return true if the receiver is less
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   406
     than aNumber, false otherwise."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   407
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   408
    |d n|
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   409
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   410
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   411
        ^ numerator < (denominator * aNumber)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   412
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   413
    aNumber isFraction ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   414
        d := aNumber denominator.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   415
        n := aNumber numerator.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   416
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   417
        "/ save a multiplication if possible
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   418
        d == denominator ifTrue:[
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   419
            ^ numerator < n
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   420
        ].
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   421
        ^ (numerator * d) < (denominator * n)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   422
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   423
    ^ aNumber lessFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   424
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   425
    "Modified: 5.11.1996 / 10:30:52 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   426
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   427
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   428
= aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   429
    "return true, if the argument represents the same numeric value
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   430
     as the receiver, false otherwise"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   431
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   432
    (aNumber isMemberOf:SmallInteger) ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   433
	(denominator = 1) ifFalse:[^ false].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   434
	^ numerator = aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   435
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   436
    aNumber isFraction ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   437
	(numerator = aNumber numerator) ifFalse:[^ false].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   438
	^ denominator = aNumber denominator
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
    ^ self retry:#= coercing:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   441
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   442
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   443
> aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   444
    "return true if the receiver is greater
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   445
     than aNumber, false otherwise."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   446
    "optional - could use inherited method ..."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   447
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   448
    |d n|
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   449
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   450
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   451
        ^ numerator > (denominator * aNumber)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   452
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   453
    aNumber isFraction ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   454
        d := aNumber denominator.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   455
        n := aNumber numerator.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   456
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   457
        "/ save a multiplication if possible
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   458
        d == denominator ifTrue:[
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   459
            ^ numerator > n
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   460
        ].
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   461
        ^ (numerator * d) > (denominator * n)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   462
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   463
    ^ self retry:#> coercing:aNumber
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   464
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   465
    "Modified: 5.11.1996 / 10:31:28 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   466
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   467
a27a279701f8 Initial revision
claus
parents:
diff changeset
   468
!Fraction methodsFor:'double dispatching'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   469
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   470
differenceFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   471
    "sent when a float does not know how to subtract the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   472
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   473
    ^ (aFloat * denominator - numerator) / denominator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   474
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   475
a27a279701f8 Initial revision
claus
parents:
diff changeset
   476
differenceFromInteger:anInteger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   477
    "sent when an integer does not know how to subtract the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   478
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   479
    ^ (self class 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   480
        numerator:((anInteger * denominator) - numerator)
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   481
        denominator:denominator) reduced
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   482
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   483
    "Modified: 5.11.1996 / 10:32:14 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   484
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   485
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   486
lessFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   487
    "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
   488
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   489
    ^ (denominator * anInteger) < numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   490
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   491
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   492
productFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   493
    "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
   494
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   495
    ^ aFloat * numerator / denominator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   496
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   497
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   498
productFromInteger:anInteger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   499
    "sent when an integer does not know how to multiply the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   500
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   501
    ^ (self class 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   502
        numerator:(anInteger * numerator)
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   503
        denominator:denominator) reduced
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   504
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   505
    "Modified: 5.11.1996 / 10:32:28 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   506
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   507
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   508
quotientFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   509
    "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
   510
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   511
    ^ (aFloat * denominator) / numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   512
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   513
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   514
quotientFromInteger:anInteger
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   515
    "sent when an integer does not know how to divide by the receiver, a fraction"
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   516
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   517
    ^ (self class 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   518
        numerator:(anInteger * denominator)
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   519
        denominator:numerator) reduced
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   520
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   521
    "Modified: 5.11.1996 / 10:32:35 / cg"
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   522
!
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   523
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   524
sumFromFloat:aFloat
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   525
    "sent when a float does not know how to add the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   526
a27a279701f8 Initial revision
claus
parents:
diff changeset
   527
    ^ (aFloat * denominator + numerator) / denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   528
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   529
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   530
sumFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   531
    "sent when an integer does not know how to add the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   532
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   533
    ^ (self class 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   534
        numerator:(numerator + (anInteger * denominator))
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   535
        denominator:denominator) reduced
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   536
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   537
    "Modified: 5.11.1996 / 10:32:43 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   538
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   539
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   540
!Fraction methodsFor:'printing & storing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   541
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   542
printOn:aStream
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   543
    "append a printed representation of the receiver to the
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   544
     argument, aStream"
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   545
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   546
    aStream nextPut:$(.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   547
    numerator printOn:aStream.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   548
    aStream nextPut:$/.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   549
    denominator printOn:aStream.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   550
    aStream nextPut:$)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   551
! !
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   552
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   553
!Fraction methodsFor:'private'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   554
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   555
reduced
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   556
    "reduce the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   557
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   558
    |gcd den|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   559
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   560
    den := denominator.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   561
    den < 0 ifTrue:[
2789
e3e8707d26b4 make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   562
	numerator := numerator negated.
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   563
	den := denominator := den negated.
2789
e3e8707d26b4 make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   564
    ].
e3e8707d26b4 make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   565
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   566
    den == 1 ifTrue:[^ numerator].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   567
    numerator == 1 ifTrue:[^ self].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   568
    numerator == 0 ifTrue:[^ 0].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   569
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   570
    gcd := numerator gcd:den.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   571
    (gcd ~~ 1) ifTrue:[
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   572
	numerator := numerator // gcd.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   573
	denominator := den // gcd.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   574
        (denominator == 1) ifTrue:[^ numerator].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   575
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   576
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   577
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   578
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   579
setNumerator:num denominator:den
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   580
    "set both numerator and denominator"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   581
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   582
    numerator := num.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   583
    denominator := den
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   584
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   585
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   586
!Fraction methodsFor:'queries'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   587
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   588
isFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   589
    "return true, if the receiver is some kind of fraction;
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   590
     true is returned here - the method is redefined from Object."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   591
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   592
    ^ true
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   593
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   594
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   595
!Fraction methodsFor:'testing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   596
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   597
negative
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   598
    "return true if the receiver is negative"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   599
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   600
    (numerator < 0) ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   601
	^ (denominator < 0) not
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   602
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   603
    ^ (denominator < 0)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   604
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   605
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   606
!Fraction methodsFor:'truncation and rounding'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   607
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   608
rounded
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   609
    "return the receiver rounded to the nearest integer as integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   610
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   611
    "/ mhmh - what about -(1/2)
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   612
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   613
    |t|
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   614
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   615
    self negative ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   616
        t := self - (1/2)
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   617
    ] ifFalse:[
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   618
        t := self + (1/2)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   619
    ].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   620
    ^ t truncated.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   621
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   622
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   623
     (1/3) rounded           
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   624
     (1/3) negated rounded     
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   625
     (1/2) rounded           
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   626
     (1/2) negated rounded   
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   627
     0.5 rounded  
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   628
     -0.5 rounded 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   629
     (2/3) rounded             
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   630
     (2/3) negated rounded     
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   631
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   632
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   633
    "Modified: 5.11.1996 / 11:32:32 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   634
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   635
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   636
truncated
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   637
    "return the receiver truncated towards zero as Integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   638
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   639
    ^ numerator quo: denominator
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   640
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   641
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   642
     (3/2) truncated     
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   643
     (3/2) negated truncated  
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   644
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   645
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   646
    "Modified: 5.11.1996 / 12:18:46 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   647
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   648
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   649
!Fraction class methodsFor:'documentation'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   650
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   651
version
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   652
    ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.38 1997-07-28 15:08:53 cg Exp $'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   653
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   654
Fraction initialize!