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