Fraction.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 28 Sep 2011 20:07:49 +0100
branchjv
changeset 17865 598963c6ff8e
parent 17846 24edc476ac18
child 17869 9610c6c94e71
permissions -rw-r--r--
Recommited from itself
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1989 by Claus Gittinger
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
     3
              All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
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
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    14
Number subclass:#Fraction
5552
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    15
	instanceVariableNames:'numerator denominator'
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    16
	classVariableNames:'FractionOne FractionZero PrintWholeNumbers'
5552
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    17
	poolDictionaries:''
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
    18
	category:'Magnitude-Numbers'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
    21
!Fraction class methodsFor:'documentation'!
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    22
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    23
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    24
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    25
 COPYRIGHT (c) 1989 by Claus Gittinger
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    26
              All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    27
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    36
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    37
documentation
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    38
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    39
    Instances of Fraction represent fractional numbers consisting of
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    40
    a numerator and denominator. Both are themselfes arbitrary precision
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    41
    integers. 
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    42
    Fractions are usually created by dividing Integers using / (for exact division).
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    43
    Notice, that all operations on fractions reduce their result; this means, that
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    44
    the result of a fraction-operation may return an integer.
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    45
    Aka:
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    46
        (1 / 7) * 7   ->  1  (not 0.99999999...)
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    47
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
    48
    Mixed mode arithmetic:
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    49
        fraction op fraction    -> fraction/integer
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    50
        fraction op fix         -> fix; scale is fix's scale
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    51
        fraction op integer     -> fraction/integer
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    52
        fraction op float       -> float
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
    53
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    54
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    55
    [classVariables:]
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    56
        PrintWholeNumbers       Booolean        experimental: 
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    57
                                                controls how fractions which are greater than 1 are printed.
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    58
                                                if true, print them as a sum of an integral and the fractional part. 
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    59
                                                (Large ones are easier to read this way)
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    60
                                                     (17/3) printString  -> '(5+(2/3))'  
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    61
                                                for now, the default is false, for backward compatibility
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
    62
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    63
    [author:]
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    64
        Claus Gittinger
1556
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    65
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1555
diff changeset
    66
    [see also:]
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    67
        Number
7380
c704ff45bb80 documentation
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
    68
        FixedPoint Float ShortFloat LongFloat Integer Complex
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    69
"
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    70
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    71
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
    72
!Fraction class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    73
a27a279701f8 Initial revision
claus
parents:
diff changeset
    74
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
    75
    "create and return a new fraction with value 0"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    76
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
    77
    ^ self numerator:0 denominator:1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    78
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    79
a27a279701f8 Initial revision
claus
parents:
diff changeset
    80
numerator:num denominator:den
8629
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
    81
    "create and return a new fraction with numerator num and denominator den.
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
    82
     Notice: stc inlines this message if sent to the global named Fraction."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    83
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    84
    |newFraction|
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
    85
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    86
%{  /* NOCONTEXT */
369
claus
parents: 359
diff changeset
    87
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
    88
    /* this check allows subclassing .. */
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    89
    if (self == Fraction) {
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
    90
        if (__bothSmallInteger(num, den)) {
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8629
diff changeset
    91
            if (den != __mkSmallInteger(0)) {
8629
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
    92
                if (__CanDoQuickAlignedNew(sizeof(struct __Fraction))) {    /* OBJECT ALLOCATION */
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
    93
                    OBJ newFraction;
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
    94
                    int spc;
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
    95
                    INT iDen;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    96
8629
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
    97
                    __qCheckedAlignedNew(newFraction, sizeof(struct __Fraction));
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
    98
                    __InstPtr(newFraction)->o_class = self;
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
    99
                    __qSTORE(newFraction, self);
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
   100
                    iDen = __intVal(den);
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
   101
                    if (iDen != 0) {
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
   102
                        if (iDen < 0) {
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8629
diff changeset
   103
                            __FractionInstPtr(newFraction)->f_numerator = __mkSmallInteger(- __intVal(num));
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8629
diff changeset
   104
                            __FractionInstPtr(newFraction)->f_denominator = __mkSmallInteger(- iDen);
8629
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
   105
                        } else {
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
   106
                            __FractionInstPtr(newFraction)->f_numerator = num;
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
   107
                            __FractionInstPtr(newFraction)->f_denominator = den;
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
   108
                        }
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8629
diff changeset
   109
                        if (num == __mkSmallInteger(1)) {
8629
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
   110
                            /* no need to reduce */
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
   111
                            RETURN ( newFraction );
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
   112
                        }
7140
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   113
                    }
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   114
                }
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   115
            }
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   116
        }
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   117
    }
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
   118
%}.
8629
07ece572135f care for zeroDivide
penk
parents: 8395
diff changeset
   119
    den = 0 ifTrue:[
7140
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   120
        ^ ZeroDivide raiseRequestWith:thisContext.
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   121
    ].
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   122
    newFraction isNil ifTrue:[
7140
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   123
        newFraction := self basicNew setNumerator:num denominator:den.
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   124
    ].
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   125
    ^ newFraction reduced
7140
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   126
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   127
    "
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   128
     Fraction numerator:1 denominator:3
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   129
     Fraction numerator:2 denominator:6
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   130
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   131
     Fraction numerator:1 denominator:0
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   132
     Fraction numerator:2 denominator:0
f65ec31fc081 oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents: 7139
diff changeset
   133
    "
17732
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   134
!
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   135
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   136
readFrom:aStringOrStream onError:exceptionBlock
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   137
    |s numerator denominator|
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   138
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   139
    "/ sigh - care for subclasses...
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   140
    self == Fraction ifFalse:[
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   141
        ^ super readFrom:aStringOrStream onError:exceptionBlock
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   142
    ].
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   143
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   144
    s := aStringOrStream readStream.
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   145
    s skipSeparators.
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   146
    s peek == $( ifTrue:[
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   147
        s next.
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   148
        
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   149
    ].
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   150
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   151
    numerator := super readFrom:s onError:[^ exceptionBlock value].
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   152
    numerator isInteger ifTrue:[
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   153
        s skipSeparators.
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   154
        (s peek == $/) ifTrue:[
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   155
            s next.
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   156
            denominator := Integer readFrom:s onError:[^ exceptionBlock value].
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   157
            ^ self numerator:numerator denominator:denominator
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   158
        ].
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   159
        ^ numerator
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   160
    ].
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   161
    ^ numerator asFraction
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   162
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   163
    "
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   164
     Fraction readFrom:'1'      
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   165
     Fraction readFrom:'2'      
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   166
     Fraction readFrom:'1.5'    
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   167
     Fraction readFrom:'1/5'    
a1892eeca6c0 trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17711
diff changeset
   168
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   169
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   170
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   171
!Fraction class methodsFor:'class initialization'!
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   172
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   173
initialize
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   174
    FractionZero isNil ifTrue:[
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   175
        FractionZero := self numerator:0 denominator:1.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   176
        FractionOne := self numerator:1 denominator:1
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   177
    ]
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   178
! !
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   179
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   180
!Fraction class methodsFor:'constants'!
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   181
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   182
pi
11671
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   183
    "return an approximation of the constant pi as Fraction.
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   184
     The approx. returned here has an error smaller than representable by float instances"
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   185
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   186
    ^ self 
11671
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   187
        numerator:314159265358979323846264343
6063
Claus Gittinger <cg@exept.de>
parents: 5552
diff changeset
   188
        denominator:100000000000000000000000000
Claus Gittinger <cg@exept.de>
parents: 5552
diff changeset
   189
Claus Gittinger <cg@exept.de>
parents: 5552
diff changeset
   190
"/    ^ self 
Claus Gittinger <cg@exept.de>
parents: 5552
diff changeset
   191
"/        numerator:  314159265358979323846264338327950288419716939937510582097494459
Claus Gittinger <cg@exept.de>
parents: 5552
diff changeset
   192
"/        denominator:100000000000000000000000000000000000000000000000000000000000000
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   193
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   194
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   195
     Fraction pi         
17841
7abcc4aef871 Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17761
diff changeset
   196
     Fraction pi asFloat - Float pi
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   197
     Float pi            
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   198
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   199
17841
7abcc4aef871 Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17761
diff changeset
   200
    "Modified: / 03-05-2011 / 11:08:46 / cg"
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   201
!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   202
11671
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   203
pi_approximation
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   204
    "return an approximation of the constant pi as Fraction.
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   205
     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
   206
     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
   207
     where 6 digits of precision are usually good enough."
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   208
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   209
    ^ self 
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   210
        numerator:355
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   211
        denominator:113
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   212
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   213
    "
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   214
     Fraction pi         
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   215
     Fraction pi asFloat
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   216
     Float pi - Fraction pi_approximation asFloat            
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   217
    "
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   218
!
6901343001b6 approx pi
Claus Gittinger <cg@exept.de>
parents: 11250
diff changeset
   219
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   220
unity
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   221
    "return the neutral element for multiplication (1 / 1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   222
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   223
    ^ FractionOne
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   224
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   225
    "Modified: 18.7.1996 / 12:26:06 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   226
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   227
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   228
zero
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   229
    "return the neutral element for addition (0 / 1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   230
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   231
    ^ FractionZero
1555
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   232
316491c1b216 commentary
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   233
    "Modified: 18.7.1996 / 12:26:12 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   234
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   235
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   236
!Fraction class methodsFor:'queries'!
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   237
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   238
isBuiltInClass
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   239
    "return true if this class is known by the run-time-system.
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   240
     Here, true is returned for myself, false for subclasses."
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   241
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   242
    ^ self == Fraction
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   243
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   244
    "Modified: 23.4.1996 / 15:59:10 / cg"
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   245
! !
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   246
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   247
!Fraction methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   248
a27a279701f8 Initial revision
claus
parents:
diff changeset
   249
denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   250
    "return the denominator"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   251
a27a279701f8 Initial revision
claus
parents:
diff changeset
   252
    ^ denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   253
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   254
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   255
numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   256
    "return the numerator"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   257
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   258
    ^ numerator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   259
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   260
a27a279701f8 Initial revision
claus
parents:
diff changeset
   261
!Fraction methodsFor:'arithmetic'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   262
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   263
* aNumber
11734
7c2cc71aabc9 comment
Claus Gittinger <cg@exept.de>
parents: 11722
diff changeset
   264
    "return the product of the receiver and the argument."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   265
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   266
    "/ notice:
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   267
    "/ the following code handles some common cases,
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   268
    "/ and exists as an optimization, to speed up those cases.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   269
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   270
    "/ 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
   271
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   272
    "/ Conceptionally, (and for most other argument types),
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   273
    "/ mixed arithmetic is implemented by double dispatching
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   274
    "/ (see the message send at the bottom)
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   275
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   276
    (aNumber isMemberOf:SmallInteger) ifTrue:[
6646
875d9668d82b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6576
diff changeset
   277
        ^ self class 
875d9668d82b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6576
diff changeset
   278
                numerator:(numerator * aNumber)
875d9668d82b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6576
diff changeset
   279
                denominator:denominator
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   280
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   281
    (aNumber isMemberOf:Float) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   282
        ^ (numerator * aNumber) / denominator
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   283
    ].
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   284
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   285
    ^ aNumber productFromFraction:self
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   286
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   287
    "Modified: 28.7.1997 / 19:09:23 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   288
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   289
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   290
+ aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   291
    "return the sum of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   292
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   293
    "/ notice:
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   294
    "/ the following code handles some common cases,
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   295
    "/ and exists as an optimization, to speed up those cases.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   296
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   297
    "/ 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
   298
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   299
    "/ Conceptionally, (and for most other argument types),
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   300
    "/ mixed arithmetic is implemented by double dispatching
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   301
    "/ (see the message send at the bottom)
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   302
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   303
    (aNumber isMemberOf:SmallInteger) ifTrue:[
6646
875d9668d82b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6576
diff changeset
   304
        ^ self class 
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   305
            numerator:(numerator + (denominator * aNumber))
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   306
            denominator:denominator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   307
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   308
    (aNumber isMemberOf:Float) ifTrue:[
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   309
        ^ (numerator asFloat / denominator asFloat) + aNumber
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   310
    ].
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   311
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   312
    ^ aNumber sumFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   313
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   314
    "Modified: 28.7.1997 / 19:09:16 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   315
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   316
a27a279701f8 Initial revision
claus
parents:
diff changeset
   317
- aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   318
    "return the difference of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   319
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   320
    "/ notice:
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   321
    "/ the following code handles some common cases,
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   322
    "/ and exists as an optimization, to speed up those cases.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   323
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   324
    "/ 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
   325
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   326
    "/ Conceptionally, (and for most other argument types),
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   327
    "/ mixed arithmetic is implemented by double dispatching
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   328
    "/ (see the message send at the bottom)
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   329
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   330
    (aNumber isMemberOf:SmallInteger) ifTrue:[
6646
875d9668d82b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6576
diff changeset
   331
        ^ self class 
875d9668d82b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6576
diff changeset
   332
                numerator:(numerator - (denominator * aNumber))
875d9668d82b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6576
diff changeset
   333
                denominator:denominator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   334
    ].
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   335
    (aNumber isMemberOf:Float) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   336
        ^ (numerator asFloat / denominator asFloat) - aNumber
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   337
    ].
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   338
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   339
    ^ aNumber differenceFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   340
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   341
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   342
     (1/3) - (1/9)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   343
     (1/9) - (1/3)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   344
     (999/1000) - (1/1000)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   345
     (999/1000) - (1/1000000)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   346
     (999000/1000000) - (1/1000000)      
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   347
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   348
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   349
    "Modified: 28.7.1997 / 19:09:11 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   350
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   351
a27a279701f8 Initial revision
claus
parents:
diff changeset
   352
/ aNumber
a27a279701f8 Initial revision
claus
parents:
diff changeset
   353
    "return the quotient of the receiver and the argument, aNumber"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   354
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   355
    "/ notice:
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   356
    "/ the following code handles some common cases,
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   357
    "/ and exists as an optimization, to speed up those cases.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   358
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   359
    "/ 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
   360
    "/
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   361
    "/ Conceptionally, (and for most other argument types),
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   362
    "/ mixed arithmetic is implemented by double dispatching
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   363
    "/ (see the message send at the bottom)
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   364
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   365
    (aNumber isMemberOf:SmallInteger) ifTrue:[
6646
875d9668d82b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6576
diff changeset
   366
        ^ self class 
875d9668d82b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6576
diff changeset
   367
                numerator:numerator
875d9668d82b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6576
diff changeset
   368
                denominator:(denominator * aNumber)
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   369
    ].
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   370
    (aNumber isMemberOf:Float) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   371
        ^ numerator / (denominator * aNumber)
16
a580032d04f6 *** empty log message ***
claus
parents: 13
diff changeset
   372
    ].
6064
04bde2eeb749 comments about double dispatching
Claus Gittinger <cg@exept.de>
parents: 6063
diff changeset
   373
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   374
    ^ aNumber quotientFromFraction:self
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   375
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   376
    "Modified: 28.7.1997 / 19:09:06 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   377
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   378
a27a279701f8 Initial revision
claus
parents:
diff changeset
   379
// aNumber
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   380
    "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
   381
     truncation towards negative infinity."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   382
a27a279701f8 Initial revision
claus
parents:
diff changeset
   383
    ^ (numerator * aNumber denominator) // (denominator * aNumber numerator)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   384
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   385
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   386
     0.5 // 1
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   387
     -0.5 // 1
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3899
diff changeset
   388
     (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
   389
     (-1/2) // 1 = -1 ifFalse:[self halt].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   390
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   391
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3899
diff changeset
   392
    "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
   393
    "Modified: / 13.2.1998 / 09:15:35 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   394
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   395
a27a279701f8 Initial revision
claus
parents:
diff changeset
   396
negated
a27a279701f8 Initial revision
claus
parents:
diff changeset
   397
    "optional - could use inherited method ..."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   398
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   399
    ^ self class 
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   400
        numerator:(numerator negated)
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   401
        denominator:denominator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   402
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   403
    "Modified: 5.11.1996 / 10:29:11 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   404
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   405
a27a279701f8 Initial revision
claus
parents:
diff changeset
   406
reciprocal
a27a279701f8 Initial revision
claus
parents:
diff changeset
   407
    "optional - could use inherited method ..."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   408
a27a279701f8 Initial revision
claus
parents:
diff changeset
   409
    numerator == 1 ifTrue:[^ denominator].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   410
    ^ self class 
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   411
        numerator:denominator
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   412
        denominator:numerator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   413
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   414
    "Modified: 5.11.1996 / 10:29:22 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   415
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   416
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   417
!Fraction methodsFor:'coercing & converting'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   418
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   419
asFixedPoint
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   420
    "return the receiver as fixedPoint number.
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   421
     Q: what should the scale be here ?"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   422
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   423
    ^ FixedPoint numerator:numerator denominator:denominator scale:2
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   424
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   425
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   426
     (1/2) asFixedPoint
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   427
    "
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   428
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   429
    "Created: 5.11.1996 / 15:15:54 / cg"
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   430
!
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1878
diff changeset
   431
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   432
asFixedPoint:scale
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   433
    "return the receiver as fixedPoint number, with the given number
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   434
     of post-decimal-point digits."
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   435
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   436
    ^ FixedPoint numerator:numerator denominator:denominator scale:scale
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   437
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   438
    "
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   439
     (1/2) asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   440
     (1/3) asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   441
     (1/3) asFixedPoint:5 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   442
     (2/3) asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   443
     (2/3) asFixedPoint:5 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   444
    "
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   445
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   446
    "Created: 5.11.1996 / 15:15:54 / cg"
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   447
    "Modified: 10.1.1997 / 19:54:50 / cg"
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   448
!
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   449
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   450
asFloat
11239
951d0e92ab3d comment
Claus Gittinger <cg@exept.de>
parents: 10486
diff changeset
   451
    "return a float with (approximately) my value.
951d0e92ab3d comment
Claus Gittinger <cg@exept.de>
parents: 10486
diff changeset
   452
     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
   453
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   454
    |num den numShift denShift bits rslt|
6571
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   455
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   456
    (numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   457
        ^ (numerator asFloat) / (denominator asFloat)
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   458
    ].
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   459
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   460
    "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
   461
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   462
    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
   463
    num := numerator abs.
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   464
    numShift := bits - num highBit. "(num highBit - bits) negated"
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   465
    numShift < 0 ifTrue:[num := num bitShift:numShift] ifFalse:[numShift := 0].
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   466
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   467
    den :=  denominator.
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   468
    denShift := bits - den highBit. "(den highBit - bits) negated"
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   469
    denShift < 0 ifTrue:[den := den bitShift:denShift] ifFalse:[denShift := 0].
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   470
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   471
    rslt := (num asFloat / den asFloat) * (2 raisedToInteger:denShift-numShift).
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   472
    numerator negative ifTrue:[ ^ rslt negated ].
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   473
    ^ rslt.
6571
101905a14870 Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents: 6486
diff changeset
   474
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   475
    " 
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   476
      (5/9) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   477
      (-5/9) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   478
      (500000000000/900000000000) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   479
      (-500000000000/900000000000) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   480
      (500000000000/9) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   481
      (5/900000000000) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   482
      89012345678901234567 asFloat / 123456789123456789 asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   483
      (89012345678901234567 / 123456789123456789) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   484
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   485
      (
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   486
       180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   487
        /
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   488
       180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   489
      ) asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   490
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   491
      180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   492
         asFloat /
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   493
      180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   494
         asFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   495
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   496
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   497
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   498
asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   499
    "return the receiver as fraction - thats itself"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   500
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   501
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   502
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   503
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   504
asInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   505
    "return an integer with my value - will usually truncate"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   506
a27a279701f8 Initial revision
claus
parents:
diff changeset
   507
    ^ numerator // denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   508
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   509
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   510
asLargeFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   511
    "return a large float with (approximately) my value"
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   512
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   513
    ^ (numerator asLargeFloat) / (denominator asLargeFloat)
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   514
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   515
    "
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   516
      (5/9) asLargeFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   517
      (500000000000/900000000000) asLargeFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   518
      (500000000000/9) asLargeFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   519
    "
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   520
!
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   521
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   522
asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   523
    "return an integer with my value - will usually truncate"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   524
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   525
    ^ self asInteger asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   526
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   527
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   528
asLongFloat
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   529
    "return a long float with (approximately) my value"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   530
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   531
    |num den numShift denShift numBits rslt|
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   532
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   533
    (numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   534
        ^ (numerator asLongFloat) / (denominator asLongFloat)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   535
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   536
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   537
    "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
   538
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   539
    numBits := LongFloat precision * 2.    "number of bits to preserve (conservative)"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   540
    num := numerator abs.
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   541
    numShift := numBits - num highBit. "(num highBit - bits) negated"
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   542
    numShift < 0 ifTrue:[
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   543
        num := num bitShift:numShift
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   544
    ] ifFalse:[
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   545
        numShift := 0
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   546
    ].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   547
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   548
    den :=  denominator.
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   549
    denShift := numBits - den highBit. "(den highBit - bits) negated"
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   550
    denShift < 0 ifTrue:[
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   551
        den := den bitShift:denShift
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   552
    ] ifFalse:[
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   553
        denShift := 0
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   554
    ].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   555
7730
dda82553acc7 make #asLongFloat return a LongFloat (returned Float)
Stefan Vogel <sv@exept.de>
parents: 7571
diff changeset
   556
    rslt := (num asLongFloat / den asLongFloat) * (2 raisedToInteger:denShift-numShift).
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   557
    numerator negative ifTrue:[ ^ rslt negated ].
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   558
    ^ rslt.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   559
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   560
    " 
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   561
      (5/9) asLongFloat                        
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   562
      (-5/9) asLongFloat   
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   563
      (Fraction basicNew setNumerator:500000000000 denominator:900000000000) asLongFloat = (5/9) asLongFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   564
      (Fraction basicNew setNumerator:500000000001 denominator:900000000000) asLongFloat = (5/9) asLongFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   565
      (500000000001/900000000000) asLongFloat  
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   566
      (-500000000001/900000000000) asLongFloat 
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   567
      (500000000001/900000000000) asLongFloat = (5/9) asLongFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   568
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   569
      (500000000000/9) asLongFloat             
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   570
      (5/900000000000) asLongFloat     
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   571
      89012345678901234567 asFloat / 123456789123456789 asLongFloat 
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   572
      (89012345678901234567 / 123456789123456789) asLongFloat        
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   573
      (-89012345678901234567 / 123456789123456789) asLongFloat       
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   574
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   575
      (
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   576
       180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   577
        /
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   578
       180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   579
      ) asLongFloat    
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   580
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   581
      180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   582
         asLongFloat /
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   583
      180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   584
         asLongFloat
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   585
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   586
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   587
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   588
asShortFloat
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   589
    "return a short float with (approximately) my value"
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   590
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   591
    (numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   592
        ^ (numerator asShortFloat) / (denominator asShortFloat)
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   593
    ].
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   594
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   595
    ^ self asFloat asShortFloat
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   596
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   597
    "
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   598
      (5/9) asShortFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   599
      (500000000000/900000000000) asShortFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   600
      (500000000000/9) asShortFloat
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
   601
    "
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   602
!
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   603
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   604
coerce:aNumber
11722
d32d16dd6384 comment
Claus Gittinger <cg@exept.de>
parents: 11671
diff changeset
   605
    "convert the argument aNumber into an instance of the receivers class and return it."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   606
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   607
    ^ aNumber asFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   608
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   609
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   610
generality
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   611
    "return the generality value - see ArithmeticValue>>retry:coercing:"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   612
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   613
    ^ 60
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   614
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   615
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   616
!Fraction methodsFor:'comparing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   617
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   618
< aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   619
    "return true if the receiver is less
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   620
     than aNumber, false otherwise."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   621
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   622
    (aNumber isMemberOf:SmallInteger) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   623
        ^ numerator < (denominator * aNumber)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   624
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   625
    ^ aNumber lessFromFraction:self
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   626
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   627
    "Modified: 5.11.1996 / 10:30:52 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   628
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   629
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   630
= aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   631
    "return true, if the argument represents the same numeric value
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   632
     as the receiver, false otherwise"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   633
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   634
    (aNumber isMemberOf:SmallInteger) ifTrue:[
6891
212aa8576cf7 oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents: 6675
diff changeset
   635
        (denominator == 1) ifFalse:[
212aa8576cf7 oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents: 6675
diff changeset
   636
            ^ numerator = (aNumber * denominator)
212aa8576cf7 oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents: 6675
diff changeset
   637
        ].
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   638
        ^ numerator = aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   639
    ].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   640
    ^ aNumber equalFromFraction:self
3631
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   641
26a44b284912 handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents: 2894
diff changeset
   642
    "Modified: / 7.7.1998 / 17:17:07 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   643
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   644
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   645
> aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   646
    "return true if the receiver is greater
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   647
     than aNumber, false otherwise."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   648
    "optional - could use inherited method ..."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   649
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   650
    (aNumber isMemberOf:SmallInteger) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   651
        ^ numerator > (denominator * aNumber)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   652
    ].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   653
    ^ aNumber < self
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   654
!
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   655
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   656
hash
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   657
    "return a number for hashing; redefined, since fractions compare
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   658
     by numeric value (i.e. (9/3) = 3), therefore (9/3) hash must be the same
4594
eb09f567a3bc float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents: 4593
diff changeset
   659
     as 3 hash."
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   660
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   661
    (denominator = 1) ifTrue:[^ numerator hash].
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   662
4594
eb09f567a3bc float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents: 4593
diff changeset
   663
    ^ self asFloat hash
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   664
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   665
    "
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   666
     3 hash           
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   667
     (9/3) hash       
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   668
     3.0 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   669
     (1/2) hash       
4594
eb09f567a3bc float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents: 4593
diff changeset
   670
     (1/4) hash       
4593
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   671
     0.0 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   672
     0.5 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   673
     0.25 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   674
     0.4 hash         
1023fa0c779e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3984
diff changeset
   675
    "
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   676
!
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   677
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   678
sameFractionValueAs:aNumber
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   679
    "return true, if the argument represents the same numeric value
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   680
     as the receiver, false otherwise"
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   681
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   682
    |rSelf rNum|
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   683
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   684
    rSelf := self reduced.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   685
    rNum := aNumber reduced.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   686
    rSelf denominator = rNum denominator ifTrue:[
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   687
        ^ rSelf numerator = rNum numerator
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   688
    ].
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   689
    ^ false
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   690
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   691
a27a279701f8 Initial revision
claus
parents:
diff changeset
   692
!Fraction methodsFor:'double dispatching'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   693
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   694
differenceFromFixedPoint:aFixedPoint
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   695
    |n d otherDenominator otherNumerator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   696
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   697
    otherDenominator := aFixedPoint denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   698
    otherNumerator := aFixedPoint numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   699
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   700
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   701
    otherDenominator == denominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   702
        n := otherNumerator - numerator. 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   703
        d := otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   704
    ] ifFalse:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   705
        n := (otherNumerator * denominator) - (numerator * otherDenominator).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   706
        d := otherDenominator * denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   707
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   708
    ^ aFixedPoint class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   709
        numerator:n
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   710
        denominator:d
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   711
        scale:(aFixedPoint scale)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   712
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   713
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   714
     ((1/3) asFixedPoint:2) - (1/3)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   715
     ((1/3) asFixedPoint:2) - (2/3) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   716
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   717
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   718
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   719
differenceFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   720
    "sent when a float does not know how to subtract the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   721
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   722
    ^ (aFloat * denominator - numerator) / denominator
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   723
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   724
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   725
differenceFromFraction:aFraction
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   726
    |n d otherDenominator otherNumerator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   727
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   728
    otherDenominator := aFraction denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   729
    otherNumerator := aFraction numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   730
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   731
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   732
    otherDenominator == denominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   733
        n := otherNumerator - numerator. 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   734
        d := otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   735
    ] ifFalse:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   736
        n := (otherNumerator * denominator) - (numerator * otherDenominator).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   737
        d := otherDenominator * denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   738
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   739
    ^ aFraction class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   740
        numerator:n
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   741
        denominator:d
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   742
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   743
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   744
     ((1/3) asFixedPoint:2) - (1/3)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   745
     ((1/3) asFixedPoint:2) - (2/3) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   746
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   747
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   748
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   749
differenceFromInteger:anInteger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   750
    "sent when an integer does not know how to subtract the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   751
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   752
    ^ self class 
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   753
        numerator:((anInteger * denominator) - numerator)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   754
        denominator:denominator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   755
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   756
    "Modified: 28.7.1997 / 19:08:53 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   757
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   758
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   759
equalFromFraction:aFraction
7571
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   760
    denominator = aFraction denominator ifFalse:[
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   761
        ^ false   " must always be reduced "
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   762
        "/ ^ (numerator * aFraction denominator) = (aFraction numerator * denominator)
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   763
    ].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   764
    ^ numerator = aFraction numerator
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   765
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   766
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   767
equalFromInteger:anInteger
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   768
    "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
   769
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   770
    "as I am always reduced, this test should not be required.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   771
     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
   772
     which might be encountered internally"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   773
7571
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   774
    denominator = 1 ifFalse:[
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   775
        ^ numerator = (anInteger * denominator)
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   776
    ].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   777
    ^ numerator = anInteger
7571
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   778
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   779
    "
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   780
     1 = (1 asFixedPoint:1)
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   781
     (1 asFixedPoint:1) = 1
57d86ad2aded fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   782
    "
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   783
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   784
10486
ecab71364dab new: #lessEqFromInteger:
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
   785
lessEqFromInteger:anInteger
ecab71364dab new: #lessEqFromInteger:
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
   786
    "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
   787
ecab71364dab new: #lessEqFromInteger:
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
   788
    ^ (denominator * anInteger) <= numerator
ecab71364dab new: #lessEqFromInteger:
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
   789
!
ecab71364dab new: #lessEqFromInteger:
Stefan Vogel <sv@exept.de>
parents: 8913
diff changeset
   790
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   791
lessFromFraction:aFraction
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   792
    "sent when a fraction does not know how to compare to the receiver"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   793
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   794
    |n d|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   795
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   796
    d := aFraction denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   797
    n := aFraction numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   798
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   799
    "/ save a multiplication if possible
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   800
    d == denominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   801
        ^ n < numerator
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   802
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   803
    ^ (denominator * n) < (numerator * d)  
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   804
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   805
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   806
lessFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   807
    "sent when an integer does not know how to compare to the receiver, a fraction"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   808
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   809
    ^ (denominator * anInteger) < numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   810
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   811
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   812
productFromFixedPoint:aFixedPoint
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   813
    ^ aFixedPoint class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   814
        numerator:(aFixedPoint numerator * numerator) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   815
        denominator:(aFixedPoint denominator * denominator)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   816
        scale:(aFixedPoint scale)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   817
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   818
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   819
     ((1/3) asFixedPoint:2) * 2       
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   820
     ((1/3) asFixedPoint:2) * (1/2) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   821
     ((1/3) asFixedPoint:2) * (3/2) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   822
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   823
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   824
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   825
productFromFloat:aFloat
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   826
    "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
   827
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   828
    ^ aFloat * numerator / denominator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   829
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   830
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   831
productFromFraction:aFraction
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   832
    ^ aFraction class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   833
        numerator:(aFraction numerator * numerator) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   834
        denominator:(aFraction denominator * denominator)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   835
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   836
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   837
     ((1/3) asFixedPoint:2) * 2       
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   838
     ((1/3) asFixedPoint:2) * (1/2) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   839
     ((1/3) asFixedPoint:2) * (3/2) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   840
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   841
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   842
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   843
productFromInteger:anInteger
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   844
    "sent when an integer does not know how to multiply the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   845
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   846
    ^ self class 
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   847
        numerator:(anInteger * numerator)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   848
        denominator:denominator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   849
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   850
    "Modified: 28.7.1997 / 19:06:22 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   851
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   852
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   853
quotientFromFixedPoint:aFixedPoint
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   854
    "Return the quotient of the argument, aFixedPoint and the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   855
     Sent when aFixedPoint does not know how to divide by the receiver."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   856
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   857
    ^ aFixedPoint class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   858
        numerator:(aFixedPoint numerator * denominator) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   859
        denominator:(aFixedPoint denominator * numerator)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   860
        scale:(aFixedPoint scale)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   861
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   862
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   863
     ((1/3) asFixedPoint:2) / 2       
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   864
     ((1/3) asFixedPoint:2) / (1/2) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   865
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   866
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   867
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   868
quotientFromFloat:aFloat
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   869
    "Return the quotient of the argument, aFloat and the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   870
     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
   871
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   872
    ^ (aFloat * denominator) / numerator
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   873
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   874
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   875
quotientFromFraction:aFraction
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   876
    "Return the quotient of the argument, aFraction and the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   877
     Sent when aFraction does not know how to divide by the receiver."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   878
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   879
    ^ aFraction class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   880
        numerator:(aFraction numerator * denominator) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   881
        denominator:(aFraction denominator * numerator)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   882
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   883
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   884
     (1/3) / (1/2) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   885
     (1/3) / (3/2) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   886
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   887
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   888
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   889
quotientFromInteger:anInteger
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   890
    "Return the quotient of the argument, anInteger and the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7403
diff changeset
   891
     Sent when anInteger does not know how to divide by the receiver."
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   892
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   893
    ^ self class 
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   894
        numerator:(anInteger * denominator)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   895
        denominator:numerator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   896
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   897
    "Modified: 28.7.1997 / 19:08:46 / cg"
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   898
!
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   899
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   900
sumFromFixedPoint:aFixedPoint
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   901
    |n d otherDenominator otherNumerator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   902
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   903
    otherDenominator := aFixedPoint denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   904
    otherNumerator := aFixedPoint numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   905
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   906
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   907
    otherDenominator == denominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   908
        n := otherNumerator + numerator. 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   909
        d := otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   910
    ] ifFalse:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   911
        n := (otherNumerator * denominator) + (numerator * otherDenominator).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   912
        d := otherDenominator * denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   913
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   914
    ^ aFixedPoint class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   915
        numerator:n
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   916
        denominator:d
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   917
        scale:(aFixedPoint scale)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   918
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   919
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   920
     ((1/3) asFixedPoint:2) + (1/3)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   921
     ((1/3) asFixedPoint:2) + (2/3) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   922
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   923
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   924
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   925
sumFromFloat:aFloat
213
3b56a17534fd *** empty log message ***
claus
parents: 92
diff changeset
   926
    "sent when a float does not know how to add the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   927
a27a279701f8 Initial revision
claus
parents:
diff changeset
   928
    ^ (aFloat * denominator + numerator) / denominator
a27a279701f8 Initial revision
claus
parents:
diff changeset
   929
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   930
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   931
sumFromFraction:aFraction
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   932
    |n d otherDenominator otherNumerator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   933
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   934
    otherDenominator := aFraction denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   935
    otherNumerator := aFraction numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   936
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   937
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   938
    otherDenominator == denominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   939
        n := otherNumerator + numerator. 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   940
        d := otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   941
    ] ifFalse:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   942
        n := (otherNumerator * denominator) + (numerator * otherDenominator).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   943
        d := otherDenominator * denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   944
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   945
    ^ aFraction class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   946
        numerator:n
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   947
        denominator:d
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   948
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   949
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   950
     (1/3) + (1/3)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   951
     (1/3) + (2/3) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   952
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   953
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   954
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   955
sumFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   956
    "sent when an integer does not know how to add the receiver, a fraction"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   957
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   958
    ^ self class 
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
   959
        numerator:(numerator + (anInteger * denominator))
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
   960
        denominator:denominator
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
   961
2792
2ae1f2e3d11f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2790
diff changeset
   962
    "Modified: 28.7.1997 / 19:08:40 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   963
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   964
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   965
!Fraction methodsFor:'printing & storing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   966
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   967
printOn:aStream
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   968
    "append a printed representation of the receiver to the
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   969
     argument, aStream"
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   970
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   971
    |t|
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   972
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   973
    PrintWholeNumbers == true ifTrue:[
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   974
        "/ experimental: print fractions which are greater than 1 as a sum of
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   975
        "/ an integral and the fractional part. They are easier to read this way.
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   976
        numerator > denominator ifTrue:[
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   977
            aStream nextPut:$(.
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   978
            t := numerator // denominator.
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   979
            t printOn:aStream.
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   980
            aStream nextPutAll:'+('.
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   981
            (numerator - (t*denominator)) printOn:aStream.
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   982
            aStream nextPut:$/.
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   983
            denominator printOn:aStream.
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   984
            aStream nextPutAll:'))'.
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   985
            ^ self
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   986
        ].
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   987
    ].
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   988
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   989
    aStream nextPut:$(.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   990
    numerator printOn:aStream.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   991
    aStream nextPut:$/.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   992
    denominator printOn:aStream.
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   993
    aStream nextPut:$)
6675
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   994
c7e2757d5e80 documentation comment
Claus Gittinger <cg@exept.de>
parents: 6650
diff changeset
   995
    "Modified: / 31.7.2002 / 09:56:41 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   996
! !
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   997
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   998
!Fraction methodsFor:'private'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   999
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1000
reduced
7139
958ca5839e25 comment
Claus Gittinger <cg@exept.de>
parents: 6891
diff changeset
  1001
    "reduce the receiver; divide the numerator and denominator by their
958ca5839e25 comment
Claus Gittinger <cg@exept.de>
parents: 6891
diff changeset
  1002
     greatest common divisor; if the result is integral, return an Integer.
958ca5839e25 comment
Claus Gittinger <cg@exept.de>
parents: 6891
diff changeset
  1003
     Otherwise, return the normalized receiver.
11250
9a962a088d30 comment
Claus Gittinger <cg@exept.de>
parents: 11239
diff changeset
  1004
     CAVEAT: bad name; should be called reduce, as it has a side effect
9a962a088d30 comment
Claus Gittinger <cg@exept.de>
parents: 11239
diff changeset
  1005
     (i.e. this is destructive wrt. the instance values)."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1006
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
  1007
    |gcd den|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1008
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
  1009
    den := denominator.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
  1010
    den < 0 ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
  1011
        numerator := numerator negated.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
  1012
        den := denominator := den negated.
2789
e3e8707d26b4 make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1013
    ].
e3e8707d26b4 make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1014
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
  1015
    den == 1 ifTrue:[^ numerator].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1016
    numerator == 1 ifTrue:[^ self].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1017
    numerator == 0 ifTrue:[^ 0].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1018
2790
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
  1019
    gcd := numerator gcd:den.
495b8ea6a4cb *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2789
diff changeset
  1020
    (gcd ~~ 1) ifTrue:[
6576
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
  1021
        gcd < 0 ifTrue:[
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
  1022
             gcd := gcd negated.
c28cdeb0bfb0 Fix asFloat
Stefan Vogel <sv@exept.de>
parents: 6571
diff changeset
  1023
        ].
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
  1024
        numerator := numerator // gcd.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
  1025
        denominator := den := den // gcd.
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
  1026
        (den == 1) ifTrue:[^ numerator].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1027
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1028
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1029
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1030
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1031
setNumerator:num denominator:den
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1032
    "set both numerator and denominator"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1033
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1034
    numerator := num.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1035
    denominator := den
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1036
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1037
6650
35de1d8400b2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6646
diff changeset
  1038
!Fraction methodsFor:'testing'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1039
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1040
isFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1041
    "return true, if the receiver is some kind of fraction;
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1042
     true is returned here - the method is redefined from Object."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1043
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1044
    ^ true
6650
35de1d8400b2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6646
diff changeset
  1045
!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1046
4658
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
  1047
isLiteral
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
  1048
    "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
  1049
     (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
  1050
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
  1051
    ^ true
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
  1052
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
  1053
!
1a74754fbe91 moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents: 4594
diff changeset
  1054
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1055
negative
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1056
    "return true if the receiver is negative"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1057
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1058
    (numerator < 0) ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
  1059
        ^ (denominator < 0) not
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1060
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1061
    ^ (denominator < 0)
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1062
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1063
5552
31b5cc144476 category changes
Claus Gittinger <cg@exept.de>
parents: 5364
diff changeset
  1064
!Fraction methodsFor:'truncation & rounding'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1065
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1066
fractionPart
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1067
    "extract the after-decimal fraction part,
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1068
     such that (self truncated + self fractionPart) = self"
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1069
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
  1070
    numerator abs < denominator abs ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
  1071
        ^ self
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1072
    ].
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1073
    ^ (numerator rem: denominator) / denominator
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1074
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1075
    "
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1076
     (3/2) fractionPart + (3/2) truncated    
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1077
     (-3/2) fractionPart + (-3/2) truncated    
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1078
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1079
     (3/2) fractionPart     
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
  1080
     (-3/2) fractionPart     
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1081
     (3/2) asFloat fractionPart     
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1082
     (-3/2) asFloat fractionPart     
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7140
diff changeset
  1083
     (2/3) fractionPart          
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1084
     ((3/2)*(15/4)) fractionPart   
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1085
     ((2/3)*(4/15)) fractionPart   
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1086
    "
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1087
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
  1088
    "Modified: / 5.11.2001 / 17:55:25 / cg"
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1089
!
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1090
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1091
integerPart
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1092
    "extract the pre-decimal integer part."
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1093
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
  1094
    numerator abs < denominator abs ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
  1095
        ^ 0
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1096
    ].
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1097
    ^ super integerPart
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1098
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1099
    "
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
  1100
     (3/2) integerPart        
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
  1101
     (-3/2) integerPart        
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1102
     (2/3) integerPart           
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1103
     ((3/2)*(15/4)) integerPart   
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1104
     ((2/3)*(4/15)) integerPart   
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1105
    "
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1106
6153
e92766db42c6 integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents: 6064
diff changeset
  1107
    "Modified: / 5.11.2001 / 17:55:01 / cg"
3899
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1108
!
aa9b50cdfced added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents: 3631
diff changeset
  1109
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1110
rounded
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1111
    "return the receiver rounded to the nearest integer as integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1112
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1113
    "/ mhmh - what about -(1/2)
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1114
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1115
    |t|
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1116
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1117
    self negative ifTrue:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
  1118
        t := self - (1/2)
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1119
    ] ifFalse:[
5364
a27f5167822c *** empty log message ***
ps
parents: 5322
diff changeset
  1120
        t := self + (1/2)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1121
    ].
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1122
    ^ t truncated.
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1123
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1124
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1125
     (1/3) rounded           
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1126
     (1/3) negated rounded     
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1127
     (1/2) rounded           
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1128
     (1/2) negated rounded   
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1129
     0.5 rounded  
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1130
     -0.5 rounded 
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1131
     (2/3) rounded             
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1132
     (2/3) negated rounded     
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1133
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1134
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1135
    "Modified: 5.11.1996 / 11:32:32 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1136
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1137
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1138
truncated
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1139
    "return the receiver truncated towards zero as Integer"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1140
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1141
    ^ numerator quo: denominator
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1142
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1143
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1144
     (3/2) truncated     
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1145
     (3/2) negated truncated  
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1146
    "
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1147
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1148
    "Modified: 5.11.1996 / 12:18:46 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1149
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1150
8395
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1151
!Fraction methodsFor:'visiting'!
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1152
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1153
acceptVisitor:aVisitor with:aParameter
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1154
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1155
    ^ aVisitor visitFraction:self with:aParameter
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1156
! !
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7859
diff changeset
  1157
1878
5615af5e7f47 fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents: 1556
diff changeset
  1158
!Fraction class methodsFor:'documentation'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1159
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1160
version
17865
598963c6ff8e Recommited from itself
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17846
diff changeset
  1161
    ^ '$Id: Fraction.st 10695 2011-09-28 19:07:49Z vranyj1 $'
17761
b0e5971141bc Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17732
diff changeset
  1162
!
b0e5971141bc Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17732
diff changeset
  1163
17841
7abcc4aef871 Merged with trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17761
diff changeset
  1164
version_CVS
17845
7e0cfaac936d Merged with /trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17841
diff changeset
  1165
    ^ '§Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.81 2011/05/03 09:08:52 cg Exp §'
17865
598963c6ff8e Recommited from itself
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17846
diff changeset
  1166
!
598963c6ff8e Recommited from itself
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17846
diff changeset
  1167
598963c6ff8e Recommited from itself
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17846
diff changeset
  1168
version_SVN
598963c6ff8e Recommited from itself
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17846
diff changeset
  1169
    ^ '$Id: Fraction.st 10695 2011-09-28 19:07:49Z vranyj1 $'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1170
! !
6891
212aa8576cf7 oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents: 6675
diff changeset
  1171
17846
24edc476ac18 Merged with /trunk
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 17845
diff changeset
  1172
Fraction initialize!