Fraction.st
author Claus Gittinger <cg@exept.de>
Thu, 12 Aug 1999 10:58:36 +0200
changeset 4594 eb09f567a3bc
parent 4593 1023fa0c779e
child 4658 1a74754fbe91
permissions -rw-r--r--
float, shortFloat and fraction all hash alike
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
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    78
    |newFraction|
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    79
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    80
%{  /* NOCONTEXT */
369
claus
parents: 359
diff changeset
    81
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
    82
    /* this check allows subclassing .. */
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    83
    if (self == Fraction) {
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    84
        if (__bothSmallInteger(num, den)) {
2894
344aec8ba014 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2853
diff changeset
    85
            if (__CanDoQuickAlignedNew(sizeof(struct __fraction))) {	/* OBJECT ALLOCATION */
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    86
                OBJ newFraction;
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    87
                int spc;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    88
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    89
                __qCheckedAlignedNew(newFraction, sizeof(struct __fraction));
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    90
                __InstPtr(newFraction)->o_class = self;
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    91
                if (__intVal(den) < 0) {
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    92
                    __FractionInstPtr(newFraction)->f_numerator = __MKSMALLINT(- __intVal(num));
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    93
                    __FractionInstPtr(newFraction)->f_denominator = __MKSMALLINT(- __intVal(den));
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    94
                } else {
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    95
                    __FractionInstPtr(newFraction)->f_numerator = num;
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    96
                    __FractionInstPtr(newFraction)->f_denominator = den;
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    97
                }
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    98
                if (num == __MKSMALLINT(1)) {
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    99
                    RETURN ( newFraction );
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   100
                }
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   101
            }
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   102
        }
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   103
    }
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   104
%}.
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   105
    newFraction isNil ifTrue:[
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   106
        newFraction :=  self basicNew setNumerator:num denominator:den.
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   107
    ].
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   108
    ^ newFraction reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   109
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   110
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   111
!Fraction class methodsFor:'constants'!
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
pi
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   114
    "return the constant pi as Fraction"
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   115
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   116
    ^ self 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   117
        numerator:  31415926535897932384626434
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   118
        denominator:10000000000000000000000000
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   119
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   120
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   121
     Fraction pi         
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   122
     Fraction pi asFloat
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   123
     Float pi            
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   124
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   125
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   126
    "Modified: 5.11.1996 / 11:11:44 / cg"
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   127
!
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
unity
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   130
    "return the neutral element for multiplication (1 / 1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   131
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   132
    ^ FractionOne
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   133
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   134
    "Modified: 18.7.1996 / 12:26:06 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   135
!
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
zero
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   138
    "return the neutral element for addition (0 / 1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   139
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   140
    ^ FractionZero
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   141
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   142
    "Modified: 18.7.1996 / 12:26:12 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   143
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   144
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   145
!Fraction class methodsFor:'queries'!
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   146
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   147
isBuiltInClass
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   148
    "return true if this class is known by the run-time-system.
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   149
     Here, true is returned for myself, false for subclasses."
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   150
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   151
    ^ self == Fraction
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   152
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   153
    "Modified: 23.4.1996 / 15:59:10 / cg"
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   154
! !
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   155
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   156
!Fraction methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   157
a27a279701f8 Initial revision
claus
parents:
diff changeset
   158
denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   159
    "return the denominator"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   160
a27a279701f8 Initial revision
claus
parents:
diff changeset
   161
    ^ denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   162
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   163
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   164
numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   165
    "return the numerator"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   166
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   167
    ^ numerator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   168
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   169
a27a279701f8 Initial revision
claus
parents:
diff changeset
   170
!Fraction methodsFor:'arithmetic'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   171
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   172
* aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   173
    "return the product of the receiver and the argument, aNumber"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   174
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   175
    |n d|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   176
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   177
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   178
        ^ (self class numerator:(numerator * aNumber)
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   179
                    denominator:denominator)
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 isFraction ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   182
        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
   183
            n := numerator * aNumber numerator.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   184
            d := denominator * aNumber denominator.
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   185
            ^ (self class numerator:n denominator:d)
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   186
        ]
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
    (aNumber isMemberOf:Float) ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   189
        ^ (numerator * aNumber) / denominator
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   190
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   191
    ^ aNumber productFromFraction:self
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   192
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   193
    "Modified: 28.7.1997 / 19:09:23 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   194
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   195
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   196
+ aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   197
    "return the sum of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   198
a27a279701f8 Initial revision
claus
parents:
diff changeset
   199
    |n d|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   200
a27a279701f8 Initial revision
claus
parents:
diff changeset
   201
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   202
        ^ (self class numerator:(numerator + (denominator * aNumber))
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   203
                    denominator:denominator)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   204
    ].
259
   205
    aNumber isFraction ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   206
        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
   207
            n := aNumber numerator.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   208
            d := aNumber denominator.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   209
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   210
            "save a multiplication if possible"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   211
            denominator == d ifTrue:[
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   212
                n := numerator + n
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   213
            ] ifFalse:[
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   214
                n := (numerator * d) + (n * denominator).
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   215
                d := denominator * d.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   216
            ].
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   217
            ^ (self class numerator:n denominator:d)
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   218
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   219
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   220
    (aNumber isMemberOf:Float) ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   221
        ^ aNumber + (numerator asFloat / denominator asFloat)
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   222
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   223
    ^ aNumber sumFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   224
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   225
    "Modified: 28.7.1997 / 19:09:16 / cg"
1
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 difference of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   230
a27a279701f8 Initial revision
claus
parents:
diff changeset
   231
    |n d|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   232
a27a279701f8 Initial revision
claus
parents:
diff changeset
   233
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   234
        ^ (self class numerator:(numerator - (denominator * aNumber))
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   235
                    denominator:denominator)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   236
    ].
259
   237
    aNumber isFraction ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   238
        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
   239
            n := aNumber numerator.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   240
            d := aNumber denominator.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   241
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   242
            "save a multiplication if possible"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   243
            denominator == d ifTrue:[
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   244
                n := numerator - n
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   245
            ] ifFalse:[
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   246
                n := (numerator * d) - (n * denominator).
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   247
                d := denominator * d
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   248
            ].
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   249
            ^ (self class numerator:n denominator:d)
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   250
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   251
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   252
    (aNumber isMemberOf:Float) ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   253
        ^ (numerator asFloat / denominator asFloat) - aNumber
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   254
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   255
    ^ aNumber differenceFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   256
1892
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
     (1/3) - (1/9)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   259
     (1/9) - (1/3)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   260
     (999/1000) - (1/1000)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   261
     (999/1000) - (1/1000000)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   262
     (999000/1000000) - (1/1000000)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   263
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   264
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   265
    "Modified: 28.7.1997 / 19:09:11 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   266
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   267
a27a279701f8 Initial revision
claus
parents:
diff changeset
   268
/ aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   269
    "return the quotient of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   270
a27a279701f8 Initial revision
claus
parents:
diff changeset
   271
    |n d|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   272
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   273
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   274
        ^ (self class numerator:numerator
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   275
                    denominator:(denominator * aNumber))
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   276
    ].
259
   277
    aNumber isFraction ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   278
        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
   279
            n := numerator * aNumber denominator.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   280
            d := denominator * aNumber numerator.
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   281
            ^ (self class numerator:n denominator:d)
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   282
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   283
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   284
    (aNumber isMemberOf:Float) ifTrue:[
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   285
        ^ numerator / (denominator * aNumber)
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   286
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   287
    ^ aNumber quotientFromFraction:self
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   288
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   289
    "Modified: 28.7.1997 / 19:09:06 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   290
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   291
a27a279701f8 Initial revision
claus
parents:
diff changeset
   292
// aNumber
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   293
    "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
   294
     truncation towards negative infinity."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   295
a27a279701f8 Initial revision
claus
parents:
diff changeset
   296
    ^ (numerator * aNumber denominator) // (denominator * aNumber numerator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   297
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   298
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   299
     0.5 // 1
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   300
     -0.5 // 1
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3899
diff changeset
   301
     (1/2) // 1  = 0 ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3899
diff changeset
   302
     (-1/2) // 1 = -1 ifFalse:[self halt].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   303
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   304
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3899
diff changeset
   305
    "Modified: / 5.11.1996 / 11:47:14 / cg"
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3899
diff changeset
   306
    "Modified: / 13.2.1998 / 09:15:35 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   307
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   308
a27a279701f8 Initial revision
claus
parents:
diff changeset
   309
negated
a27a279701f8 Initial revision
claus
parents:
diff changeset
   310
    "optional - could use inherited method ..."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   311
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   312
    ^ self class 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   313
        numerator:(numerator negated)
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   314
        denominator:denominator
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   315
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   316
    "Modified: 5.11.1996 / 10:29:11 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   317
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   318
a27a279701f8 Initial revision
claus
parents:
diff changeset
   319
reciprocal
a27a279701f8 Initial revision
claus
parents:
diff changeset
   320
    "optional - could use inherited method ..."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   321
a27a279701f8 Initial revision
claus
parents:
diff changeset
   322
    numerator == 1 ifTrue:[^ denominator].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   323
    ^ self class 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   324
        numerator:denominator
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   325
        denominator:numerator
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   326
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   327
    "Modified: 5.11.1996 / 10:29:22 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   328
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   329
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   330
!Fraction methodsFor:'coercing & converting'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   331
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   332
asFixedPoint
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   333
    "return the receiver as fixedPoint number.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   334
     Q: what should the scale be here ?"
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
    ^ FixedPoint numerator:numerator denominator:denominator scale:2
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   337
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
     (1/2) asFixedPoint
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   340
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   341
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   342
    "Created: 5.11.1996 / 15:15:54 / cg"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   343
!
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   344
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   345
asFixedPoint:scale
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   346
    "return the receiver as fixedPoint number, with the given number
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   347
     of post-decimal-point digits."
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   348
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   349
    ^ FixedPoint numerator:numerator denominator:denominator scale:scale
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   350
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   351
    "
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   352
     (1/2) asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   353
     (1/3) asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   354
     (1/3) asFixedPoint:5 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   355
     (2/3) asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   356
     (2/3) asFixedPoint:5 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   357
    "
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   358
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   359
    "Created: 5.11.1996 / 15:15:54 / cg"
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   360
    "Modified: 10.1.1997 / 19:54:50 / cg"
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   361
!
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   362
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   363
asFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   364
    "return a float with (approximately) my value"
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
    ^ (numerator asFloat) / (denominator asFloat)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   367
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   368
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   369
asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   370
    "return the receiver as fraction - thats itself"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   371
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   372
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   373
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   374
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   375
asInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   376
    "return an integer with my value - will usually truncate"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   377
a27a279701f8 Initial revision
claus
parents:
diff changeset
   378
    ^ numerator // denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   379
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   380
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   381
asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   382
    "return an integer with my value - will usually truncate"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   383
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   384
    ^ self asInteger asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   385
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   386
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   387
asShortFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   388
    "return a shortFloat with (approximately) my value"
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   389
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   390
    ^ (numerator asShortFloat) / (denominator asShortFloat)
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   391
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   392
    "Created: 17.4.1996 / 12:21:08 / cg"
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   393
!
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   394
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   395
coerce:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   396
    "return aNumber converted into receivers type"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   397
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   398
    ^ aNumber asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   399
!
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
generality
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   402
    "return the generality value - see ArithmeticValue>>retry:coercing:"
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
    ^ 60
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   405
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   406
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   407
!Fraction methodsFor:'comparing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   408
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   409
< aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   410
    "return true if the receiver is less
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   411
     than aNumber, false otherwise."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   412
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   413
    |d n|
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   414
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   415
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   416
        ^ numerator < (denominator * aNumber)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   417
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   418
    aNumber isFraction ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   419
        d := aNumber denominator.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   420
        n := aNumber numerator.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   421
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   422
        "/ save a multiplication if possible
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   423
        d == denominator ifTrue:[
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   424
            ^ numerator < n
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   425
        ].
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   426
        ^ (numerator * d) < (denominator * n)
701
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 lessFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   429
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   430
    "Modified: 5.11.1996 / 10:30:52 / cg"
701
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
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   433
= aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   434
    "return true, if the argument represents the same numeric value
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   435
     as the receiver, false otherwise"
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
    (aNumber isMemberOf:SmallInteger) ifTrue:[
3631
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   438
        (denominator = 1) ifFalse:[^ false].
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   439
        ^ numerator = aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   440
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   441
    aNumber isFraction ifTrue:[
3631
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   442
        (numerator = aNumber numerator) ifFalse:[^ false].
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   443
        ^ denominator = aNumber denominator
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   444
    ].
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   445
    (aNumber isInteger) ifTrue:[
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   446
        (denominator = 1) ifFalse:[^ false].
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   447
        ^ numerator = aNumber
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   448
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   449
    ^ self retry:#= coercing:aNumber
3631
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   450
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   451
    "Modified: / 7.7.1998 / 17:17:07 / cg"
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
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   454
> aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   455
    "return true if the receiver is greater
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   456
     than aNumber, false otherwise."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   457
    "optional - could use inherited method ..."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   458
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   459
    |d n|
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   460
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   461
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   462
        ^ numerator > (denominator * aNumber)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   463
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   464
    aNumber isFraction ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   465
        d := aNumber denominator.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   466
        n := aNumber numerator.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   467
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   468
        "/ save a multiplication if possible
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   469
        d == denominator ifTrue:[
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   470
            ^ numerator > n
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   471
        ].
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   472
        ^ (numerator * d) > (denominator * n)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   473
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   474
    ^ self retry:#> coercing:aNumber
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   475
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   476
    "Modified: 5.11.1996 / 10:31:28 / cg"
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   477
!
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   478
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   479
hash
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   480
    "return a number for hashing; redefined, since fractions compare
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   481
     by numeric value (i.e. (9/3) = 3), therefore (9/3) hash must be the same
4594
eb09f567a3bc float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents: 4593
diff changeset
   482
     as 3 hash."
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   483
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   484
    (denominator = 1) ifTrue:[^ numerator hash].
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   485
4594
eb09f567a3bc float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents: 4593
diff changeset
   486
    ^ self asFloat hash
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   487
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   488
    "
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   489
     3 hash           
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   490
     (9/3) hash       
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   491
     3.0 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   492
     (1/2) hash       
4594
eb09f567a3bc float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents: 4593
diff changeset
   493
     (1/4) hash       
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   494
     0.0 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   495
     0.5 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   496
     0.25 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   497
     0.4 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   498
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   499
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   500
a27a279701f8 Initial revision
claus
parents:
diff changeset
   501
!Fraction methodsFor:'double dispatching'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   502
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   503
differenceFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   504
    "sent when a float does not know how to subtract the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   505
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   506
    ^ (aFloat * denominator - numerator) / denominator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   507
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   508
a27a279701f8 Initial revision
claus
parents:
diff changeset
   509
differenceFromInteger:anInteger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   510
    "sent when an integer does not know how to subtract the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   511
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   512
    ^ (self class 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   513
        numerator:((anInteger * denominator) - numerator)
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   514
        denominator:denominator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   515
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   516
    "Modified: 28.7.1997 / 19:08:53 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   517
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   518
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   519
lessFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   520
    "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
   521
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   522
    ^ (denominator * anInteger) < numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   523
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   524
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   525
productFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   526
    "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
   527
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   528
    ^ aFloat * numerator / denominator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   529
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   530
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   531
productFromInteger:anInteger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   532
    "sent when an integer does not know how to multiply the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   533
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   534
    ^ (self class 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   535
        numerator:(anInteger * numerator)
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   536
        denominator:denominator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   537
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   538
    "Modified: 28.7.1997 / 19:06:22 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   539
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   540
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   541
quotientFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   542
    "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
   543
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   544
    ^ (aFloat * denominator) / numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   545
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   546
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   547
quotientFromInteger:anInteger
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   548
    "sent when an integer does not know how to divide by the receiver, a fraction"
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   549
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   550
    ^ (self class 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   551
        numerator:(anInteger * denominator)
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   552
        denominator:numerator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   553
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   554
    "Modified: 28.7.1997 / 19:08:46 / cg"
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   555
!
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   556
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   557
sumFromFloat:aFloat
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   558
    "sent when a float does not know how to add the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   559
a27a279701f8 Initial revision
claus
parents:
diff changeset
   560
    ^ (aFloat * denominator + numerator) / denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   561
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   562
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   563
sumFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   564
    "sent when an integer does not know how to add the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   565
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   566
    ^ (self class 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   567
        numerator:(numerator + (anInteger * denominator))
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   568
        denominator:denominator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   569
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   570
    "Modified: 28.7.1997 / 19:08:40 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   571
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   572
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   573
!Fraction methodsFor:'printing & storing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   574
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   575
printOn:aStream
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   576
    "append a printed representation of the receiver to the
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   577
     argument, aStream"
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   578
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   579
    aStream nextPut:$(.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   580
    numerator printOn:aStream.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   581
    aStream nextPut:$/.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   582
    denominator printOn:aStream.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   583
    aStream nextPut:$)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   584
! !
701
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:'private'!
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
reduced
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   589
    "reduce the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   590
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   591
    |gcd den|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   592
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   593
    den := denominator.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   594
    den < 0 ifTrue:[
2853
0647da37ba9a oops - care for negative denominator
ca
parents: 2792
diff changeset
   595
        numerator := numerator negated.
0647da37ba9a oops - care for negative denominator
ca
parents: 2792
diff changeset
   596
        den := denominator := den negated.
2789
e3e8707d26b4 make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   597
    ].
e3e8707d26b4 make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   598
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   599
    den == 1 ifTrue:[^ numerator].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   600
    numerator == 1 ifTrue:[^ self].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   601
    numerator == 0 ifTrue:[^ 0].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   602
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   603
    gcd := numerator gcd:den.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   604
    (gcd ~~ 1) ifTrue:[
2853
0647da37ba9a oops - care for negative denominator
ca
parents: 2792
diff changeset
   605
        numerator := numerator // gcd.
0647da37ba9a oops - care for negative denominator
ca
parents: 2792
diff changeset
   606
        denominator := den := den // gcd.
0647da37ba9a oops - care for negative denominator
ca
parents: 2792
diff changeset
   607
        den < 0 ifTrue:[
0647da37ba9a oops - care for negative denominator
ca
parents: 2792
diff changeset
   608
            numerator := numerator negated.
0647da37ba9a oops - care for negative denominator
ca
parents: 2792
diff changeset
   609
            den := denominator := den negated.
0647da37ba9a oops - care for negative denominator
ca
parents: 2792
diff changeset
   610
        ].
0647da37ba9a oops - care for negative denominator
ca
parents: 2792
diff changeset
   611
        (den == 1) ifTrue:[^ numerator].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   612
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   613
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   614
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   615
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   616
setNumerator:num denominator:den
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   617
    "set both numerator and denominator"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   618
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   619
    numerator := num.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   620
    denominator := den
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   621
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   622
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   623
!Fraction methodsFor:'queries'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   624
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   625
isFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   626
    "return true, if the receiver is some kind of fraction;
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   627
     true is returned here - the method is redefined from Object."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   628
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   629
    ^ true
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   630
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   631
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   632
!Fraction methodsFor:'testing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   633
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   634
negative
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   635
    "return true if the receiver is negative"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   636
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   637
    (numerator < 0) ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   638
	^ (denominator < 0) not
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   639
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   640
    ^ (denominator < 0)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   641
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   642
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   643
!Fraction methodsFor:'truncation and rounding'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   644
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   645
fractionPart
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   646
    "extract the after-decimal fraction part."
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   647
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   648
    numerator < denominator ifTrue:[
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   649
        ^ self
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   650
    ].
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   651
    ^ super fractionPart
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   652
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   653
    "
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   654
     (3/2) fractionPart 
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   655
     (2/3) fractionPart 
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   656
     ((3/2)*(15/4)) fractionPart   
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   657
     ((2/3)*(4/15)) fractionPart   
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   658
    "
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   659
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   660
    "Modified: / 28.10.1998 / 17:15:11 / cg"
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   661
!
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   662
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   663
integerPart
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   664
    "extract the pre-decimal integer part."
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   665
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   666
    numerator < denominator ifTrue:[
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   667
        ^ 0
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   668
    ].
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   669
    ^ super integerPart
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   670
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   671
    "
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   672
     (3/2) integerPart       
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   673
     (2/3) integerPart           
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   674
     ((3/2)*(15/4)) integerPart   
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   675
     ((2/3)*(4/15)) integerPart   
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   676
    "
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   677
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   678
    "Modified: / 28.10.1998 / 17:16:10 / cg"
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   679
!
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   680
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   681
rounded
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   682
    "return the receiver rounded to the nearest integer as integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   683
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   684
    "/ mhmh - what about -(1/2)
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   685
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   686
    |t|
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   687
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   688
    self negative ifTrue:[
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   689
        t := self - (1/2)
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   690
    ] ifFalse:[
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   691
        t := self + (1/2)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   692
    ].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   693
    ^ t truncated.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   694
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   695
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   696
     (1/3) rounded           
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   697
     (1/3) negated rounded     
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   698
     (1/2) rounded           
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   699
     (1/2) negated rounded   
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   700
     0.5 rounded  
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   701
     -0.5 rounded 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   702
     (2/3) rounded             
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   703
     (2/3) negated rounded     
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   704
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   705
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   706
    "Modified: 5.11.1996 / 11:32:32 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   707
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   708
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   709
truncated
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   710
    "return the receiver truncated towards zero as Integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   711
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   712
    ^ numerator quo: denominator
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   713
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   714
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   715
     (3/2) truncated     
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   716
     (3/2) negated truncated  
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   717
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   718
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   719
    "Modified: 5.11.1996 / 12:18:46 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   720
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   721
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   722
!Fraction class methodsFor:'documentation'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   723
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   724
version
4594
eb09f567a3bc float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents: 4593
diff changeset
   725
    ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.46 1999-08-12 08:58:20 cg Exp $'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   726
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   727
Fraction initialize!