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