Fraction.st
author Stefan Vogel <sv@exept.de>
Mon, 08 Apr 2002 12:22:13 +0200
changeset 6486 38ae7f67ee12
parent 6153 e92766db42c6
child 6571 101905a14870
permissions -rw-r--r--
Object coding
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
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
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
5552
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    13
"{ Package: 'stx:libbasic' }"
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    14
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    15
Number subclass:#Fraction
5552
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    16
	instanceVariableNames:'numerator denominator'
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    17
	classVariableNames:'FractionOne FractionZero'
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    18
	poolDictionaries:''
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    19
	category:'Magnitude-Numbers'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
    22
!Fraction class methodsFor:'documentation'!
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    23
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    24
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    25
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    26
 COPYRIGHT (c) 1989 by Claus Gittinger
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    27
              All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    30
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    36
!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    37
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    38
documentation
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    39
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    40
    Instances of Fraction represent fractional numbers consisting of
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    41
    a numerator and denominator. Both are themselfes arbitrary precision
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
    42
    integers. Fractions are usually created by dividing Integers using /
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    43
    (for exact division).
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    44
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
    45
    Mixed mode arithmetic:
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    46
        fraction op fraction    -> fraction
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    47
        fraction op fix         -> fix; scale is fix's scale
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    48
        fraction op integer     -> fraction
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    49
        fraction op float       -> float
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
    50
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    51
    [author:]
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    52
        Claus Gittinger
1556
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    53
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    54
    [see also:]
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    55
        Number
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    56
        Float Integer FixedPoint 
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    57
"
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    58
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    59
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
    60
!Fraction class methodsFor:'initialization'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    61
a27a279701f8 Initial revision
claus
parents:
diff changeset
    62
initialize
302
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
    63
    FractionZero isNil ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    64
        FractionZero := self numerator:0 denominator:1.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    65
        FractionOne := self numerator:1 denominator:1
302
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
    66
    ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    67
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
    68
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
    69
!Fraction class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    70
a27a279701f8 Initial revision
claus
parents:
diff changeset
    71
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
    72
    "create and return a new fraction with value 0"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    73
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
    74
    ^ self numerator:0 denominator:1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    75
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    76
a27a279701f8 Initial revision
claus
parents:
diff changeset
    77
numerator:num denominator:den
a27a279701f8 Initial revision
claus
parents:
diff changeset
    78
    "create and return a new fraction with numerator num and denominator den"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    79
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    80
    |newFraction|
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    81
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    82
%{  /* NOCONTEXT */
369
claus
parents: 359
diff changeset
    83
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
    84
    /* this check allows subclassing .. */
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    85
    if (self == Fraction) {
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    86
        if (__bothSmallInteger(num, den)) {
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    87
            if (__CanDoQuickAlignedNew(sizeof(struct __Fraction))) {    /* OBJECT ALLOCATION */
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    88
                OBJ newFraction;
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    89
                int spc;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    90
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    91
                __qCheckedAlignedNew(newFraction, sizeof(struct __Fraction));
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    92
                __InstPtr(newFraction)->o_class = self;
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    93
                if (__intVal(den) < 0) {
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    94
                    __FractionInstPtr(newFraction)->f_numerator = __MKSMALLINT(- __intVal(num));
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    95
                    __FractionInstPtr(newFraction)->f_denominator = __MKSMALLINT(- __intVal(den));
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    96
                } else {
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    97
                    __FractionInstPtr(newFraction)->f_numerator = num;
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    98
                    __FractionInstPtr(newFraction)->f_denominator = den;
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    99
                }
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   100
                if (num == __MKSMALLINT(1)) {
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   101
                    RETURN ( newFraction );
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   102
                }
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   103
            }
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   104
        }
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   105
    }
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   106
%}.
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   107
    newFraction isNil ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   108
        newFraction :=  self basicNew setNumerator:num denominator:den.
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   109
    ].
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   110
    ^ newFraction reduced
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   111
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   112
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   113
!Fraction class methodsFor:'constants'!
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   114
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   115
pi
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   116
    "return the constant pi as Fraction"
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
    ^ self 
6063
Claus Gittinger <cg@exept.de>
parents: 5552
diff changeset
   119
        numerator:  314159265358979323846264343
Claus Gittinger <cg@exept.de>
parents: 5552
diff changeset
   120
        denominator:100000000000000000000000000
Claus Gittinger <cg@exept.de>
parents: 5552
diff changeset
   121
Claus Gittinger <cg@exept.de>
parents: 5552
diff changeset
   122
"/    ^ self 
Claus Gittinger <cg@exept.de>
parents: 5552
diff changeset
   123
"/        numerator:  314159265358979323846264338327950288419716939937510582097494459
Claus Gittinger <cg@exept.de>
parents: 5552
diff changeset
   124
"/        denominator:100000000000000000000000000000000000000000000000000000000000000
1878
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
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   127
     Fraction pi         
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   128
     Fraction pi asFloat
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   129
     Float pi            
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   130
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   131
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   132
    "Modified: 5.11.1996 / 11:11:44 / cg"
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   133
!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   134
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   135
unity
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   136
    "return the neutral element for multiplication (1 / 1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   137
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   138
    ^ FractionOne
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   139
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   140
    "Modified: 18.7.1996 / 12:26:06 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   141
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   142
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   143
zero
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   144
    "return the neutral element for addition (0 / 1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   145
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   146
    ^ FractionZero
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   147
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   148
    "Modified: 18.7.1996 / 12:26:12 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   149
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   150
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   151
!Fraction class methodsFor:'queries'!
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   152
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   153
isBuiltInClass
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   154
    "return true if this class is known by the run-time-system.
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   155
     Here, true is returned for myself, false for subclasses."
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   156
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   157
    ^ self == Fraction
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   158
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   159
    "Modified: 23.4.1996 / 15:59:10 / cg"
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   160
! !
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   161
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   162
!Fraction methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   163
a27a279701f8 Initial revision
claus
parents:
diff changeset
   164
denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   165
    "return the denominator"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   166
a27a279701f8 Initial revision
claus
parents:
diff changeset
   167
    ^ denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   168
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   169
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   170
numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   171
    "return the numerator"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   172
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   173
    ^ numerator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   174
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   175
a27a279701f8 Initial revision
claus
parents:
diff changeset
   176
!Fraction methodsFor:'arithmetic'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   177
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   178
* aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   179
    "return the product of the receiver and the argument, aNumber"
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
    |n d|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   182
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   183
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   184
    "/ notice:
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   185
    "/ the following code handles some common cases,
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   186
    "/ and exists as an optimization, to speed up those cases.
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   187
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   188
    "/ Conceptionally, (and for most other argument types),
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   189
    "/ mixed arithmetic is implemented by double dispatching
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   190
    "/ (see the message send at the bottom)
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   191
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   192
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   193
    (aNumber isMemberOf:SmallInteger) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   194
        ^ (self class numerator:(numerator * aNumber)
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   195
                    denominator:denominator)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   196
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   197
    aNumber isFraction ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   198
        aNumber isFixedPoint ifFalse:[  "/ the value was corrent, but the scale is lost
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   199
            n := numerator * aNumber numerator.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   200
            d := denominator * aNumber denominator.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   201
            ^ (self class numerator:n denominator:d)
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   202
        ]
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   203
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   204
    (aNumber isMemberOf:Float) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   205
        ^ (numerator * aNumber) / denominator
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   206
    ].
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   207
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   208
    ^ aNumber productFromFraction:self
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   209
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   210
    "Modified: 28.7.1997 / 19:09:23 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   211
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   212
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   213
+ aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   214
    "return the sum of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   215
a27a279701f8 Initial revision
claus
parents:
diff changeset
   216
    |n d|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   217
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   218
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   219
    "/ notice:
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   220
    "/ the following code handles some common cases,
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   221
    "/ and exists as an optimization, to speed up those cases.
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   222
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   223
    "/ Conceptionally, (and for most other argument types),
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   224
    "/ mixed arithmetic is implemented by double dispatching
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   225
    "/ (see the message send at the bottom)
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   226
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   227
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   228
    (aNumber isMemberOf:SmallInteger) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   229
        ^ (self class numerator:(numerator + (denominator * aNumber))
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   230
                    denominator:denominator)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   231
    ].
259
   232
    aNumber isFraction ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   233
        aNumber isFixedPoint ifFalse:[  "/ the value was corrent, but the scale is lost
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   234
            n := aNumber numerator.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   235
            d := aNumber denominator.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   236
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   237
            "save a multiplication if possible"
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   238
            denominator == d ifTrue:[
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   239
                n := numerator + n
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   240
            ] ifFalse:[
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   241
                n := (numerator * d) + (n * denominator).
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   242
                d := denominator * d.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   243
            ].
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   244
            ^ (self class numerator:n denominator:d)
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   245
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   246
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   247
    (aNumber isMemberOf:Float) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   248
        ^ aNumber + (numerator asFloat / denominator asFloat)
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   249
    ].
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   250
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   251
    ^ aNumber sumFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   252
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   253
    "Modified: 28.7.1997 / 19:09:16 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   254
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   255
a27a279701f8 Initial revision
claus
parents:
diff changeset
   256
- aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   257
    "return the difference of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   258
a27a279701f8 Initial revision
claus
parents:
diff changeset
   259
    |n d|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   260
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   261
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   262
    "/ notice:
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   263
    "/ the following code handles some common cases,
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   264
    "/ and exists as an optimization, to speed up those cases.
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   265
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   266
    "/ Conceptionally, (and for most other argument types),
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   267
    "/ mixed arithmetic is implemented by double dispatching
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   268
    "/ (see the message send at the bottom)
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   269
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   270
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   271
    (aNumber isMemberOf:SmallInteger) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   272
        ^ (self class numerator:(numerator - (denominator * aNumber))
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   273
                    denominator:denominator)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   274
    ].
259
   275
    aNumber isFraction ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   276
        aNumber isFixedPoint ifFalse:[  "/ the value was corrent, but the scale is lost
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   277
            n := aNumber numerator.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   278
            d := aNumber denominator.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   279
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   280
            "save a multiplication if possible"
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   281
            denominator == d ifTrue:[
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   282
                n := numerator - n
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   283
            ] ifFalse:[
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   284
                n := (numerator * d) - (n * denominator).
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   285
                d := denominator * d
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   286
            ].
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   287
            ^ (self class numerator:n denominator:d)
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   288
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   289
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   290
    (aNumber isMemberOf:Float) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   291
        ^ (numerator asFloat / denominator asFloat) - aNumber
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   292
    ].
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   293
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   294
    ^ aNumber differenceFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   295
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   296
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   297
     (1/3) - (1/9)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   298
     (1/9) - (1/3)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   299
     (999/1000) - (1/1000)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   300
     (999/1000) - (1/1000000)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   301
     (999000/1000000) - (1/1000000)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   302
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   303
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   304
    "Modified: 28.7.1997 / 19:09:11 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   305
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   306
a27a279701f8 Initial revision
claus
parents:
diff changeset
   307
/ aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   308
    "return the quotient of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   309
a27a279701f8 Initial revision
claus
parents:
diff changeset
   310
    |n d|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   311
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   312
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   313
    "/ notice:
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   314
    "/ the following code handles some common cases,
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   315
    "/ and exists as an optimization, to speed up those cases.
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   316
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   317
    "/ Conceptionally, (and for most other argument types),
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   318
    "/ mixed arithmetic is implemented by double dispatching
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   319
    "/ (see the message send at the bottom)
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   320
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   321
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   322
    (aNumber isMemberOf:SmallInteger) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   323
        ^ (self class numerator:numerator
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   324
                    denominator:(denominator * aNumber))
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   325
    ].
259
   326
    aNumber isFraction ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   327
        aNumber isFixedPoint ifFalse:[  "/ the value was corrent, but the scale is lost
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   328
            n := numerator * aNumber denominator.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   329
            d := denominator * aNumber numerator.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   330
            ^ (self class numerator:n denominator:d)
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   331
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   332
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   333
    (aNumber isMemberOf:Float) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   334
        ^ numerator / (denominator * aNumber)
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   335
    ].
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   336
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   337
    ^ aNumber quotientFromFraction:self
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   338
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   339
    "Modified: 28.7.1997 / 19:09:06 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   340
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   341
a27a279701f8 Initial revision
claus
parents:
diff changeset
   342
// aNumber
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   343
    "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
   344
     truncation towards negative infinity."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   345
a27a279701f8 Initial revision
claus
parents:
diff changeset
   346
    ^ (numerator * aNumber denominator) // (denominator * aNumber numerator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   347
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   348
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   349
     0.5 // 1
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   350
     -0.5 // 1
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3899
diff changeset
   351
     (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
   352
     (-1/2) // 1 = -1 ifFalse:[self halt].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   353
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   354
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3899
diff changeset
   355
    "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
   356
    "Modified: / 13.2.1998 / 09:15:35 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   357
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   358
a27a279701f8 Initial revision
claus
parents:
diff changeset
   359
negated
a27a279701f8 Initial revision
claus
parents:
diff changeset
   360
    "optional - could use inherited method ..."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   361
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   362
    ^ self class 
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   363
        numerator:(numerator negated)
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   364
        denominator:denominator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   365
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   366
    "Modified: 5.11.1996 / 10:29:11 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   367
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   368
a27a279701f8 Initial revision
claus
parents:
diff changeset
   369
reciprocal
a27a279701f8 Initial revision
claus
parents:
diff changeset
   370
    "optional - could use inherited method ..."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   371
a27a279701f8 Initial revision
claus
parents:
diff changeset
   372
    numerator == 1 ifTrue:[^ denominator].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   373
    ^ self class 
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   374
        numerator:denominator
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   375
        denominator:numerator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   376
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   377
    "Modified: 5.11.1996 / 10:29:22 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   378
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   379
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   380
!Fraction methodsFor:'coercing & converting'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   381
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   382
asFixedPoint
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   383
    "return the receiver as fixedPoint number.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   384
     Q: what should the scale be here ?"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   385
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   386
    ^ FixedPoint numerator:numerator denominator:denominator scale:2
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   387
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   388
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   389
     (1/2) asFixedPoint
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   390
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   391
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   392
    "Created: 5.11.1996 / 15:15:54 / cg"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   393
!
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   394
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   395
asFixedPoint:scale
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   396
    "return the receiver as fixedPoint number, with the given number
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   397
     of post-decimal-point digits."
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   398
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   399
    ^ FixedPoint numerator:numerator denominator:denominator scale:scale
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   400
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   401
    "
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   402
     (1/2) asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   403
     (1/3) asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   404
     (1/3) asFixedPoint:5 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   405
     (2/3) asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   406
     (2/3) asFixedPoint:5 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   407
    "
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   408
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   409
    "Created: 5.11.1996 / 15:15:54 / cg"
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   410
    "Modified: 10.1.1997 / 19:54:50 / cg"
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   411
!
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   412
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   413
asFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   414
    "return a float with (approximately) my value"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   415
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   416
    ^ (numerator asFloat) / (denominator asFloat)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   417
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   418
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   419
asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   420
    "return the receiver as fraction - thats itself"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   421
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   422
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   423
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   424
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   425
asInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   426
    "return an integer with my value - will usually truncate"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   427
a27a279701f8 Initial revision
claus
parents:
diff changeset
   428
    ^ numerator // denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   429
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   430
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   431
asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   432
    "return an integer with my value - will usually truncate"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   433
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   434
    ^ self asInteger asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   435
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   436
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   437
asShortFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   438
    "return a shortFloat with (approximately) my value"
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   439
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   440
    ^ (numerator asShortFloat) / (denominator asShortFloat)
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   441
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   442
    "Created: 17.4.1996 / 12:21:08 / cg"
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   443
!
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   444
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   445
coerce:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   446
    "return aNumber converted into receivers type"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   447
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   448
    ^ aNumber asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   449
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   450
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   451
generality
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   452
    "return the generality value - see ArithmeticValue>>retry:coercing:"
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
    ^ 60
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   455
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   456
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   457
!Fraction methodsFor:'comparing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   458
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   459
< aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   460
    "return true if the receiver is less
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   461
     than aNumber, false otherwise."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   462
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   463
    |d n|
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   464
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   465
    (aNumber isMemberOf:SmallInteger) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   466
        ^ numerator < (denominator * aNumber)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   467
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   468
    aNumber isFraction ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   469
        d := aNumber denominator.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   470
        n := aNumber numerator.
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   471
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   472
        "/ save a multiplication if possible
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   473
        d == denominator ifTrue:[
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   474
            ^ numerator < n
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   475
        ].
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   476
        ^ (numerator * d) < (denominator * n)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   477
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   478
    ^ aNumber lessFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   479
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   480
    "Modified: 5.11.1996 / 10:30:52 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   481
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   482
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   483
= aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   484
    "return true, if the argument represents the same numeric value
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   485
     as the receiver, false otherwise"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   486
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   487
    (aNumber isMemberOf:SmallInteger) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   488
        (denominator = 1) ifFalse:[^ false].
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   489
        ^ numerator = aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   490
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   491
    aNumber isFraction ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   492
        denominator = aNumber denominator ifTrue:[
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   493
            ^ numerator = aNumber numerator
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   494
        ].
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   495
        ^ self sameFractionValueAs:aNumber
3631
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   496
    ].
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   497
    (aNumber isInteger) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   498
        (denominator = 1) ifFalse:[^ false].
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   499
        ^ numerator = aNumber
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   500
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   501
    ^ self retry:#= coercing:aNumber
3631
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   502
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   503
    "Modified: / 7.7.1998 / 17:17:07 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   504
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   505
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   506
> aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   507
    "return true if the receiver is greater
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   508
     than aNumber, false otherwise."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   509
    "optional - could use inherited method ..."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   510
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   511
    |d n|
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   512
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   513
    (aNumber isMemberOf:SmallInteger) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   514
        ^ numerator > (denominator * aNumber)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   515
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   516
    aNumber isFraction ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   517
        d := aNumber denominator.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   518
        n := aNumber numerator.
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   519
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   520
        "/ save a multiplication if possible
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   521
        d == denominator ifTrue:[
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   522
            ^ numerator > n
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   523
        ].
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   524
        ^ (numerator * d) > (denominator * n)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   525
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   526
    ^ self retry:#> coercing:aNumber
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   527
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   528
    "Modified: 5.11.1996 / 10:31:28 / cg"
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   529
!
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   530
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   531
hash
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   532
    "return a number for hashing; redefined, since fractions compare
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   533
     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
   534
     as 3 hash."
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   535
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   536
    (denominator = 1) ifTrue:[^ numerator hash].
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   537
4594
eb09f567a3bc float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents: 4593
diff changeset
   538
    ^ self asFloat hash
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   539
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   540
    "
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   541
     3 hash           
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   542
     (9/3) hash       
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   543
     3.0 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   544
     (1/2) hash       
4594
eb09f567a3bc float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents: 4593
diff changeset
   545
     (1/4) hash       
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   546
     0.0 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   547
     0.5 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   548
     0.25 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   549
     0.4 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   550
    "
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   551
!
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   552
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   553
sameFractionValueAs:aNumber
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   554
    "return true, if the argument represents the same numeric value
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   555
     as the receiver, false otherwise"
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   556
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   557
    |rSelf rNum|
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   558
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   559
    rSelf := self reduced.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   560
    rNum := aNumber reduced.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   561
    rSelf denominator = rNum denominator ifTrue:[
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   562
        ^ rSelf numerator = rNum numerator
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   563
    ].
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   564
    ^ false
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   565
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   566
a27a279701f8 Initial revision
claus
parents:
diff changeset
   567
!Fraction methodsFor:'double dispatching'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   568
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   569
differenceFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   570
    "sent when a float does not know how to subtract the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   571
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   572
    ^ (aFloat * denominator - numerator) / denominator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   573
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   574
a27a279701f8 Initial revision
claus
parents:
diff changeset
   575
differenceFromInteger:anInteger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   576
    "sent when an integer does not know how to subtract the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   577
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   578
    ^ (self class 
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   579
        numerator:((anInteger * denominator) - numerator)
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   580
        denominator:denominator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   581
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   582
    "Modified: 28.7.1997 / 19:08:53 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   583
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   584
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   585
lessFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   586
    "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
   587
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   588
    ^ (denominator * anInteger) < numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   589
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   590
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   591
productFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   592
    "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
   593
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   594
    ^ aFloat * numerator / denominator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   595
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   596
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   597
productFromInteger:anInteger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   598
    "sent when an integer does not know how to multiply the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   599
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   600
    ^ (self class 
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   601
        numerator:(anInteger * numerator)
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   602
        denominator:denominator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   603
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   604
    "Modified: 28.7.1997 / 19:06:22 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   605
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   606
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   607
quotientFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   608
    "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
   609
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   610
    ^ (aFloat * denominator) / numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   611
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   612
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   613
quotientFromInteger:anInteger
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   614
    "sent when an integer does not know how to divide by the receiver, a fraction"
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   615
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   616
    ^ (self class 
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   617
        numerator:(anInteger * denominator)
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   618
        denominator:numerator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   619
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   620
    "Modified: 28.7.1997 / 19:08:46 / cg"
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   621
!
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   622
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   623
sumFromFloat:aFloat
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   624
    "sent when a float does not know how to add the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   625
a27a279701f8 Initial revision
claus
parents:
diff changeset
   626
    ^ (aFloat * denominator + numerator) / denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   627
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   628
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   629
sumFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   630
    "sent when an integer does not know how to add the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   631
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   632
    ^ (self class 
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   633
        numerator:(numerator + (anInteger * denominator))
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   634
        denominator:denominator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   635
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   636
    "Modified: 28.7.1997 / 19:08:40 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   637
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   638
6486
38ae7f67ee12 Object coding
Stefan Vogel <sv@exept.de>
parents: 6153
diff changeset
   639
!Fraction methodsFor:'encoding'!
38ae7f67ee12 Object coding
Stefan Vogel <sv@exept.de>
parents: 6153
diff changeset
   640
38ae7f67ee12 Object coding
Stefan Vogel <sv@exept.de>
parents: 6153
diff changeset
   641
encodeOn:anEncoder with:aParameter
38ae7f67ee12 Object coding
Stefan Vogel <sv@exept.de>
parents: 6153
diff changeset
   642
38ae7f67ee12 Object coding
Stefan Vogel <sv@exept.de>
parents: 6153
diff changeset
   643
    anEncoder encodeFraction:self with:aParameter
38ae7f67ee12 Object coding
Stefan Vogel <sv@exept.de>
parents: 6153
diff changeset
   644
! !
38ae7f67ee12 Object coding
Stefan Vogel <sv@exept.de>
parents: 6153
diff changeset
   645
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   646
!Fraction methodsFor:'printing & storing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   647
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   648
printOn:aStream
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   649
    "append a printed representation of the receiver to the
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   650
     argument, aStream"
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   651
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   652
    aStream nextPut:$(.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   653
    numerator printOn:aStream.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   654
    aStream nextPut:$/.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   655
    denominator printOn:aStream.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   656
    aStream nextPut:$)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   657
! !
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   658
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   659
!Fraction methodsFor:'private'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   660
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   661
reduced
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   662
    "reduce the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   663
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   664
    |gcd den|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   665
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   666
    den := denominator.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   667
    den < 0 ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   668
        numerator := numerator negated.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   669
        den := denominator := den negated.
2789
e3e8707d26b4 make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   670
    ].
e3e8707d26b4 make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   671
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   672
    den == 1 ifTrue:[^ numerator].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   673
    numerator == 1 ifTrue:[^ self].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   674
    numerator == 0 ifTrue:[^ 0].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   675
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   676
    gcd := numerator gcd:den.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   677
    (gcd ~~ 1) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   678
        numerator := numerator // gcd.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   679
        denominator := den := den // gcd.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   680
        den < 0 ifTrue:[
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   681
            numerator := numerator negated.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   682
            den := denominator := den negated.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   683
        ].
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   684
        (den == 1) ifTrue:[^ numerator].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   685
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   686
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   687
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   688
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   689
setNumerator:num denominator:den
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   690
    "set both numerator and denominator"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   691
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   692
    numerator := num.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   693
    denominator := den
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   694
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   695
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   696
!Fraction methodsFor:'queries'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   697
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   698
isFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   699
    "return true, if the receiver is some kind of fraction;
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   700
     true is returned here - the method is redefined from Object."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   702
    ^ true
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   703
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   704
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   705
!Fraction methodsFor:'testing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   706
4658
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
   707
isLiteral
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
   708
    "return true, if the receiver can be used as a literal constant in ST syntax
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
   709
     (i.e. can be used in constant arrays)"
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
   710
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
   711
    ^ true
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
   712
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
   713
!
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
   714
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   715
negative
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   716
    "return true if the receiver is negative"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   717
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   718
    (numerator < 0) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   719
        ^ (denominator < 0) not
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
    ^ (denominator < 0)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   722
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   723
5552
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
   724
!Fraction methodsFor:'truncation & rounding'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   725
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   726
fractionPart
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   727
    "extract the after-decimal fraction part."
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   728
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
   729
    numerator abs < denominator abs ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   730
        ^ self
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   731
    ].
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   732
    ^ super fractionPart
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   733
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   734
    "
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   735
     (3/2) fractionPart 
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
   736
     (-3/2) fractionPart     
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   737
     (2/3) fractionPart 
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   738
     ((3/2)*(15/4)) fractionPart   
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   739
     ((2/3)*(4/15)) fractionPart   
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   740
    "
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   741
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
   742
    "Modified: / 5.11.2001 / 17:55:25 / cg"
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   743
!
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   744
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   745
integerPart
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   746
    "extract the pre-decimal integer part."
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   747
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
   748
    numerator abs < denominator abs ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   749
        ^ 0
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   750
    ].
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   751
    ^ super integerPart
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   752
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   753
    "
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
   754
     (3/2) integerPart        
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
   755
     (-3/2) integerPart        
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   756
     (2/3) integerPart           
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   757
     ((3/2)*(15/4)) integerPart   
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   758
     ((2/3)*(4/15)) integerPart   
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   759
    "
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   760
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
   761
    "Modified: / 5.11.2001 / 17:55:01 / cg"
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   762
!
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
   763
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   764
rounded
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   765
    "return the receiver rounded to the nearest integer as integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   766
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   767
    "/ mhmh - what about -(1/2)
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   768
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   769
    |t|
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   770
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   771
    self negative ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   772
        t := self - (1/2)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   773
    ] ifFalse:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   774
        t := self + (1/2)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   775
    ].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   776
    ^ t truncated.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   777
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   778
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   779
     (1/3) rounded           
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   780
     (1/3) negated rounded     
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   781
     (1/2) rounded           
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   782
     (1/2) negated rounded   
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   783
     0.5 rounded  
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   784
     -0.5 rounded 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   785
     (2/3) rounded             
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   786
     (2/3) negated rounded     
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   787
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   788
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   789
    "Modified: 5.11.1996 / 11:32:32 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   790
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   791
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   792
truncated
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   793
    "return the receiver truncated towards zero as Integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   794
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   795
    ^ numerator quo: denominator
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   796
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   797
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   798
     (3/2) truncated     
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   799
     (3/2) negated truncated  
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   800
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   801
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   802
    "Modified: 5.11.1996 / 12:18:46 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   803
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   804
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   805
!Fraction class methodsFor:'documentation'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   806
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   807
version
6486
38ae7f67ee12 Object coding
Stefan Vogel <sv@exept.de>
parents: 6153
diff changeset
   808
    ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.54 2002-04-08 10:22:13 stefan Exp $'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   809
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   810
Fraction initialize!