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