Fraction.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 24281 69518cfc5366
child 24931 843e3aaea193
permissions -rw-r--r--
#REFACTORING by exept class: Smalltalk class changed: #recursiveInstallAutoloadedClassesFrom:rememberIn:maxLevels:noAutoload:packageTop:showSplashInLevels: Transcript showCR:(... bindWith:...) -> Transcript showCR:... with:...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24201
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
     1
"{ Encoding: utf8 }"
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
     2
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     3
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     4
 COPYRIGHT (c) 1989 by Claus Gittinger
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
     5
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
"
5552
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    14
"{ Package: 'stx:libbasic' }"
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    15
18839
aa7721c46f4e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
    16
"{ NameSpace: Smalltalk }"
aa7721c46f4e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
    17
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
Number subclass:#Fraction
5552
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    19
	instanceVariableNames:'numerator denominator'
21974
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
    20
	classVariableNames:'FractionOne FractionZero PrintWholeNumbers Pi Pi_1000 E'
5552
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    21
	poolDictionaries:''
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    22
	category:'Magnitude-Numbers'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    23
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    24
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
    25
!Fraction class methodsFor:'documentation'!
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    26
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    27
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
 COPYRIGHT (c) 1989 by Claus Gittinger
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
    30
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    36
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    37
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    38
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    39
!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    40
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    41
documentation
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    42
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    43
    Instances of Fraction represent fractional numbers consisting of
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    44
    a numerator and denominator. Both are themselfes arbitrary precision
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
    45
    integers.
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    46
    Fractions are usually created by dividing Integers using / (for exact division).
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    47
    Notice, that all operations on fractions reduce their result; this means, that
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    48
    the result of a fraction-operation may return an integer.
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    49
    Aka:
21766
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    50
        (1 / 7) * 7   ->  1  (not 0.99999999...)
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    51
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
    52
    Mixed mode arithmetic:
21766
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    53
        fraction op fraction    -> fraction/integer
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    54
        fraction op fix         -> fix; scale is fix's scale
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    55
        fraction op integer     -> fraction/integer
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    56
        fraction op float       -> float
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
    57
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    58
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    59
    [classVariables:]
21766
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    60
        PrintWholeNumbers       Boolean        experimental:
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    61
                                                controls how fractions which are greater than 1 are printed.
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    62
                                                if true, print them as a sum of an integral and the fractional part.
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    63
                                                (Large ones are easier to read this way)
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    64
                                                     (17/3) printString  -> '(5+(2/3))'
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    65
                                                for now, the default is false, for backward compatibility
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    66
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    67
    [author:]
21766
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    68
        Claus Gittinger
1556
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    69
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    70
    [see also:]
21766
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    71
        Number
4974e66281d4 #OTHER by mawalch
mawalch
parents: 20416
diff changeset
    72
        FixedPoint Float ShortFloat LongFloat Integer Complex
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    73
"
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    74
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    75
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
    76
!Fraction class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    77
a27a279701f8 Initial revision
claus
parents:
diff changeset
    78
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
    79
    "create and return a new fraction with value 0"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    80
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
    81
    ^ self numerator:0 denominator:1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    82
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    83
a27a279701f8 Initial revision
claus
parents:
diff changeset
    84
numerator:num denominator:den
8629
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
    85
    "create and return a new fraction with numerator num and denominator den.
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
    86
     Notice: stc inlines this message if sent to the global named Fraction."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    87
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    88
    |newFraction|
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    89
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    90
%{  /* NOCONTEXT */
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18239
diff changeset
    91
#ifdef __SCHTEAM__
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
    92
    if (self == Fraction.Class) {
24135
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
    93
        return context._RETURN(new STFraction(num, den));
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
    94
    }
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
    95
#else
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
    96
    /* this check allows subclassing .. */
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    97
    if (self == Fraction) {
24135
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
    98
        if (__bothSmallInteger(num, den)) {
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
    99
            if (den != __mkSmallInteger(0)) {
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   100
                if (__CanDoQuickNew(sizeof(struct __Fraction))) {    /* OBJECT ALLOCATION */
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   101
                    OBJ newFraction;
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   102
                    INT iDen;
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   103
                    INT iNum;
19259
9e95248432bd #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19248
diff changeset
   104
24135
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   105
                    __qCheckedNew(newFraction, sizeof(struct __Fraction));
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   106
                    __InstPtr(newFraction)->o_class = self;
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   107
                    __qSTORE(newFraction, self);
19259
9e95248432bd #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19248
diff changeset
   108
24135
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   109
                    iDen = __intVal(den);
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   110
                    iNum = __intVal(num);
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   111
24135
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   112
                    if (iDen < 0) {
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   113
                        iNum = -iNum;
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   114
                        iDen = -iDen;
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   115
                    }
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   116
                    while ( (((iNum | iDen) & 1) == 0) && ( iNum != 0) && ( iDen != 0)) {
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   117
                        /* both even and non-zero */
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   118
                        iNum = iNum >> 1;
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   119
                        iDen = iDen >> 1;
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   120
                    }
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   121
                    if (iNum >= _MAX_INT) {
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   122
                        __FractionInstPtr(newFraction)->f_numerator = __MKINT(iNum);
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   123
                    } else {
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   124
                        __FractionInstPtr(newFraction)->f_numerator = __MKSMALLINT(iNum);
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   125
                    }
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   126
                    if (iDen >= _MAX_INT) {
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   127
                        __FractionInstPtr(newFraction)->f_denominator = __MKINT(iDen);
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   128
                    } else {
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   129
                        __FractionInstPtr(newFraction)->f_denominator = __MKSMALLINT(iDen);
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   130
                    }
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   131
                    if (iNum == 1) {
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   132
                        /* no need to reduce */
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   133
                        RETURN ( newFraction );
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   134
                    }
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   135
                }
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   136
            }
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   137
        }
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   138
    }
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18239
diff changeset
   139
#endif /* not __SCHTEAM__ */
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   140
%}.
8629
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
   141
    den = 0 ifTrue:[
24135
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   142
        ^ ZeroDivide raiseRequestWith:thisContext.
7140
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   143
    ].
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   144
    newFraction isNil ifTrue:[
24135
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   145
        newFraction := self basicNew setNumerator:num denominator:den.
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   146
    ].
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   147
    ^ newFraction reduced
7140
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   148
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   149
    "
24135
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   150
     Fraction numerator:1 denominator:3    -> (1/3)
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   151
     Fraction numerator:-1 denominator:3   -> (-1/3)
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   152
     Fraction numerator:1 denominator:-3   -> (-1/3)
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   153
     Fraction numerator:-1 denominator:-3  -> (1/3)
1172bf7d7292 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22895
diff changeset
   154
20308
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   155
     Fraction numerator:2 denominator:3
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   156
     Fraction numerator:2 denominator:6
19259
9e95248432bd #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19248
diff changeset
   157
9e95248432bd #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19248
diff changeset
   158
     Fraction numerator:1 denominator:0  -> error
9e95248432bd #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19248
diff changeset
   159
     Fraction numerator:2 denominator:0  -> error
7140
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   160
20308
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   161
     Fraction numerator:5 denominator:10
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   162
     Fraction numerator:50 denominator:100
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   163
     Fraction numerator:8 denominator:16
7140
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   164
    "
19259
9e95248432bd #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19248
diff changeset
   165
19261
02cb0c66a1b8 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19259
diff changeset
   166
    "Modified: / 27-02-2016 / 00:25:47 / cg"
11899
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   167
!
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   168
16938
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   169
readDecimalFractionFrom:aStringOrStream onError:exceptionBlock
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   170
    "Read an arbitrary number (>0) of digits representing a decimal fraction."
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   171
22895
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   172
    |numDigits factor fraction s ch denom|
16938
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   173
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   174
    s := aStringOrStream isStream ifTrue:[aStringOrStream] ifFalse:[aStringOrStream readStream].
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   175
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   176
    factor := (1 / 10).
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   177
    fraction := 0.
22895
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   178
    numDigits := 0.
16938
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   179
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   180
    [
22895
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   181
        ch := s peekOrNil.
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   182
        ch notNil and:[ch isDigit].
16938
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   183
    ] whileTrue: [
22895
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   184
        s next.
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   185
        fraction := fraction*10 + (ch digitValue).
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   186
        numDigits := numDigits + 1.
16938
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   187
    ].
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   188
22895
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   189
    numDigits > 0 ifFalse:[^ exceptionBlock valueWithOptionalArgument: 'Missing digits in fraction'].
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   190
    numDigits <= 10 ifTrue:[
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   191
        denom := #(10 100 1000 10000 100000
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   192
                   1000000 10000000 100000000 1000000000 10000000000) at:numDigits.
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   193
    ] ifFalse:[
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   194
        denom := 10 raisedTo:numDigits.
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   195
    ].
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
   196
    ^ fraction / denom
16938
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   197
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   198
    "
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   199
     Fraction readDecimalFractionFrom:'1'   onError:[nil]     -> 0.1
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   200
     Fraction readDecimalFractionFrom:'123' onError:[nil]     -> 0.123
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   201
     Fraction readDecimalFractionFrom:'5'   onError:[nil]     -> 0.5
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   202
     Fraction readDecimalFractionFrom:'005' onError:[nil]     -> 0.005
16938
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   203
     Fraction readDecimalFractionFrom:''    onError:[nil]     -> nil
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   204
     Fraction readDecimalFractionFrom:'aa'  onError:[nil]     -> nil
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   205
    "
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   206
!
ffe3202e45bd class: Fraction
Claus Gittinger <cg@exept.de>
parents: 16729
diff changeset
   207
11899
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   208
readFrom:aStringOrStream onError:exceptionBlock
22607
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   209
    |s numerator denominator hasParen result|
11899
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   210
11901
285349acebd8 readFrom
Claus Gittinger <cg@exept.de>
parents: 11899
diff changeset
   211
    "/ sigh - care for subclasses...
285349acebd8 readFrom
Claus Gittinger <cg@exept.de>
parents: 11899
diff changeset
   212
    self == Fraction ifFalse:[
22607
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   213
        ^ super readFrom:aStringOrStream onError:exceptionBlock
11901
285349acebd8 readFrom
Claus Gittinger <cg@exept.de>
parents: 11899
diff changeset
   214
    ].
285349acebd8 readFrom
Claus Gittinger <cg@exept.de>
parents: 11899
diff changeset
   215
11899
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   216
    s := aStringOrStream readStream.
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   217
    s skipSeparators.
22607
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   218
    hasParen := s peekFor:$(.
11899
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   219
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   220
    numerator := super readFrom:s onError:[^ exceptionBlock value].
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   221
    numerator isInteger ifTrue:[
22607
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   222
        s skipSeparators.
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   223
        (s peekFor:$/) ifTrue:[
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   224
            denominator := Integer readFrom:s onError:[^ exceptionBlock value].
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   225
            result := self numerator:numerator denominator:denominator
22608
78704fb221f9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22607
diff changeset
   226
        ] ifFalse:[
78704fb221f9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22607
diff changeset
   227
            result := numerator.
22607
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   228
        ].
11899
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   229
    ].
22607
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   230
    hasParen ifTrue:[
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   231
        s skipSeparators.
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   232
        (s peekFor:$)) ifFalse:exceptionBlock.
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   233
    ].
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   234
    result notNil ifTrue:[
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   235
        ^ result.
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   236
    ].
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   237
    ^ numerator asFraction.
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   238
11899
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   239
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   240
    "
22607
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   241
     Fraction readFromString:'1'
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   242
     Fraction readFromString:'2'
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   243
     Fraction readFromString:'1.5'
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   244
     Fraction readFromString:'1/5'
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   245
     Fraction readFromString:'(1/5)'
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   246
     Fraction readFromString:'(1/5'
11899
ad777208fdb5 added fraction-readFrom
Claus Gittinger <cg@exept.de>
parents: 11734
diff changeset
   247
    "
22607
4eb32d774d9c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22254
diff changeset
   248
22608
78704fb221f9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22607
diff changeset
   249
    "Modified: / 14-03-2018 / 19:02:44 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   250
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   251
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   252
!Fraction class methodsFor:'class initialization'!
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   253
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   254
initialize
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   255
    FractionZero isNil ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   256
	FractionZero := self numerator:0 denominator:1.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   257
	FractionOne := self numerator:1 denominator:1
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   258
    ]
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   259
! !
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   260
21819
c62073747a0a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21766
diff changeset
   261
!Fraction class methodsFor:'coercing & converting'!
c62073747a0a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21766
diff changeset
   262
c62073747a0a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21766
diff changeset
   263
coerce:aNumber
c62073747a0a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21766
diff changeset
   264
    "convert the argument aNumber into an instance of the receiver's class and return it."
c62073747a0a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21766
diff changeset
   265
c62073747a0a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21766
diff changeset
   266
    ^ aNumber asFraction
c62073747a0a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21766
diff changeset
   267
! !
c62073747a0a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21766
diff changeset
   268
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   269
!Fraction class methodsFor:'constants'!
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   270
21974
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   271
e
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   272
    "return an approximation of the constant e as Fraction.
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   273
     The approx. returned here has an error smaller than representable by float instances
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   274
     (roughly 26 valid digits)"
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   275
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   276
    E isNil ifTrue:[
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   277
        E := self
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   278
                numerator:  271828182845904523536028747 
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   279
                denominator:100000000000000000000000000
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   280
    ].
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   281
    ^ E
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   282
    
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   283
    "E := nil
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   284
    
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   285
     Fraction e
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   286
     Fraction e asFloat - Float e
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   287
     Fraction e asLongFloat - LongFloat e
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   288
     Float e
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   289
     FixedPoint e
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   290
    "
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   291
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   292
    "Created: / 03-07-2017 / 17:22:47 / cg"
24265
2bc155f7d89b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24201
diff changeset
   293
    "Modified (comment): / 06-06-2019 / 17:10:56 / Claus Gittinger"
21974
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   294
!
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   295
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   296
e_approximation
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   297
    "return an approximation of e as Fraction.
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   298
     The approx. returned is good for 6 valid digits and has an error of less than -2.67-07.
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   299
     The value might be useful to avoid floating point numbers in graphic rendering code,
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   300
     where 6 digits of precision are usually good enough."
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   301
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   302
    ^ self
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   303
        numerator:67957
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   304
        denominator:25000
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   305
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   306
    "
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   307
     Fraction e
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   308
     Fraction e asFloat               
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   309
     Fraction e_approximation asFloat 
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   310
     
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   311
     (Fraction e - Fraction e_approximation asFloat) abs
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   312
     (Float e - Fraction e_approximation asFloat) abs
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   313
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   314
     19/7 can be used as an approx with ~1% error:
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   315
         Float e - (19/7) asFloat
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   316
         
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   317
     87/32 is another candidate:
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   318
         Float e - (87/32) asFloat 
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   319
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   320
     and 106/39:
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   321
         Float e - (106/39) asFloat 
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   322
    "
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   323
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   324
    "Created: / 03-07-2017 / 17:21:38 / cg"
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   325
!
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   326
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   327
pi
11671
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   328
    "return an approximation of the constant pi as Fraction.
21944
5f6d5497f82a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21943
diff changeset
   329
     The approx. returned here has an error smaller than representable by float instances
5f6d5497f82a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21943
diff changeset
   330
     (roughly 26 valid digits)"
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   331
21942
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   332
    Pi isNil ifTrue:[
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   333
        Pi := self
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   334
            numerator:  314159265358979323846264343
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   335
            denominator:100000000000000000000000000
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   336
    ].
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   337
    ^ Pi
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   338
    
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   339
    "
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   340
     ^ self
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   341
        numerator:  314159265358979323846264338327950288419716939937510582097494459
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   342
        denominator:100000000000000000000000000000000000000000000000000000000000000
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   343
    "
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   344
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   345
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   346
     Fraction pi
13357
487c502b40b7 comment/format in: #pi
Claus Gittinger <cg@exept.de>
parents: 11901
diff changeset
   347
     Fraction pi asFloat - Float pi
19248
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   348
     Fraction pi asLongFloat - LongFloat pi
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   349
     Float pi
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   350
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   351
21944
5f6d5497f82a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21943
diff changeset
   352
    "Modified (comment): / 03-07-2017 / 13:21:03 / cg"
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   353
!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   354
19248
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   355
pi1000
21944
5f6d5497f82a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21943
diff changeset
   356
    "return an approximation of the constant pi as Fraction (>= 1000 valid decimal digits)."
19248
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   357
21942
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   358
    Pi_1000 isNil ifTrue:[
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   359
        Pi_1000 := self
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   360
                        numerator:  31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   361
                        denominator:10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.
19248
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   362
    ].
21942
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   363
    ^ Pi_1000
19248
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   364
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   365
    "
21942
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   366
     Pi_1000 := nil.
19248
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   367
     Fraction pi1000
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   368
     Fraction pi1000 asLongFloat - LongFloat pi
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   369
     LongFloat pi
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   370
    "
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   371
21944
5f6d5497f82a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21943
diff changeset
   372
    "Modified (comment): / 03-07-2017 / 13:21:15 / cg"
19248
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   373
!
a34488b9f1e6 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19241
diff changeset
   374
11671
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   375
pi_approximation
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   376
    "return an approximation of the constant pi as Fraction.
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   377
     The approx. returned is good for 6 valid digits and has an error of less than -2.67-07.
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   378
     The value might be useful to avoid floating point numbers in graphic rendering code,
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   379
     where 6 digits of precision are usually good enough."
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   380
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   381
    ^ self
21942
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   382
        numerator:355
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   383
        denominator:113
11671
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   384
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   385
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   386
     Fraction pi
11671
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   387
     Fraction pi asFloat
21942
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   388
     
21943
dbca6e4e1ba5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21942
diff changeset
   389
     (Fraction pi - Fraction pi_approximation asFloat) abs
21942
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   390
     (Float pi - Fraction pi_approximation asFloat) abs
21974
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   391
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   392
     22/7 can be used as an approx with 1% error:
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   393
         Float pi - (22/7) asFloat
11671
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   394
    "
21942
73cd95d3f948 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21819
diff changeset
   395
21974
cf76d289d886 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21944
diff changeset
   396
    "Modified (comment): / 03-07-2017 / 17:31:44 / cg"
11671
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   397
!
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   398
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   399
unity
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   400
    "return the neutral element for multiplication (1 / 1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   401
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   402
    ^ FractionOne
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   403
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   404
    "Modified: 18.7.1996 / 12:26:06 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   405
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   406
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   407
zero
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   408
    "return the neutral element for addition (0 / 1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   409
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   410
    ^ FractionZero
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   411
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   412
    "Modified: 18.7.1996 / 12:26:12 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   413
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   414
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   415
!Fraction class methodsFor:'queries'!
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   416
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   417
isBuiltInClass
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   418
    "return true if this class is known by the run-time-system.
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   419
     Here, true is returned for myself, false for subclasses."
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   420
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   421
    ^ self == Fraction
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   422
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   423
    "Modified: 23.4.1996 / 15:59:10 / cg"
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   424
! !
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   425
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   426
!Fraction methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   427
a27a279701f8 Initial revision
claus
parents:
diff changeset
   428
denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   429
    "return the denominator"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   430
a27a279701f8 Initial revision
claus
parents:
diff changeset
   431
    ^ denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   432
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   433
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   434
numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   435
    "return the numerator"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   436
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   437
    ^ numerator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   438
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   439
a27a279701f8 Initial revision
claus
parents:
diff changeset
   440
!Fraction methodsFor:'arithmetic'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   441
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   442
* aNumber
11734
7c2cc71aabc9 comment
Claus Gittinger <cg@exept.de>
parents: 11722
diff changeset
   443
    "return the product of the receiver and the argument."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   444
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   445
    "/ notice:
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   446
    "/ the following code handles some common cases,
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   447
    "/ and exists as an optimization, to speed up those cases.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   448
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   449
    "/ a message send; otherwise double-dispatch is just as fast.
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   450
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   451
    "/ Conceptionally, (and for most other argument types),
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   452
    "/ mixed arithmetic is implemented by double dispatching
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   453
    "/ (see the message send at the bottom)
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   454
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   455
    (aNumber isMemberOf:SmallInteger) ifTrue:[
22254
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   456
        ^ self class
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   457
                numerator:(numerator * aNumber)
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   458
                denominator:denominator
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   459
    ].
22254
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   460
    aNumber isFloat ifTrue:[
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   461
        ^ (numerator * aNumber) / denominator
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   462
    ].
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   463
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   464
    ^ aNumber productFromFraction:self
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   465
22254
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   466
    "
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   467
        2/3 * 3 asLongFloat
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   468
    "
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   469
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   470
    "Modified: / 28-07-1997 / 19:09:23 / cg"
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   471
    "Modified (comment): / 14-09-2017 / 15:27:30 / stefan"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   472
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   473
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   474
+ aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   475
    "return the sum of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   476
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   477
    "/ notice:
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   478
    "/ the following code handles some common cases,
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   479
    "/ and exists as an optimization, to speed up those cases.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   480
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   481
    "/ a message send; otherwise double-dispatch is just as fast.
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   482
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   483
    "/ Conceptionally, (and for most other argument types),
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   484
    "/ mixed arithmetic is implemented by double dispatching
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   485
    "/ (see the message send at the bottom)
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   486
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   487
    (aNumber isMemberOf:SmallInteger) ifTrue:[
22254
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   488
        ^ self class
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   489
            numerator:(numerator + (denominator * aNumber))
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   490
            denominator:denominator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   491
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   492
    (aNumber isMemberOf:Float) ifTrue:[
22254
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   493
        ^ (numerator asFloat / denominator asFloat) + aNumber
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   494
    ].
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   495
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   496
    ^ aNumber sumFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   497
22254
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   498
    "
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   499
        2/3 + 10 asLongFloat
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   500
    "
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   501
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   502
    "Modified: / 28-07-1997 / 19:09:16 / cg"
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   503
    "Modified (comment): / 14-09-2017 / 15:26:46 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   504
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   505
a27a279701f8 Initial revision
claus
parents:
diff changeset
   506
- aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   507
    "return the difference of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   508
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   509
    "/ notice:
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   510
    "/ the following code handles some common cases,
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   511
    "/ and exists as an optimization, to speed up those cases.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   512
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   513
    "/ a message send; otherwise double-dispatch is just as fast.
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   514
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   515
    "/ Conceptionally, (and for most other argument types),
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   516
    "/ mixed arithmetic is implemented by double dispatching
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   517
    "/ (see the message send at the bottom)
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   518
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   519
    (aNumber isMemberOf:SmallInteger) ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   520
	^ self class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   521
		numerator:(numerator - (denominator * aNumber))
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   522
		denominator:denominator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   523
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   524
    (aNumber isMemberOf:Float) ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   525
	^ (numerator asFloat / denominator asFloat) - aNumber
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   526
    ].
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   527
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   528
    ^ aNumber differenceFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   529
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   530
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   531
     (1/3) - (1/9)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   532
     (1/9) - (1/3)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   533
     (999/1000) - (1/1000)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   534
     (999/1000) - (1/1000000)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   535
     (999000/1000000) - (1/1000000)
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   536
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   537
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   538
    "Modified: 28.7.1997 / 19:09:11 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   539
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   540
a27a279701f8 Initial revision
claus
parents:
diff changeset
   541
/ aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   542
    "return the quotient of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   543
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   544
    "/ notice:
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   545
    "/ the following code handles some common cases,
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   546
    "/ and exists as an optimization, to speed up those cases.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   547
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   548
    "/ a message send; otherwise double-dispatch is just as fast.
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   549
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   550
    "/ Conceptionally, (and for most other argument types),
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   551
    "/ mixed arithmetic is implemented by double dispatching
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   552
    "/ (see the message send at the bottom)
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   553
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   554
    (aNumber isMemberOf:SmallInteger) ifTrue:[
22254
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   555
        ^ self class
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   556
                numerator:numerator
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   557
                denominator:(denominator * aNumber)
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   558
    ].
22254
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   559
    aNumber isFloat ifTrue:[
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   560
        ^ numerator / (denominator * aNumber)
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   561
    ].
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   562
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   563
    ^ aNumber quotientFromFraction:self
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   564
22254
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   565
    "Modified: / 28-07-1997 / 19:09:06 / cg"
30d452a7c004 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22099
diff changeset
   566
    "Modified: / 14-09-2017 / 15:24:48 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   567
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   568
a27a279701f8 Initial revision
claus
parents:
diff changeset
   569
// aNumber
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   570
    "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
   571
     truncation towards negative infinity."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   572
a27a279701f8 Initial revision
claus
parents:
diff changeset
   573
    ^ (numerator * aNumber denominator) // (denominator * aNumber numerator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   574
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   575
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   576
     0.5 // 1
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   577
     -0.5 // 1
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3899
diff changeset
   578
     (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
   579
     (-1/2) // 1 = -1 ifFalse:[self halt].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   580
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   581
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3899
diff changeset
   582
    "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
   583
    "Modified: / 13.2.1998 / 09:15:35 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   584
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   585
a27a279701f8 Initial revision
claus
parents:
diff changeset
   586
negated
a27a279701f8 Initial revision
claus
parents:
diff changeset
   587
    "optional - could use inherited method ..."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   588
18861
01abf9837590 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18839
diff changeset
   589
    "/ no need to reduce - I am already
01abf9837590 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18839
diff changeset
   590
    ^ self class basicNew
20308
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   591
	setNumerator:(numerator negated)
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   592
	denominator:denominator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   593
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   594
    "Modified: 5.11.1996 / 10:29:11 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   595
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   596
a27a279701f8 Initial revision
claus
parents:
diff changeset
   597
reciprocal
a27a279701f8 Initial revision
claus
parents:
diff changeset
   598
    "optional - could use inherited method ..."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   599
a27a279701f8 Initial revision
claus
parents:
diff changeset
   600
    numerator == 1 ifTrue:[^ denominator].
18861
01abf9837590 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18839
diff changeset
   601
    "/ no need to reduce - I am already
01abf9837590 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18839
diff changeset
   602
    ^ self class basicNew
20308
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   603
	setNumerator:denominator
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   604
	denominator:numerator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   605
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   606
    "Modified: 5.11.1996 / 10:29:22 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   607
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   608
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   609
!Fraction methodsFor:'coercing & converting'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   610
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   611
asFixedPoint
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   612
    "return the receiver as fixedPoint number.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   613
     Q: what should the scale be here ?"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   614
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   615
    ^ FixedPoint numerator:numerator denominator:denominator scale:2
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   616
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   617
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   618
     (1/2) asFixedPoint
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   619
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   620
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   621
    "Created: 5.11.1996 / 15:15:54 / cg"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   622
!
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   623
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   624
asFixedPoint:scale
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   625
    "return the receiver as fixedPoint number, with the given number
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   626
     of post-decimal-point digits."
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   627
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   628
    ^ FixedPoint numerator:numerator denominator:denominator scale:scale
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   629
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   630
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   631
     (1/2) asFixedPoint:2
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   632
     (1/3) asFixedPoint:2
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   633
     (1/3) asFixedPoint:5
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   634
     (2/3) asFixedPoint:2
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   635
     (2/3) asFixedPoint:5
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   636
    "
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   637
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   638
    "Created: 5.11.1996 / 15:15:54 / cg"
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   639
    "Modified: 10.1.1997 / 19:54:50 / cg"
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   640
!
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   641
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   642
asFloat
11239
951d0e92ab3d comment
Claus Gittinger <cg@exept.de>
parents: 10486
diff changeset
   643
    "return a float with (approximately) my value.
951d0e92ab3d comment
Claus Gittinger <cg@exept.de>
parents: 10486
diff changeset
   644
     Since floats have a limited precision, you usually loose bits when doing this."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   645
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   646
    |num den numShift denShift bits rslt|
6571
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   647
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   648
    (numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   649
	^ (numerator asFloat) / (denominator asFloat)
6571
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   650
    ].
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   651
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   652
    "Do it the hard way: reduce magnitude and undo reduction on the quotient"
6571
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   653
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   654
    bits := Float precision * 2.    "number of bits to preserve (conservative)"
6571
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   655
    num := numerator abs.
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   656
    numShift := bits - num highBit. "(num highBit - bits) negated"
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   657
    numShift < 0 ifTrue:[num := num bitShift:numShift] ifFalse:[numShift := 0].
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   658
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   659
    den :=  denominator.
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   660
    denShift := bits - den highBit. "(den highBit - bits) negated"
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   661
    denShift < 0 ifTrue:[den := den bitShift:denShift] ifFalse:[denShift := 0].
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   662
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   663
    rslt := (num asFloat / den asFloat) * (2 raisedToInteger:denShift-numShift).
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   664
    numerator negative ifTrue:[ ^ rslt negated ].
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   665
    ^ rslt.
6571
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   666
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   667
    "
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   668
      (5/9) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   669
      (-5/9) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   670
      (500000000000/900000000000) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   671
      (-500000000000/900000000000) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   672
      (500000000000/9) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   673
      (5/900000000000) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   674
      89012345678901234567 asFloat / 123456789123456789 asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   675
      (89012345678901234567 / 123456789123456789) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   676
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   677
      (
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   678
       180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   679
	/
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   680
       180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   681
      ) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   682
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   683
      180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   684
	 asFloat /
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   685
      180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   686
	 asFloat
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   687
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   688
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   689
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   690
asFraction
20416
dc0f91c47688 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20308
diff changeset
   691
    "return the receiver as fraction - that's the receiver itself"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   692
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   693
    ^ self
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
asInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   697
    "return an integer with my value - will usually truncate"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   698
a27a279701f8 Initial revision
claus
parents:
diff changeset
   699
    ^ numerator // denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   700
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   701
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   702
asLargeFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   703
    "return a large float with (approximately) my value"
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   704
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   705
    ^ (numerator asLargeFloat) / (denominator asLargeFloat)
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   706
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   707
    "
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   708
      (5/9) asLargeFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   709
      (500000000000/900000000000) asLargeFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   710
      (500000000000/9) asLargeFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   711
    "
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   712
!
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   713
24201
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   714
asLargeFloatPrecision:n
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   715
    "Answer a Floating point with arbitrary precision
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   716
     close to the receiver."
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   717
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   718
    "Note: form below would not be the closest approximation
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   719
    ^ (numerator asLargeFloatPrecision: n)
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   720
            inPlaceDivideBy: (denominator asLargeFloatPrecision: n)"
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   721
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   722
    ^ LargeFloat fromFraction:self precision:n
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   723
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   724
    "
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   725
      (5/9) asFloat 0.555555555555556
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   726
      (5/9) asLargeFloatPrecision:200 0.555556
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   727
      (5/9) asLargeFloat
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   728
      
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   729
      (500000000000/900000000000) asFloat * 900000000000 - 500000000000
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   730
      ((500000000000/900000000000) asLargeFloatPrecision:200) * 900000000000 - 500000000000
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   731
      (500000000000/900000000000) asLargeFloat * 900000000000 - 500000000000
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   732
      
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   733
      (500000000000/9) asLargeFloatPrecision:200
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   734
      (500000000000/9) asLargeFloat:200
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   735
    "
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   736
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   737
    "Created: / 27-05-2019 / 08:30:08 / Claus Gittinger"
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   738
    "Modified (comment): / 28-05-2019 / 06:04:48 / Claus Gittinger"
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   739
!
511a59fbb595 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24135
diff changeset
   740
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   741
asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   742
    "return an integer with my value - will usually truncate"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   743
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   744
    ^ self asInteger asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   745
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   746
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   747
asLongFloat
15278
ddf05cd996da class: Fraction
Claus Gittinger <cg@exept.de>
parents: 13357
diff changeset
   748
    "return a long float with (approximately) my value.
ddf05cd996da class: Fraction
Claus Gittinger <cg@exept.de>
parents: 13357
diff changeset
   749
     Since floats have a limited precision, you usually loose bits when doing this."
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   750
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   751
    |num den numShift denShift numBits rslt|
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   752
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   753
    (numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   754
	^ (numerator asLongFloat) / (denominator asLongFloat)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   755
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   756
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   757
    "Do it the hard way: reduce magnitude and undo reduction on the quotient"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   758
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   759
    numBits := LongFloat precision * 2.    "number of bits to preserve (conservative)"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   760
    num := numerator abs.
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   761
    numShift := numBits - num highBit. "(num highBit - bits) negated"
15278
ddf05cd996da class: Fraction
Claus Gittinger <cg@exept.de>
parents: 13357
diff changeset
   762
    numShift < 0 ifTrue:[num := num bitShift:numShift] ifFalse:[ numShift := 0].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   763
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   764
    den :=  denominator.
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   765
    denShift := numBits - den highBit. "(den highBit - bits) negated"
15278
ddf05cd996da class: Fraction
Claus Gittinger <cg@exept.de>
parents: 13357
diff changeset
   766
    denShift < 0 ifTrue:[den := den bitShift:denShift] ifFalse:[denShift := 0].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   767
7730
dda82553acc7 make #asLongFloat return a LongFloat (returned Float)
Stefan Vogel <sv@exept.de>
parents: 7571
diff changeset
   768
    rslt := (num asLongFloat / den asLongFloat) * (2 raisedToInteger:denShift-numShift).
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   769
    numerator negative ifTrue:[ ^ rslt negated ].
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   770
    ^ rslt.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   771
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   772
    "
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   773
      (5/9) asLongFloat
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   774
      (-5/9) asLongFloat
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   775
      (Fraction basicNew setNumerator:500000000000 denominator:900000000000) asLongFloat = (5/9) asLongFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   776
      (Fraction basicNew setNumerator:500000000001 denominator:900000000000) asLongFloat = (5/9) asLongFloat
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   777
      (500000000001/900000000000) asLongFloat
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   778
      (-500000000001/900000000000) asLongFloat
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   779
      (500000000001/900000000000) asLongFloat = (5/9) asLongFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   780
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   781
      (500000000000/9) asLongFloat
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   782
      (5/900000000000) asLongFloat
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   783
      89012345678901234567 asFloat / 123456789123456789 asLongFloat
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   784
      (89012345678901234567 / 123456789123456789) asLongFloat
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   785
      (-89012345678901234567 / 123456789123456789) asLongFloat
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   786
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   787
      (
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   788
       180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   789
	/
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   790
       180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   791
      ) asLongFloat
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   792
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   793
      180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   794
	 asLongFloat /
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   795
      180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   796
	 asLongFloat
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   797
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   798
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   799
24281
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   800
asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   801
    "return a QuadFloat with (approximately) my value.
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   802
     Since floats have a limited precision, you usually loose bits when doing this."
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   803
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   804
    |num den numShift denShift numBits rslt|
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   805
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   806
    (numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   807
        ^ (numerator asQuadFloat) / (denominator asQuadFloat)
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   808
    ].
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   809
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   810
    "Do it the hard way: reduce magnitude and undo reduction on the quotient"
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   811
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   812
    numBits := QuadFloat precision * 2.    "number of bits to preserve (conservative)"
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   813
    num := numerator abs.
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   814
    numShift := numBits - num highBit. "(num highBit - bits) negated"
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   815
    numShift < 0 ifTrue:[num := num bitShift:numShift] ifFalse:[ numShift := 0].
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   816
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   817
    den :=  denominator.
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   818
    denShift := numBits - den highBit. "(den highBit - bits) negated"
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   819
    denShift < 0 ifTrue:[den := den bitShift:denShift] ifFalse:[denShift := 0].
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   820
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   821
    rslt := (num asQuadFloat / den asQuadFloat) * (2 raisedToInteger:denShift-numShift).
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   822
    numerator negative ifTrue:[ ^ rslt negated ].
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   823
    ^ rslt.
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   824
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   825
    "
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   826
      (5/9) asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   827
      (-5/9) asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   828
      (Fraction basicNew setNumerator:500000000000 denominator:900000000000) asQuadFloat = (5/9) asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   829
      (Fraction basicNew setNumerator:500000000001 denominator:900000000000) asQuadFloat = (5/9) asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   830
      (500000000001/900000000000) asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   831
      (-500000000001/900000000000) asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   832
      (500000000001/900000000000) asQuadFloat = (5/9) asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   833
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   834
      (500000000000/9) asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   835
      (5/900000000000) asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   836
      89012345678901234567 asFloat / 123456789123456789 asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   837
      (89012345678901234567 / 123456789123456789) asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   838
      (-89012345678901234567 / 123456789123456789) asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   839
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   840
      (
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   841
       180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   842
        /
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   843
       180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   844
      ) asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   845
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   846
      180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   847
         asQuadFloat /
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   848
      180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   849
         asQuadFloat
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   850
    "
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   851
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   852
    "Created: / 07-06-2019 / 02:32:19 / Claus Gittinger"
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   853
!
69518cfc5366 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24265
diff changeset
   854
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   855
asShortFloat
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   856
    "return a short float with (approximately) my value"
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   857
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   858
    (numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   859
	^ (numerator asShortFloat) / (denominator asShortFloat)
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   860
    ].
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   861
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   862
    ^ self asFloat asShortFloat
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   863
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   864
    "
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   865
      (5/9) asShortFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   866
      (500000000000/900000000000) asShortFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   867
      (500000000000/9) asShortFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   868
    "
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   869
!
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   870
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   871
coerce:aNumber
18839
aa7721c46f4e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   872
    "convert the argument aNumber into an instance of the receiver's class and return it."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   873
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   874
    ^ aNumber asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   875
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   876
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   877
generality
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   878
    "return the generality value - see ArithmeticValue>>retry:coercing:"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   879
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   880
    ^ 60
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   881
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   882
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   883
!Fraction methodsFor:'comparing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   884
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   885
< aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   886
    "return true if the receiver is less
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   887
     than aNumber, false otherwise."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   888
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   889
    (aNumber isMemberOf:SmallInteger) ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   890
	^ numerator < (denominator * aNumber)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   891
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   892
    ^ aNumber lessFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   893
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   894
    "Modified: 5.11.1996 / 10:30:52 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   895
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   896
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   897
= aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   898
    "return true, if the argument represents the same numeric value
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   899
     as the receiver, false otherwise"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   900
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   901
    (aNumber isMemberOf:SmallInteger) ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   902
	(denominator == 1) ifFalse:[
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   903
	    ^ numerator = (aNumber * denominator)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   904
	].
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   905
	^ numerator = aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   906
    ].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   907
    ^ aNumber equalFromFraction:self
3631
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   908
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   909
    "Modified: / 7.7.1998 / 17:17:07 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   910
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   911
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   912
> aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   913
    "return true if the receiver is greater
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   914
     than aNumber, false otherwise."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   915
    "optional - could use inherited method ..."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   916
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   917
    (aNumber isMemberOf:SmallInteger) ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   918
	^ numerator > (denominator * aNumber)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   919
    ].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   920
    ^ aNumber < self
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   921
!
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   922
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   923
hash
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   924
    "return a number for hashing; redefined, since fractions compare
18861
01abf9837590 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18839
diff changeset
   925
     by numeric value (i.e. (1/2) = 0.5), hash values must be the same"
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   926
18861
01abf9837590 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18839
diff changeset
   927
    (denominator == 1) ifTrue:[^ numerator hash].
01abf9837590 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18839
diff changeset
   928
    (denominator == -1) ifTrue:[^ numerator hash negated].
4594
eb09f567a3bc float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents: 4593
diff changeset
   929
    ^ self asFloat hash
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   930
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   931
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   932
     3 hash
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   933
     (9/3) hash
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   934
     3.0 hash
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   935
     (1/2) hash
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   936
     (1/4) hash
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   937
     0.0 hash
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   938
     0.5 hash
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   939
     0.25 hash
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   940
     0.4 hash
20308
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   941
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   942
     0.25 hash
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   943
     -0.25 hash
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   944
     (1/4) hash
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
   945
     (-1/4) hash
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   946
    "
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   947
!
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   948
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   949
sameFractionValueAs:aNumber
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   950
    "return true, if the argument represents the same numeric value
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   951
     as the receiver, false otherwise"
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   952
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   953
    |rSelf rNum|
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   954
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   955
    rSelf := self reduced.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   956
    rNum := aNumber reduced.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   957
    rSelf denominator = rNum denominator ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   958
	^ rSelf numerator = rNum numerator
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   959
    ].
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   960
    ^ false
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   961
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   962
a27a279701f8 Initial revision
claus
parents:
diff changeset
   963
!Fraction methodsFor:'double dispatching'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   964
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   965
differenceFromFixedPoint:aFixedPoint
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   966
    |n d otherDenominator otherNumerator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   967
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   968
    otherDenominator := aFixedPoint denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   969
    otherNumerator := aFixedPoint numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   970
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   971
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   972
    otherDenominator == denominator ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   973
	n := otherNumerator - numerator.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   974
	d := otherDenominator.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   975
    ] ifFalse:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   976
	n := (otherNumerator * denominator) - (numerator * otherDenominator).
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   977
	d := otherDenominator * denominator.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   978
    ].
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   979
    ^ aFixedPoint class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   980
	numerator:n
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   981
	denominator:d
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   982
	scale:(aFixedPoint scale)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   983
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   984
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   985
     ((1/3) asFixedPoint:2) - (1/3)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
   986
     ((1/3) asFixedPoint:2) - (2/3)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   987
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   988
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   989
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   990
differenceFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   991
    "sent when a float does not know how to subtract the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   992
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   993
    ^ (aFloat * denominator - numerator) / denominator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   994
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   995
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   996
differenceFromFraction:aFraction
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   997
    |n d otherDenominator otherNumerator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   998
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   999
    otherDenominator := aFraction denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1000
    otherNumerator := aFraction numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1001
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1002
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1003
    otherDenominator == denominator ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1004
	n := otherNumerator - numerator.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1005
	d := otherDenominator.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1006
    ] ifFalse:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1007
	n := (otherNumerator * denominator) - (numerator * otherDenominator).
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1008
	d := otherDenominator * denominator.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1009
    ].
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1010
    ^ aFraction class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1011
	numerator:n
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1012
	denominator:d
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1013
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1014
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1015
     ((1/3) asFixedPoint:2) - (1/3)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1016
     ((1/3) asFixedPoint:2) - (2/3)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1017
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1018
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1019
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1020
differenceFromInteger:anInteger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
  1021
    "sent when an integer does not know how to subtract the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1022
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1023
    ^ self class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1024
	numerator:((anInteger * denominator) - numerator)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1025
	denominator:denominator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1026
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
  1027
    "Modified: 28.7.1997 / 19:08:53 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1028
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1029
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1030
equalFromFraction:aFraction
7571
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
  1031
    denominator = aFraction denominator ifFalse:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1032
	^ false   " must always be reduced "
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1033
	"/ ^ (numerator * aFraction denominator) = (aFraction numerator * denominator)
7571
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
  1034
    ].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1035
    ^ numerator = aFraction numerator
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1036
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1037
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1038
equalFromInteger:anInteger
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1039
    "sent when an integer does not know how to compare to the receiver, a fraction"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1040
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1041
    "as I am always reduced, this test should not be required.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1042
     However, it is here for subclasses and to allow comparing unnormalized fractions,
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1043
     which might be encountered internally"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1044
7571
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
  1045
    denominator = 1 ifFalse:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1046
	^ numerator = (anInteger * denominator)
7571
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
  1047
    ].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1048
    ^ numerator = anInteger
7571
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
  1049
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
  1050
    "
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
  1051
     1 = (1 asFixedPoint:1)
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
  1052
     (1 asFixedPoint:1) = 1
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
  1053
    "
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1054
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1055
10486
ecab71364dab new: #lessEqFromInteger:
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  1056
lessEqFromInteger:anInteger
ecab71364dab new: #lessEqFromInteger:
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  1057
    "sent when an integer does not know how to compare to the receiver, a fraction"
ecab71364dab new: #lessEqFromInteger:
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  1058
ecab71364dab new: #lessEqFromInteger:
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  1059
    ^ (denominator * anInteger) <= numerator
ecab71364dab new: #lessEqFromInteger:
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  1060
!
ecab71364dab new: #lessEqFromInteger:
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
  1061
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1062
lessFromFraction:aFraction
18861
01abf9837590 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18839
diff changeset
  1063
    "sent when a fraction does not know how to compare to the receiver.
01abf9837590 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18839
diff changeset
  1064
     Return true if aFraction < self."
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1065
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1066
    |n d|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1067
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1068
    d := aFraction denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1069
    n := aFraction numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1070
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1071
    "/ save a multiplication if possible
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1072
    d == denominator ifTrue:[
20308
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
  1073
	^ n < numerator
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1074
    ].
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1075
    ^ (denominator * n) < (numerator * d)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1076
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1077
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1078
lessFromInteger:anInteger
18861
01abf9837590 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18839
diff changeset
  1079
    "sent when an integer does not know how to compare to the receiver, a fraction.
01abf9837590 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18839
diff changeset
  1080
     Return true if anInteger < self."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1081
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1082
    ^ (denominator * anInteger) < numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1083
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1084
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1085
productFromFixedPoint:aFixedPoint
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1086
    ^ aFixedPoint class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1087
	numerator:(aFixedPoint numerator * numerator)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1088
	denominator:(aFixedPoint denominator * denominator)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1089
	scale:(aFixedPoint scale)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1090
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1091
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1092
     ((1/3) asFixedPoint:2) * 2
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1093
     ((1/3) asFixedPoint:2) * (1/2)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1094
     ((1/3) asFixedPoint:2) * (3/2)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1095
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1096
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1097
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1098
productFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1099
    "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
  1100
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1101
    ^ aFloat * numerator / denominator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1102
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1103
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1104
productFromFraction:aFraction
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1105
    ^ aFraction class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1106
	numerator:(aFraction numerator * numerator)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1107
	denominator:(aFraction denominator * denominator)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1108
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1109
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1110
     ((1/3) asFixedPoint:2) * 2
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1111
     ((1/3) asFixedPoint:2) * (1/2)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1112
     ((1/3) asFixedPoint:2) * (3/2)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1113
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1114
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1115
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1116
productFromInteger:anInteger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
  1117
    "sent when an integer does not know how to multiply the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1118
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1119
    ^ self class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1120
	numerator:(anInteger * numerator)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1121
	denominator:denominator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1122
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
  1123
    "Modified: 28.7.1997 / 19:06:22 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1124
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1125
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1126
quotientFromFixedPoint:aFixedPoint
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1127
    "Return the quotient of the argument, aFixedPoint and the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1128
     Sent when aFixedPoint does not know how to divide by the receiver."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1129
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1130
    ^ aFixedPoint class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1131
	numerator:(aFixedPoint numerator * denominator)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1132
	denominator:(aFixedPoint denominator * numerator)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1133
	scale:(aFixedPoint scale)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1134
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1135
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1136
     ((1/3) asFixedPoint:2) / 2
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1137
     ((1/3) asFixedPoint:2) / (1/2)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1138
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1139
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1140
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1141
quotientFromFloat:aFloat
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1142
    "Return the quotient of the argument, aFloat and the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1143
     Sent when aFloat does not know how to divide by the receiver."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1144
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1145
    ^ (aFloat * denominator) / numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1146
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1147
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1148
quotientFromFraction:aFraction
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1149
    "Return the quotient of the argument, aFraction and the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1150
     Sent when aFraction does not know how to divide by the receiver."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1151
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1152
    ^ aFraction class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1153
	numerator:(aFraction numerator * denominator)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1154
	denominator:(aFraction denominator * numerator)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1155
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1156
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1157
     (1/3) / (1/2)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1158
     (1/3) / (3/2)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1159
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1160
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1161
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
  1162
quotientFromInteger:anInteger
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1163
    "Return the quotient of the argument, anInteger and the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
  1164
     Sent when anInteger does not know how to divide by the receiver."
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
  1165
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1166
    ^ self class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1167
	numerator:(anInteger * denominator)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1168
	denominator:numerator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1169
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
  1170
    "Modified: 28.7.1997 / 19:08:46 / cg"
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
  1171
!
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
  1172
22099
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1173
raisedFromFloat:aFloat
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1174
    "aFloat does not know how to be raised to the receiver"
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1175
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1176
    numerator == 1 ifTrue:[
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1177
        ^ aFloat nthRoot:denominator
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1178
    ].
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1179
    ^ self asFloat raisedFromFloat:aFloat
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1180
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1181
    "
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1182
     100 raisedTo:(2/5) asFloat
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1183
     100 raisedTo:(1/5) asFloat
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1184
     
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1185
     100 nthRoot:2      10.0
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1186
     100 nthRoot:3      4.641588833612778893
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1187
     100 raisedTo:(1/3) 3.16227766016838
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1188
     100 raisedTo:(1/4) 3.16227766016838
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1189
     100 raisedTo:(1/5) 2.51188643150958
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1190
     100 raisedTo:(1/6) 2.15443469003188
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1191
     100 raisedTo:(1/7) 1.93069772888325
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1192
    "
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1193
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1194
    "Created: / 25-07-2017 / 16:13:26 / cg"
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1195
!
cc9d508cf69c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21974
diff changeset
  1196
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1197
sumFromFixedPoint:aFixedPoint
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1198
    |n d otherDenominator otherNumerator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1199
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1200
    otherDenominator := aFixedPoint denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1201
    otherNumerator := aFixedPoint numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1202
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1203
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1204
    otherDenominator == denominator ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1205
	n := otherNumerator + numerator.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1206
	d := otherDenominator.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1207
    ] ifFalse:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1208
	n := (otherNumerator * denominator) + (numerator * otherDenominator).
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1209
	d := otherDenominator * denominator.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1210
    ].
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1211
    ^ aFixedPoint class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1212
	numerator:n
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1213
	denominator:d
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1214
	scale:(aFixedPoint scale)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1215
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1216
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1217
     ((1/3) asFixedPoint:2) + (1/3)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1218
     ((1/3) asFixedPoint:2) + (2/3)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1219
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1220
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1221
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1222
sumFromFloat:aFloat
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
  1223
    "sent when a float does not know how to add the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1224
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1225
    ^ (aFloat * denominator + numerator) / denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1226
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1227
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1228
sumFromFraction:aFraction
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1229
    |n d otherDenominator otherNumerator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1230
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1231
    otherDenominator := aFraction denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1232
    otherNumerator := aFraction numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1233
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1234
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1235
    otherDenominator == denominator ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1236
	n := otherNumerator + numerator.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1237
	d := otherDenominator.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1238
    ] ifFalse:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1239
	n := (otherNumerator * denominator) + (numerator * otherDenominator).
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1240
	d := otherDenominator * denominator.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1241
    ].
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1242
    ^ aFraction class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1243
	numerator:n
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1244
	denominator:d
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1245
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1246
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1247
     (1/3) + (1/3)
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1248
     (1/3) + (2/3)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1249
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1250
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1251
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1252
sumFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1253
    "sent when an integer does not know how to add the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1254
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1255
    ^ self class
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1256
	numerator:(numerator + (anInteger * denominator))
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1257
	denominator:denominator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1258
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
  1259
    "Modified: 28.7.1997 / 19:08:40 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1260
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1261
22895
711322e64257 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22608
diff changeset
  1262
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1263
!Fraction methodsFor:'printing & storing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1264
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
  1265
printOn:aStream
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
  1266
    "append a printed representation of the receiver to the
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
  1267
     argument, aStream"
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
  1268
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
  1269
    |t|
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
  1270
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
  1271
    PrintWholeNumbers == true ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1272
	"/ experimental: print fractions which are greater than 1 as a sum of
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1273
	"/ an integral and the fractional part. They are easier to read this way.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1274
	numerator > denominator ifTrue:[
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1275
	    aStream nextPut:$(.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1276
	    t := numerator // denominator.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1277
	    t printOn:aStream.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1278
	    aStream nextPutAll:'+('.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1279
	    (numerator - (t*denominator)) printOn:aStream.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1280
	    aStream nextPut:$/.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1281
	    denominator printOn:aStream.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1282
	    aStream nextPutAll:'))'.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1283
	    ^ self
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1284
	].
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
  1285
    ].
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
  1286
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
  1287
    aStream nextPut:$(.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
  1288
    numerator printOn:aStream.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
  1289
    aStream nextPut:$/.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
  1290
    denominator printOn:aStream.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
  1291
    aStream nextPut:$)
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
  1292
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
  1293
    "Modified: / 31.7.2002 / 09:56:41 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1294
! !
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1295
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1296
!Fraction methodsFor:'private'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1297
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1298
reduced
7139
958ca5839e25 comment
Claus Gittinger <cg@exept.de>
parents: 6891
diff changeset
  1299
    "reduce the receiver; divide the numerator and denominator by their
958ca5839e25 comment
Claus Gittinger <cg@exept.de>
parents: 6891
diff changeset
  1300
     greatest common divisor; if the result is integral, return an Integer.
958ca5839e25 comment
Claus Gittinger <cg@exept.de>
parents: 6891
diff changeset
  1301
     Otherwise, return the normalized receiver.
11250
9a962a088d30 comment
Claus Gittinger <cg@exept.de>
parents: 11239
diff changeset
  1302
     CAVEAT: bad name; should be called reduce, as it has a side effect
9a962a088d30 comment
Claus Gittinger <cg@exept.de>
parents: 11239
diff changeset
  1303
     (i.e. this is destructive wrt. the instance values)."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1304
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
  1305
    |gcd den|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1306
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
  1307
    den := denominator.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
  1308
    den < 0 ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1309
	numerator := numerator negated.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1310
	den := denominator := den negated.
2789
e3e8707d26b4 make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1311
    ].
e3e8707d26b4 make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1312
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
  1313
    den == 1 ifTrue:[^ numerator].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1314
    numerator == 1 ifTrue:[^ self].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1315
    numerator == 0 ifTrue:[^ 0].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1316
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
  1317
    gcd := numerator gcd:den.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
  1318
    (gcd ~~ 1) ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1319
	gcd < 0 ifTrue:[
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1320
	     gcd := gcd negated.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1321
	].
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1322
	numerator := numerator // gcd.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1323
	denominator := den := den // gcd.
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1324
	(den == 1) ifTrue:[^ numerator].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1325
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1326
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1327
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1328
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1329
setNumerator:num denominator:den
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1330
    "set both numerator and denominator"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1331
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1332
    numerator := num.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1333
    denominator := den
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1334
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1335
6650
35de1d8400b2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6646
diff changeset
  1336
!Fraction methodsFor:'testing'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1337
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1338
isFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1339
    "return true, if the receiver is some kind of fraction;
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1340
     true is returned here - the method is redefined from Object."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1341
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1342
    ^ true
6650
35de1d8400b2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6646
diff changeset
  1343
!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1344
4658
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
  1345
isLiteral
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
  1346
    "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
  1347
     (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
  1348
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
  1349
    ^ true
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
  1350
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
  1351
!
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
  1352
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1353
negative
18861
01abf9837590 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18839
diff changeset
  1354
    "return true if the receiver is less than zero"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1355
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1356
    (numerator < 0) ifTrue:[
20308
9110f117d260 ALIGNMENT code clean up
Claus Gittinger <cg@exept.de>
parents: 19261
diff changeset
  1357
	^ (denominator < 0) not
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1358
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1359
    ^ (denominator < 0)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1360
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1361
5552
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
  1362
!Fraction methodsFor:'truncation & rounding'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1363
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1364
fractionPart
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1365
    "extract the after-decimal fraction part,
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1366
     such that (self truncated + self fractionPart) = self"
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1367
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
  1368
    numerator abs < denominator abs ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1369
	^ self
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1370
    ].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1371
    ^ (numerator rem: denominator) / denominator
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1372
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1373
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1374
     (3/2) fractionPart + (3/2) truncated
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1375
     (-3/2) fractionPart + (-3/2) truncated
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1376
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1377
     (3/2) fractionPart
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1378
     (-3/2) fractionPart
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1379
     (3/2) asFloat fractionPart
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1380
     (-3/2) asFloat fractionPart
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1381
     (2/3) fractionPart
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1382
     ((3/2)*(15/4)) fractionPart
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1383
     ((2/3)*(4/15)) fractionPart
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1384
    "
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1385
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
  1386
    "Modified: / 5.11.2001 / 17:55:25 / cg"
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1387
!
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1388
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1389
integerPart
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1390
    "extract the pre-decimal integer part."
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1391
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
  1392
    numerator abs < denominator abs ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1393
	^ 0
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1394
    ].
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1395
    ^ super integerPart
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1396
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1397
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1398
     (3/2) integerPart
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1399
     (-3/2) integerPart
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1400
     (2/3) integerPart
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1401
     ((3/2)*(15/4)) integerPart
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1402
     ((2/3)*(4/15)) integerPart
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1403
    "
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1404
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
  1405
    "Modified: / 5.11.2001 / 17:55:01 / cg"
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1406
!
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1407
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1408
rounded
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1409
    "return the receiver rounded to the nearest integer as integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1410
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1411
    "/ mhmh - what about -(1/2)
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1412
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1413
    |t|
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1414
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1415
    self negative ifTrue:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1416
	t := self - (1/2)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1417
    ] ifFalse:[
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1418
	t := self + (1/2)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1419
    ].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1420
    ^ t truncated.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1421
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1422
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1423
     (1/3) rounded
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1424
     (1/3) negated rounded
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1425
     (1/2) rounded
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1426
     (1/2) negated rounded
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1427
     0.5 rounded
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1428
     -0.5 rounded
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1429
     (2/3) rounded
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1430
     (2/3) negated rounded
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1431
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1432
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1433
    "Modified: 5.11.1996 / 11:32:32 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1434
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1435
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1436
truncated
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1437
    "return the receiver truncated towards zero as Integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1438
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1439
    ^ numerator quo: denominator
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1440
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1441
    "
18239
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1442
     (3/2) truncated
1f10572b7324 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16938
diff changeset
  1443
     (3/2) negated truncated
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1444
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1445
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1446
    "Modified: 5.11.1996 / 12:18:46 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1447
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1448
8395
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1449
!Fraction methodsFor:'visiting'!
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1450
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1451
acceptVisitor:aVisitor with:aParameter
16729
0cf2bfd45072 comment/format only
Claus Gittinger <cg@exept.de>
parents: 15278
diff changeset
  1452
    "dispatch for visitor pattern; send #visitFraction:with: to aVisitor"
8395
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1453
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1454
    ^ aVisitor visitFraction:self with:aParameter
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1455
! !
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1456
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1457
!Fraction class methodsFor:'documentation'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1458
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1459
version
18839
aa7721c46f4e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1460
    ^ '$Header$'
13357
487c502b40b7 comment/format in: #pi
Claus Gittinger <cg@exept.de>
parents: 11901
diff changeset
  1461
!
487c502b40b7 comment/format in: #pi
Claus Gittinger <cg@exept.de>
parents: 11901
diff changeset
  1462
487c502b40b7 comment/format in: #pi
Claus Gittinger <cg@exept.de>
parents: 11901
diff changeset
  1463
version_CVS
18839
aa7721c46f4e #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1464
    ^ '$Header$'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1465
! !
6891
212aa8576cf7 oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents: 6675
diff changeset
  1466
15278
ddf05cd996da class: Fraction
Claus Gittinger <cg@exept.de>
parents: 13357
diff changeset
  1467
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1468
Fraction initialize!