FixedPoint.st
author Stefan Vogel <sv@exept.de>
Tue, 28 Apr 2020 16:21:34 +0200
changeset 25373 f030619565e1
parent 25058 08eb3345f6d9
permissions -rw-r--r--
#REFACTORING by stefan class: ArrayedCollection class changed: #with:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25058
08eb3345f6d9 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25007
diff changeset
     1
"{ Encoding: utf8 }"
08eb3345f6d9 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25007
diff changeset
     2
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
"
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
     4
     This is public domain code, not covered by the ST/X copyright.
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
     5
     Code is provided 'as is', as a goody, without any warranty.
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
     6
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
     this comes from:
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
     Jan Steinman, Bytesmiths
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
     2002 Parkside Court, West Linn, OR 97068-2767 USA, +1 503 657 7703
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
     Friedlistrasse 19, CH-3006, Bern, Switzerland, +41 31 999 3946
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
     this code was published in comp.lang.smalltalk; 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
     added here as an example ...
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
"
5570
e6e14f50d721 category change
Claus Gittinger <cg@exept.de>
parents: 3731
diff changeset
    16
"{ Package: 'stx:libbasic' }"
e6e14f50d721 category change
Claus Gittinger <cg@exept.de>
parents: 3731
diff changeset
    17
17445
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
    18
"{ NameSpace: Smalltalk }"
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
    19
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
Fraction subclass:#FixedPoint
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
    21
	instanceVariableNames:'scale'
21975
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
    22
	classVariableNames:'PrintTruncated Pi Pi_1000 E'
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
    23
	poolDictionaries:''
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
    24
	category:'Magnitude-Numbers'
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
FixedPoint comment:'
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
Description: This class implements infinite precision fixed-point numbers. 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
It doesn''t really do anything too interesting except creating instances, converting, and printing, 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
since its superclass Fraction does all the work.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
Test: "''123456789012345678901234567890.123456789'' asFixed * 1000000000 = 123456789012345678901234567890123456789"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
Notes: 1) The current implementation does not convert arbitrarily-based String representations, 
6704
abdf0002a791 oops - I messed mixed arithmetic up
penk
parents: 6650
diff changeset
    35
          which shouldn''t be too much a problem for financial types.'
6237
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
    36
!
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
!FixedPoint class methodsFor:'documentation'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
copyright
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
"
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
    42
     This is public domain code, not covered by the ST/X copyright.
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
    43
     Code is provided 'as is', as a goody, without any warranty.
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
    44
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
     this comes from:
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
     Jan Steinman, Bytesmiths
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
     2002 Parkside Court, West Linn, OR 97068-2767 USA, +1 503 657 7703
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
     Friedlistrasse 19, CH-3006, Bern, Switzerland, +41 31 999 3946
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
     this code was published in comp.lang.smalltalk; 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
     added here as an example ...
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
documentation
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
"
7380
c704ff45bb80 documentation
Claus Gittinger <cg@exept.de>
parents: 7376
diff changeset
    58
    This class implements infinite precision fixed-point numbers,
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
    which internally hold exact (fractional) results, but print themself with
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
    60
    a limited number of digits after the decimal point (rounded). 
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
    61
25058
08eb3345f6d9 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25007
diff changeset
    62
    These are also named ScaledDecimals in other systems.
08eb3345f6d9 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25007
diff changeset
    63
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
    64
    NOTICE:
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
    65
        it seems that squeak prints these truncated,
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
    66
        whereas ST/X prints them rounded.
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
    67
        This behavior should probably be controllable by providing      
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
    68
        a subclass (RoundedFixedPoint ?) which redefines the printOn: method.
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
    69
        (it is now controlled by a classVar, which is of course not a thread-safe
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
    70
        solution).
7380
c704ff45bb80 documentation
Claus Gittinger <cg@exept.de>
parents: 7376
diff changeset
    71
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
    These can be used in computation, where rounding errors should not accumulate,
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
    but only a limited precision is required for the final result.
1898
883a1046ca9a commentary
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
    74
    (i.e. business applications)
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
    It doesn't really do anything too interesting except creating instances, 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
    converting, and printing, since its superclass Fraction does all the work.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
    Test: 
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
    80
        '123456789012345678901234567890.123456789' asFixedPoint * 1000000000
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
        -> 123456789012345678901234567890123456789'
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
    Notes: 1) The current implementation does not convert arbitrarily-based 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
              String representations, which shouldn't be too much a problem 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
              for financial types.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
           2) the implementation is a hack - it has not been optimized for speed
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
              in particular.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
    90
    Mixed mode arithmetic:
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
    91
        fix op fix       -> fix, scale is max. of operands
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
    92
        fix op fraction  -> fix; scale is fix's scale
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
    93
        fix op integer   -> fix; scale is fix's scale
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
    94
        fix op float     -> float
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
    95
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
    [author:]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
        Jan Steinman, Bytesmiths
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
    98
        adapted, modified & enhanced by Claus Gittinger
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
    [see also:]
9129
f1451239cd4b comment
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   101
        Number Fraction Integer Float ShortFloat LongFloat Complex
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
examples
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
                                                                [exBegin]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
     a := (FixedPoint fromString:'123.456').
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   111
     b := '1.10' asFixedPoint.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
     r := a + b.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
     Transcript showCR:r.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
     Transcript showCR:(r withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
     Transcript showCR:(r rounded).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
                                                                [exEnd]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
                                                                [exBegin]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
     a := (FixedPoint fromString:'0.9999999').
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   123
     b := 0.0000001 asFixedPoint. 
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
     r := a + b.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
     Transcript showCR:r.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
     Transcript showCR:(r withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
     Transcript showCR:(r rounded).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
                                                                [exEnd]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
                                                                [exBegin]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
     a := (FixedPoint fromString:'0.9999998').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
     r := a + b.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
     Transcript showCR:r.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
     Transcript showCR:(r withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
     Transcript showCR:(r rounded).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
                                                                [exEnd]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
                                                                [exBegin]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
     a := (FixedPoint fromString:'1.0').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
     r := a + b.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
     Transcript showCR:r.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
     Transcript showCR:(r withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
     Transcript showCR:(r rounded).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
                                                                [exEnd]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
                                                                [exBegin]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
     r := a + b.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
     Transcript showCR:r.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
     Transcript showCR:(r withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
     Transcript showCR:(r rounded).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
                                                                [exEnd]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
!FixedPoint class methodsFor:'instance creation'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   172
numerator:n denominator:d
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   173
    "redefined to block the inherited instance creation method from fraction.
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   174
     Raises an error - you must give a scale or provide a power-of-10 denominator"
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   175
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   176
    (d isPowerOf:10) ifTrue:[
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   177
        ^ self numerator:n denominator:d scale:(d log10 asInteger)
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   178
    ].    
6237
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   179
    self shouldNotImplement. "use #numerator:denominator:scale"
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   180
    "/ ^ self numerator:n denominator:d scale:(d log max:n log) ceiling
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   181
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   182
    "
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   183
     self numerator:123 denominator:100    
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   184
    "
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   185
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   186
    "Modified: / 03-07-2017 / 12:46:11 / cg"
6237
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   187
!
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   188
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
numerator:n denominator:d scale:s
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   190
    "create and return a new fixedPoint instances with the given scale
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   191
     (post decimal digits when printed). Assume its already reduced."
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   192
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
    ^ self basicNew
6894
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   194
        setNumerator:n denominator:d scale:s
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   197
readFrom:aStringOrStream 
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   198
    "return the next FixedPoint from the (character-)stream aStream. 
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   199
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   200
     NOTICE:   
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   201
       This behaves different from the default readFrom:, in returning
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   202
       0 (instead of raising an error) in case no number can be read.
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   203
       It is unclear, if this is the correct behavior (ST-80 does this)
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   204
       - depending on the upcoming ANSI standard, this may change."
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   205
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   206
    ^ self readFrom:aStringOrStream onError:0
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   208
    "
13147
b262e06ef444 comment/format in: #readFrom:
Claus Gittinger <cg@exept.de>
parents: 12724
diff changeset
   209
     FixedPoint readFrom:'.456'  
11900
d2e9bfce6e42 comment
Claus Gittinger <cg@exept.de>
parents: 11845
diff changeset
   210
     FixedPoint readFrom:'123.456'  
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   211
     FixedPoint readFrom:'123.'  
6636
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   212
     FixedPoint readFrom:'3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788'
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   213
     FixedPoint readFrom:(ReadStream on:'foobar')     
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   214
     FixedPoint readFrom:(ReadStream on:'foobar') onError:nil  
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   215
     FixedPoint readFrom:'0b1234' onError:nil  
11900
d2e9bfce6e42 comment
Claus Gittinger <cg@exept.de>
parents: 11845
diff changeset
   216
d2e9bfce6e42 comment
Claus Gittinger <cg@exept.de>
parents: 11845
diff changeset
   217
     FixedPoint readFrom:'1'      
d2e9bfce6e42 comment
Claus Gittinger <cg@exept.de>
parents: 11845
diff changeset
   218
     FixedPoint readFrom:'2'      
d2e9bfce6e42 comment
Claus Gittinger <cg@exept.de>
parents: 11845
diff changeset
   219
     FixedPoint readFrom:'1.5'    
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   220
    "
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   221
13147
b262e06ef444 comment/format in: #readFrom:
Claus Gittinger <cg@exept.de>
parents: 12724
diff changeset
   222
    "Modified: / 23-11-2010 / 14:39:29 / cg"
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   223
    "Modified (comment): / 09-03-2017 / 15:44:38 / cg"
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   224
!
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   225
11591
Claus Gittinger <cg@exept.de>
parents: 11251
diff changeset
   226
readFrom:aStringOrStream decimalPointCharacters:decimalPointCharacters onError:exceptionBlock
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   227
    "return an instance of me as described on the string or stream, aStringOrStream.
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   228
     If an error occurs during conversion, return the result from evaluating exceptionBlock"
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   229
24994
24d2a0b0a027 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 21975
diff changeset
   230
    | aStream sign integerPart fractionStream char fractionPart scale nextChar val|
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   231
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   232
    aStream := aStringOrStream readStream.
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   233
19239
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   234
    aStream peekOrNil == $- ifTrue:[
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   235
        sign := -1.
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   236
        aStream next.
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   237
    ] ifFalse:[
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   238
        sign := 1
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   239
    ].
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   240
19239
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   241
    nextChar := aStream peekOrNil.
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   242
    (nextChar isNil or:[nextChar isLetter]) ifTrue: [^ exceptionBlock value].
17445
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
   243
    (decimalPointCharacters includes:nextChar) ifTrue:[
13147
b262e06ef444 comment/format in: #readFrom:
Claus Gittinger <cg@exept.de>
parents: 12724
diff changeset
   244
        "/ no integer part
b262e06ef444 comment/format in: #readFrom:
Claus Gittinger <cg@exept.de>
parents: 12724
diff changeset
   245
        integerPart := 0.
b262e06ef444 comment/format in: #readFrom:
Claus Gittinger <cg@exept.de>
parents: 12724
diff changeset
   246
        aStream next.
b262e06ef444 comment/format in: #readFrom:
Claus Gittinger <cg@exept.de>
parents: 12724
diff changeset
   247
    ] ifFalse:[
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   248
        (nextChar isDigit) ifFalse: [^ exceptionBlock value].
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   249
        "/ FIX: only read the integer chars - not to the end.
21635
8201def44dbb #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 21633
diff changeset
   250
        integerPart := Integer readFrom:aStream "(aStream upToAny:decimalPointCharacters)" allowRadix:false onError:[^ exceptionBlock value].
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   251
        nextChar := aStream peekOrNil.
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   252
        (decimalPointCharacters includes:nextChar) ifFalse:[
24994
24d2a0b0a027 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 21975
diff changeset
   253
            sign < 0 ifTrue:[ integerPart := integerPart negated ].
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   254
            "/ only integer part
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   255
            ^ self basicNew 
24994
24d2a0b0a027 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 21975
diff changeset
   256
                setNumerator:integerPart
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   257
                denominator:1
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   258
                scale:0
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   259
        ].    
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   260
        aStream next.
13147
b262e06ef444 comment/format in: #readFrom:
Claus Gittinger <cg@exept.de>
parents: 12724
diff changeset
   261
    ].
19239
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   262
    nextChar := aStream peekOrNil.
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   263
    (nextChar isNil or:[nextChar isDigit not]) ifTrue: [
24994
24d2a0b0a027 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 21975
diff changeset
   264
        sign < 0 ifTrue:[ integerPart := integerPart negated ].
19239
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   265
        ^ self basicNew 
24994
24d2a0b0a027 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 21975
diff changeset
   266
            setNumerator:integerPart
19239
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   267
            denominator:1
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   268
            scale:0
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   269
    ].
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   270
    
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   271
    fractionStream := ReadWriteStream on:(String new:10).
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   272
    [
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   273
        char := aStream nextOrNil.
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   274
        char notNil and:[char isDigit]
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   275
    ] whileTrue:[
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   276
        fractionStream nextPut:char
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   277
    ].
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   278
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   279
    scale := fractionStream position.
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   280
    fractionStream reset.
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   281
    fractionPart := Integer readFrom:fractionStream onError:[^ exceptionBlock value]. 
24994
24d2a0b0a027 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 21975
diff changeset
   282
    val := integerPart * (10 raisedTo:scale) + fractionPart.
24d2a0b0a027 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 21975
diff changeset
   283
    sign < 0 ifTrue:[ val := val negated ].
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   284
    ^ self basicNew 
24994
24d2a0b0a027 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 21975
diff changeset
   285
        setNumerator:val 
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   286
        scale:scale
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   287
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   288
    "
17445
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
   289
     FixedPoint readFrom:'1.00'    
11591
Claus Gittinger <cg@exept.de>
parents: 11251
diff changeset
   290
     FixedPoint readFrom:'123.456'  
17445
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
   291
     FixedPoint readFrom:'123,456' decimalPointCharacters:',' 
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
   292
     FixedPoint readFrom:'-123.456'     
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
   293
     FixedPoint readFrom:'123'          
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   294
     FixedPoint readFrom:'-123' 
17445
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
   295
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   296
     -- notice the difference between readFromString and readFrom:
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   297
     
17445
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
   298
     FixedPoint readFromString:'-123.abcd' onError:[47.5]  
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
   299
     FixedPoint readFromString:'-1a.bcd' onError:[47.5]  
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
   300
     FixedPoint readFromString:'foot' onError:['bad fixedpoint'] 
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   301
     FixedPoint readFromString:'0b1234' onError:['bad fixedpoint'] 
17445
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
   302
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   303
     -- readFrom only reads what can be read, leaving the stream at the end:
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   304
     
17445
1753d81dbc4c class: FixedPoint
Claus Gittinger <cg@exept.de>
parents: 17054
diff changeset
   305
     FixedPoint readFrom:'-123.abcd' onError:[47.5]  
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   306
     FixedPoint readFrom:'-1a.bcd' onError:[47.5] 
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   307
     FixedPoint readFrom:'foot' onError:['bad fixedpoint'] 
21633
991fc5368cac #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 20414
diff changeset
   308
     FixedPoint readFrom:'0b1234' onError:['bad fixedpoint'] 
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   309
    "
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   310
13147
b262e06ef444 comment/format in: #readFrom:
Claus Gittinger <cg@exept.de>
parents: 12724
diff changeset
   311
    "Created: / 25-10-1997 / 15:28:59 / cg"
21635
8201def44dbb #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 21633
diff changeset
   312
    "Modified: / 09-03-2017 / 16:27:36 / cg"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
21818
20bc6feedae5 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21635
diff changeset
   315
!FixedPoint class methodsFor:'coercing & converting'!
20bc6feedae5 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21635
diff changeset
   316
20bc6feedae5 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21635
diff changeset
   317
coerce:aNumber
25007
050a1002f4b6 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24994
diff changeset
   318
    "convert the argument aNumber into an instance of the receiver (class) and return it."
21818
20bc6feedae5 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21635
diff changeset
   319
20bc6feedae5 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21635
diff changeset
   320
    ^ aNumber asFixedPoint
20bc6feedae5 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21635
diff changeset
   321
! !
20bc6feedae5 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21635
diff changeset
   322
6636
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   323
!FixedPoint class methodsFor:'constants'!
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   324
21975
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   325
e
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   326
    "e with roughly 26 valid digits..."
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   327
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   328
    |e|
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   329
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   330
    E isNil ifTrue:[
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   331
        e := super e.
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   332
        E := e asFixedPoint:(e denominator log10 asInteger - 1)
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   333
    ].
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   334
    ^ E
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   335
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   336
    "
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   337
     E := nil.
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   338
     self e
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   339
        -> 2.7182818284590452353602875
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   340
     wolfram:
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   341
           2.718281828459045235360287471352662497757247093699959574966...
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   342
     
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   343
     self e squared    
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   344
     self e reciprocal 
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   345
     1 / self e 
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   346
     self e * 1000000000000000000000000000000000000000000    
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   347
    "
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   348
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   349
    "Created: / 03-07-2017 / 17:25:38 / cg"
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   350
!
1701bf8eb8e3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21945
diff changeset
   351
6636
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   352
pi
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   353
    "pi with roughly 26 valid digits..."
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   354
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   355
    |p|
6636
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   356
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   357
    Pi isNil ifTrue:[
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   358
        p := super pi.
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   359
        Pi := p asFixedPoint:(p denominator log10 asInteger - 1)
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   360
    ].
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   361
    ^ Pi
19247
586d1013b1ac #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19239
diff changeset
   362
586d1013b1ac #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19239
diff changeset
   363
    "
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   364
     Pi := nil.
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   365
     self pi
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   366
        -> 3.1415926535897932384626434
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   367
     wolfram:
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   368
        -> 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904...
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   369
     
19247
586d1013b1ac #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19239
diff changeset
   370
     self pi squared    
586d1013b1ac #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19239
diff changeset
   371
     self pi reciprocal 
586d1013b1ac #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19239
diff changeset
   372
     1 / self pi 
586d1013b1ac #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19239
diff changeset
   373
     self pi * 1000000000000000000000000000000000000000000    
586d1013b1ac #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19239
diff changeset
   374
    "
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   375
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   376
    "Modified (comment): / 03-07-2017 / 13:19:40 / cg"
19247
586d1013b1ac #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19239
diff changeset
   377
!
586d1013b1ac #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19239
diff changeset
   378
586d1013b1ac #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19239
diff changeset
   379
pi1000
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   380
    "pi with roughly 1000 valid digits..."
19247
586d1013b1ac #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19239
diff changeset
   381
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   382
    Pi_1000 isNil ifTrue:[
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   383
        Pi_1000 := super pi1000 asFixedPoint:1000
6636
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   384
    ].
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   385
    ^ Pi_1000.
6636
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   386
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   387
    "
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   388
     Pi_1000 := nil.
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   389
     
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   390
     self pi1000
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   391
        -> 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   392
     wolfram:
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   393
        -> 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904...
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   394
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   395
     self pi1000 squared    
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   396
     self pi1000 reciprocal 
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   397
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   398
     (self pi1000 - self pi) asFloat
6636
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   399
    "
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   400
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   401
    "Modified (comment): / 03-07-2017 / 13:20:24 / cg"
6636
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   402
! !
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   403
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   404
!FixedPoint class methodsFor:'printing control'!
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   405
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   406
printTruncated
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   407
    "return the PrintTruncated flag, which controls printing.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   408
     See the description in the documentation for details"
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   409
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   410
    ^ PrintTruncated ? false.
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   411
!
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   412
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   413
printTruncated:aBoolean
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   414
    "set the PrintTruncated flag, which controls printing.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   415
     See the description in the documentation for details"
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   416
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   417
    PrintTruncated := aBoolean.
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   418
! !
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   419
6898
d0182256f3ff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6897
diff changeset
   420
!FixedPoint class methodsFor:'queries'!
d0182256f3ff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6897
diff changeset
   421
d0182256f3ff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6897
diff changeset
   422
exponentCharacter
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   423
    "return the character used to print between mantissa an exponent.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   424
     Also used by the scanner when reading numbers."
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   425
6898
d0182256f3ff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6897
diff changeset
   426
    ^ $s
d0182256f3ff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6897
diff changeset
   427
! !
d0182256f3ff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6897
diff changeset
   428
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   429
!FixedPoint methodsFor:'accessing'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   430
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   431
epsilon
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   432
    ^ (1 / (10 raisedTo:scale))
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   433
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   434
    "
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   435
     (2 asFixedPoint:4) epsilon   
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   436
     (2 asFixedPoint:100) epsilon   
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   437
     (2 asFixedPoint:1000) epsilon   
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   438
    "
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   439
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   440
    "Created: / 03-07-2017 / 12:35:22 / cg"
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   441
!
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   442
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   443
scale
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   444
    "return the number of places of significance that is carried by the receiver."
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   445
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   446
    ^ scale
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   447
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   448
    "Modified: 12.4.1997 / 11:21:05 / cg"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   449
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   450
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   451
!FixedPoint methodsFor:'arithmetic'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   452
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   453
* aNumber
11732
f4ef9b3eb755 comment
Claus Gittinger <cg@exept.de>
parents: 11716
diff changeset
   454
    "return the product of the receiver and the argument.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   455
     Redefined to care for the scale if the argument is another fixPoint number.
18837
f1835c2995f2 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17445
diff changeset
   456
     The result's scale is the maximum of the receiver's scale and the argument's scale."
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   457
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   458
    "/ notice:
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   459
    "/ the following code handles some common cases,
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   460
    "/ and exists as an optimization, to speed up those cases.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   461
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   462
    "/ a message send; otherwise double-dispatch is just as fast.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   463
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   464
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   465
        ^ self class 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   466
                numerator:(numerator * aNumber)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   467
                denominator:denominator
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   468
                scale:scale
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   469
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   470
    ^ aNumber productFromFixedPoint:self
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   471
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   472
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   473
     |a r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   474
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   475
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   476
     r := a * 5.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   477
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   478
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   479
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   480
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   481
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   482
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   483
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   484
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   485
     b := (FixedPoint fromString:'1.10').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   486
     r := a * b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   487
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   488
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   489
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   490
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   491
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   492
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   493
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   494
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   495
     b := (FixedPoint fromString:'-1.10').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   496
     r := a * b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   497
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   498
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   499
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   500
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   501
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   502
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   503
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   504
     a := (FixedPoint fromString:'0.9999999').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   505
     b := (FixedPoint fromString:'0.9999999').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   506
     r := a * b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   507
     Transcript show:'fixed (exact)  : '; showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   508
     Transcript show:'fixed (scale2) : '; showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   509
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   510
     Transcript show:'float (inexact): '; showCR:(0.9999999 * 0.9999999).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   511
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   512
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   513
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   514
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   515
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   516
     a := 1.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   517
     b := (FixedPoint fromString:'0.9999999').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   518
     r := a * b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   519
     Transcript show:'fixed (exact)  : '; showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   520
     Transcript show:'fixed (scale2) : '; showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   521
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   522
     Transcript show:'float (inexact): '; showCR:(0.9999999 * 0.9999999).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   523
    "
6636
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   524
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   525
    "                       
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   526
     |a r|
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   527
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   528
     a := (FixedPoint fromString:'123.456').
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   529
     r := a * 5.0.    
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   530
     Transcript showCR:r.    
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   531
     Transcript showCR:(r withScale:2).
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   532
    "
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   533
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   534
    "                       
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   535
     |a r|
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   536
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   537
     a := (FixedPoint fromString:'123.456').
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   538
     r := 5.0 * a.    
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   539
     Transcript showCR:r.    
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   540
     Transcript showCR:(r withScale:2).
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   541
    "
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   542
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   543
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   544
+ aNumber
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   545
    "return the sum of the receiver and the argument, aNumber.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   546
     Redefined to care for the scale if the argument is another fixPoint number.
18837
f1835c2995f2 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17445
diff changeset
   547
     The result's scale will be the maximum of the receiver's and the argument's scale."
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   548
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   549
    |n|
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   550
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   551
    "/ notice:
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   552
    "/ the following code handles some common cases,
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   553
    "/ and exists as an optimization, to speed up those cases.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   554
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   555
    "/ a message send; otherwise double-dispatch is just as fast.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   556
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   557
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   558
        "save a multiplication if possible"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   559
        denominator == 1 ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   560
            n := numerator + aNumber.
6645
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   561
        ] ifFalse:[
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   562
            n := numerator + (aNumber * denominator).
6645
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   563
        ].
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   564
        ^ self class 
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   565
            numerator:n 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   566
            denominator:denominator
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   567
            scale:scale
6645
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   568
    ].
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   569
    ^ aNumber sumFromFixedPoint:self
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   570
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   571
    "
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   572
     ((1/3) asFixedPoint:2) + 1    
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   573
     ((1/3) asFixedPoint:2) + (1/3)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   574
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   575
    "
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   576
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   577
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   578
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   579
     b := (FixedPoint fromString:'1.10').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   580
     a + b
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   581
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   582
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   583
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   584
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   585
     a := (FixedPoint fromString:'0.9999999').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   586
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   587
     a + b                   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   588
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   589
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   590
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   591
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   593
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   594
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   595
     a + b                             
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   596
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   597
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   598
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   599
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   600
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   601
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   602
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   603
     (a + b) withScale:2  
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   604
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   605
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   606
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   607
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   608
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   609
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   610
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   611
     (a + b) withScale:1  
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   612
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   613
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   614
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   615
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   616
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   617
     a := 1.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   618
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   619
     Transcript showCR:((a + b) withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   620
     Transcript showCR:(a + b)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   621
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   622
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   623
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   624
- aNumber
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   625
    "return the difference of the receiver and the argument, aNumber.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   626
     Redefined to care for the scale if the argument is another fixPoint number.
18837
f1835c2995f2 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17445
diff changeset
   627
     The result's scale is the maximum of the receiver's scale and the argument's scale."
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   628
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   629
    |n|
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   630
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   631
    "/ notice:
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   632
    "/ the following code handles some common cases,
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   633
    "/ and exists as an optimization, to speed up those cases.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   634
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   635
    "/ a message send; otherwise double-dispatch is just as fast.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   636
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   637
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   638
        "save a multiplication if possible"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   639
        denominator == 1 ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   640
            n := numerator - aNumber.
6645
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   641
        ] ifFalse:[
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   642
            n := numerator - (aNumber * denominator).
6645
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   643
        ].
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   644
        ^ self class 
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   645
            numerator:n 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   646
            denominator:denominator
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   647
            scale:scale
6645
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   648
    ].
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   649
    ^ aNumber differenceFromFixedPoint:self
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   650
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   651
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   652
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   653
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   654
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   655
     b := (FixedPoint fromString:'1.10').
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   656
     a - b     
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   657
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   658
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   659
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   660
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   661
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   662
     a := (FixedPoint fromString:'0.9999999').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   663
     b := (FixedPoint fromString:'0.0000009').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   664
     a - b                   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   665
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   666
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   667
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   668
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   669
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   670
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   671
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   672
     a - b                          
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   673
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   674
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   675
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   676
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   677
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   678
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   679
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   680
     (a - b) withScale:2  
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   681
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   682
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   683
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   684
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   685
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   686
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   687
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   688
     (a - b) withScale:1  
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   689
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   690
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   691
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   692
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   693
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   694
     a := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   695
     b := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   696
     (a - b) withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   697
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   698
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   699
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   700
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   701
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   702
     a := 1.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   703
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   704
     Transcript showCR:((a - b) withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   705
     Transcript showCR:(a - b)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   706
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   707
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   708
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   709
/ aNumber
1898
883a1046ca9a commentary
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   710
    "return the quotient of the receiver and the argument, aNumber.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   711
     Redefined to care for the scale if the argument is another fixPoint number.
18837
f1835c2995f2 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17445
diff changeset
   712
     The result's scale is the maximum of the receiver's scale and the argument's scale."
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   713
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   714
    "/ notice:
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   715
    "/ the following code handles some common cases,
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   716
    "/ and exists as an optimization, to speed up those cases.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   717
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   718
    "/ a message send; otherwise double-dispatch is just as fast.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   719
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   720
    (aNumber isMemberOf:SmallInteger) ifTrue:[
6886
7acddeb0e526 checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6704
diff changeset
   721
        ^ self class 
6894
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   722
                numerator:numerator
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   723
                denominator:(denominator * aNumber)
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   724
                scale:scale
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   725
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   726
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   727
    ^ aNumber quotientFromFixedPoint:self
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   728
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   729
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   730
     |a r|                     
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   731
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   732
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   733
     r := a / 5.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   734
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   735
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   736
     Transcript showCR:(r withScale:9).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   737
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   738
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   739
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   740
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   741
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   742
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   743
     b := (FixedPoint fromString:'1.10').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   744
     r := a / b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   745
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   746
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   747
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   748
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   749
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   750
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   751
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   752
     a := (FixedPoint fromString:'-123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   753
     b := (FixedPoint fromString:'-1.10').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   754
     r := a / b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   755
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   756
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   757
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   758
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   759
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   760
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   761
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   762
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   763
     b := (FixedPoint fromString:'-1.10').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   764
     r := a / b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   765
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   766
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   767
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   768
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   769
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   770
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   771
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   772
     a := 1.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   773
     b := (FixedPoint fromString:'0.9999999').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   774
     r := a / b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   775
     Transcript show:'fixed (exact)  : '; showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   776
     Transcript show:'fixed (scale2) : '; showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   777
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   778
     Transcript show:'float (inexact): '; showCR:(1 / 0.9999999).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   779
    "
6570
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   780
!
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   781
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   782
negated
6894
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   783
    "redefined from Fraction to preserve scale"
6570
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   784
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   785
    ^ self class 
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   786
        numerator:(numerator negated)
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   787
        denominator:denominator
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   788
        scale:scale
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   789
!
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   790
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   791
reciprocal
6894
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   792
    "redefined from Fraction to preserve scale"
6570
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   793
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   794
    numerator == 1 ifTrue:[^ denominator].
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   795
    ^ self class 
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   796
        numerator:denominator
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   797
        denominator:numerator
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   798
        scale:scale
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   799
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   800
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   801
!FixedPoint methodsFor:'coercing & converting'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   802
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   803
asFixedPoint
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   804
    "return the receiver as a fixedPoint number 
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   805
     - that's the receiver itself"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   806
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   807
    ^ self
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   808
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   809
    "Modified: / 10-01-1997 / 19:53:14 / cg"
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   810
    "Modified (comment): / 03-07-2017 / 13:22:28 / cg"
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   811
!
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   812
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   813
asFixedPoint:newScale
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   814
    "return a fixedPoint with the same value as the receiver, 
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   815
     and newScale number of valid decimal digits"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   816
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   817
    |minRequiredDenominator factor|
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   818
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   819
    newScale == scale ifTrue:[^ self].
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   820
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   821
    minRequiredDenominator := 10 raisedTo:newScale.
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   822
    denominator < minRequiredDenominator ifTrue:[
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   823
        factor := minRequiredDenominator / denominator.
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   824
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   825
        ^ self class
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   826
            numerator:(numerator * factor)
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   827
            denominator:minRequiredDenominator
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   828
            scale:newScale
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
   829
    ].
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   830
    ^ self class
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   831
        numerator:numerator
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   832
        denominator:denominator
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   833
        scale:newScale
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   834
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   835
    "
6638
7523142a1152 comment
Claus Gittinger <cg@exept.de>
parents: 6636
diff changeset
   836
     '12345.12345' asFixedPoint:2   
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   837
     (FixedPoint fromString:'12345.12345') asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   838
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   839
     ((FixedPoint fromString:'0.33333333')
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   840
      + 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   841
      (FixedPoint fromString:'0.33333333')
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   842
     ) asFixedPoint:2   
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   843
    "
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   844
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   845
    "Modified: / 12-04-1997 / 11:20:37 / cg"
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   846
    "Modified (comment): / 03-07-2017 / 13:22:56 / cg"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   847
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   848
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   849
asFraction
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   850
    "return the receiver as a fraction"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   851
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   852
    ^ Fraction
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   853
        numerator:numerator
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   854
        denominator:denominator
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   855
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   856
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   857
     (FixedPoint fromString:'0.2')           
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   858
     (FixedPoint fromString:'0.2') asFraction
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   859
     (FixedPoint fromString:'0.2') asFloat
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   860
     (FixedPoint fromString:'0.2') asShortFloat
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   861
     (FixedPoint fromString:'0.2') asInteger
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   862
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   863
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   864
19239
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   865
asIntegerIfPossible
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   866
    "if the receiver can be represented as an integer without loosing precision,
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   867
     return that integer. Otherwise, return the receiver.
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   868
     Useful for printing / string conversion"
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   869
     
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   870
    |gcd|
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   871
    
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   872
    (denominator == 1) ifTrue:[
19239
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   873
        ^ numerator
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   874
    ].
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   875
    numerator >= denominator ifTrue:[
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   876
        ((gcd := numerator gcd:denominator) isPowerOf:10) ifTrue:[
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   877
            (denominator / gcd) == 1 ifTrue:[
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   878
                ^ numerator / gcd
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   879
            ].    
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   880
        ].    
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   881
    ].    
19239
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   882
    ^ self
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   883
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   884
    "
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   885
     1.2345s3 asIntegerIfPossible
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   886
     12.345s3 asIntegerIfPossible
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   887
     123.45s3 asIntegerIfPossible
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   888
     1234.5s3 asIntegerIfPossible
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   889
     12345.0s3 asIntegerIfPossible
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   890
     12345s3 asIntegerIfPossible
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   891
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   892
     1.2345s3 asInteger
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   893
    "
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   894
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
   895
    "Modified (comment): / 03-07-2017 / 13:43:10 / cg"
19239
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   896
!
f9260f171b9e #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18837
diff changeset
   897
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   898
coerce:aNumber
18837
f1835c2995f2 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17445
diff changeset
   899
    "convert the argument aNumber into an instance of the receiver's class and return it."
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   900
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   901
    ^ aNumber asFixedPoint
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   902
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   903
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   904
generality
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   905
    "return the generality value - see ArithmeticValue>>retry:coercing:"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   906
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   907
    ^ 65
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   908
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   909
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   910
     (FixedPoint fromString:'1.001') + 1      
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   911
     (FixedPoint fromString:'1.001') + 1.0    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   912
     (FixedPoint fromString:'1.001') + (1/2)   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   913
     (FixedPoint fromString:'1.001') + 1.0 asShortFloat 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   914
     (FixedPoint fromString:'1.001') + 1 asLargeInteger 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   915
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   916
     1 + (FixedPoint fromString:'1.001') 
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
   917
     1.0 + (FixedPoint fromString:'1.001')      
1891
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
   918
     (1/2) + (FixedPoint fromString:'1.001')    
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   919
     1.0 asShortFloat + (FixedPoint fromString:'1.001')
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   920
     1 asLargeInteger + (FixedPoint fromString:'1.001')
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   921
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   922
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   923
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   924
!FixedPoint methodsFor:'double dispatching'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   925
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   926
differenceFromFixedPoint:aFixedPoint
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   927
    "sent when a fixedPoint is asked to subtract the receiver.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   928
     The result has the higher scale of the two operands.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   929
     Redefined here to compute the scale."
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   930
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   931
    |n d otherNumerator otherDenominator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   932
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   933
    otherNumerator := aFixedPoint numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   934
    otherDenominator := aFixedPoint denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   935
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   936
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   937
    denominator = otherDenominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   938
        n := otherNumerator - numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   939
        d := otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   940
    ] ifFalse:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   941
        n := (otherNumerator * denominator) - (numerator * otherDenominator).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   942
        d := denominator * otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   943
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   944
    ^ self class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   945
            numerator:n 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   946
            denominator:d
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   947
            scale:(scale max:aFixedPoint scale)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   948
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   949
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   950
     ((1/3) asFixedPoint:2) - ((1/3) asFixedPoint:2)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   951
     ((1/3) asFixedPoint:2) - ((2/3) asFixedPoint:2)     
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   952
     (1/3) - ((2/3) asFixedPoint:2)     
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   953
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   954
     ((1/3) asFixedPoint:2) - ((1/3) asFixedPoint:4)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   955
     ((1/3) asFixedPoint:2) - ((2/3) asFixedPoint:4)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   956
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   957
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   958
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   959
differenceFromFraction:aFraction
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   960
    "sent when a fraction is asked to subtract the receiver.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   961
     The result has my scale.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   962
     Redefined here to preserve the scale."
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   963
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   964
    |n d otherNumerator otherDenominator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   965
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   966
    otherNumerator := aFraction numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   967
    otherDenominator := aFraction denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   968
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   969
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   970
    denominator = otherDenominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   971
        n := otherNumerator - numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   972
        d := otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   973
    ] ifFalse:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   974
        n := (otherNumerator * denominator) - (numerator * otherDenominator).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   975
        d := denominator * otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   976
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   977
    ^ self class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   978
            numerator:n 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   979
            denominator:d
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   980
            scale:scale
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   981
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   982
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   983
     (1/3) - ((2/3) asFixedPoint:2)     
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   984
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   985
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   986
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   987
differenceFromInteger:anInteger
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   988
    "sent when an integer does not know how to subtract the receiver.
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   989
     The result has my scale.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   990
     Redefined here to preserve the scale."
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   991
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   992
    ^ self class 
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   993
        numerator:((anInteger * denominator) - numerator)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   994
        denominator:denominator
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   995
        scale:scale
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   996
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   997
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   998
productFromFixedPoint:aFixedPoint
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
   999
    "sent when a fixedPoint is asked to multiply the receiver.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1000
     The result has the higher scale of the two operands.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1001
     Redefined here to compute the scale."
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1002
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1003
    ^ aFixedPoint class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1004
        numerator:(aFixedPoint numerator * numerator) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1005
        denominator:(aFixedPoint denominator * denominator)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1006
        scale:(scale max:aFixedPoint scale)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1007
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1008
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1009
     ((1/3) asFixedPoint:2) * ((1/3) asFixedPoint:4)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1010
     (1/3) * ((1/3) asFixedPoint:4)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1011
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1012
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1013
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1014
productFromFraction:aFraction
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1015
    "sent when a fraction is asked to multiply the receiver.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1016
     The result has my scale.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1017
     Redefined here to preserve the scale."
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1018
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1019
    ^ self class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1020
        numerator:(aFraction numerator * numerator) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1021
        denominator:(aFraction denominator * denominator)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1022
        scale:scale
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1023
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1024
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1025
     (1/3) * ((1/3) asFixedPoint:4) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1026
    "
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1027
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1028
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1029
productFromInteger:anInteger
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1030
    "sent when an integer does not know how to multiply the receiver.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1031
     Redefined here to preserve the scale."
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1032
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1033
    ^ self class 
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1034
        numerator:(anInteger * numerator)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1035
        denominator:denominator
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1036
        scale:scale
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1037
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1038
    "Modified: 5.11.1996 / 10:32:28 / cg"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1039
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1040
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1041
quotientFromFixedPoint:aFixedPoint
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1042
    "sent when a fixedPoint is asked to divide by the receiver.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1043
     The result has the higher scale of the two operands.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1044
     Redefined here to compute the scale."
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
  1045
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1046
    ^ aFixedPoint class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1047
        numerator:(aFixedPoint numerator * denominator) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1048
        denominator:(aFixedPoint denominator * numerator)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1049
        scale:(scale max:aFixedPoint scale)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1050
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1051
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1052
     ((1/3) asFixedPoint:2) / ((1/3) asFixedPoint:2) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1053
     ((1/3) asFixedPoint:2) / ((1/3) asFixedPoint:4) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1054
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1055
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1056
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1057
quotientFromFraction:aFraction
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
  1058
    "Return the quotient of the argument, aFraction and the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
  1059
     Sent when aFraction does not know how to divide by the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
  1060
     Redefined here to preserve the scale."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
  1061
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1062
    ^ aFraction class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1063
        numerator:(aFraction numerator * denominator) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1064
        denominator:(aFraction denominator * numerator)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1065
        scale:scale
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1066
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1067
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1068
     ((1/3) asFixedPoint:2) / ((1/3) asFixedPoint:2)  
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1069
     ((1/3) asFixedPoint:2) / ((1/3) asFixedPoint:4)  
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1070
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1071
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1072
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1073
quotientFromInteger:anInteger
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
  1074
    "Return the quotient of the argument, anInteger and the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
  1075
     Sent when anInteger does not know how to divide by the receiver.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1076
     Redefined here to preserve the scale."
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1077
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1078
    ^ self class 
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1079
        numerator:(anInteger * denominator)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1080
        denominator:numerator
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1081
        scale:scale
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1082
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1083
    "Modified: 5.11.1996 / 10:32:35 / cg"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1084
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1085
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1086
sumFromFixedPoint:aFixedPoint
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1087
    "sent when a fixedPoint is asked to add by the receiver.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1088
     The result has the higher scale of the two operands.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1089
     Redefined here to compute the scale."
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1090
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1091
    |n d otherNumerator otherDenominator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1092
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1093
    otherNumerator := aFixedPoint numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1094
    otherDenominator := aFixedPoint denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1095
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1096
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1097
    denominator = otherDenominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1098
        n := numerator + otherNumerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1099
        d := otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1100
    ] ifFalse:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1101
        n := (numerator * otherDenominator) + (otherNumerator * denominator).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1102
        d := denominator * otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1103
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1104
    ^ self class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1105
            numerator:n 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1106
            denominator:d
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1107
            scale:(scale max:aFixedPoint scale)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1108
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1109
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1110
     ((1/3) asFixedPoint:2) + ((1/3) asFixedPoint:2)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1111
     ((1/3) asFixedPoint:2) + ((2/3) asFixedPoint:2)     
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1112
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1113
     ((1/3) asFixedPoint:2) + ((1/3) asFixedPoint:4)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1114
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1115
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1116
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1117
sumFromFraction:aFraction
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1118
    "sent when a fraction is asked to add the receiver.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1119
     The result has my scale.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1120
     Redefined here to preserve the scale."
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1121
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1122
    |n d otherNumerator otherDenominator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1123
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1124
    otherNumerator := aFraction numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1125
    otherDenominator := aFraction denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1126
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1127
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1128
    denominator = otherDenominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1129
        n := numerator + otherNumerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1130
        d := otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1131
    ] ifFalse:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1132
        n := (numerator * otherDenominator) + (otherNumerator * denominator).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1133
        d := denominator * otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1134
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1135
    ^ self class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1136
            numerator:n 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1137
            denominator:d
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1138
            scale:scale
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1139
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1140
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1141
     (1/3) + ((1/3) asFixedPoint:2)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1142
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1143
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1144
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1145
sumFromInteger:anInteger
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1146
    "sent when an integer does not know how to add the receiver.
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1147
     The result has my scale.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1148
     Redefined here to preserve the scale."
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1149
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1150
    ^ self class 
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1151
        numerator:(numerator + (anInteger * denominator))
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1152
        denominator:denominator
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
  1153
        scale:scale
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1154
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1155
    "Modified: 5.11.1996 / 10:32:43 / cg"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1156
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1157
10192
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1158
!FixedPoint methodsFor:'mathematical functions'!
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1159
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1160
sqrt
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1161
    "compute the square root, using the Newton method.
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1162
     The approximated return value has an error less than 
18837
f1835c2995f2 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17445
diff changeset
  1163
     the receiver's last digit, as specified in the scale."
10192
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1164
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1165
    ^ self
18837
f1835c2995f2 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17445
diff changeset
  1166
        sqrtWithErrorLessThan:(1 / (10 raisedTo:scale+1)) asFixedPoint
10192
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1167
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1168
    "
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1169
     (2 asFixedPoint:4) sqrt   
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1170
     (2 asFixedPoint:100) sqrt   
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1171
     (2 asFixedPoint:1000) sqrt   
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1172
     (10 asFixedPoint:100) sqrt   
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1173
     (100 asFixedPoint:100) sqrt   
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1174
    "
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1175
! !
Claus Gittinger <cg@exept.de>
parents: 9137
diff changeset
  1176
5570
e6e14f50d721 category change
Claus Gittinger <cg@exept.de>
parents: 3731
diff changeset
  1177
!FixedPoint methodsFor:'printing & storing'!
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1178
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1179
printDecimalOn:aStream roundToScale:roundToScale truncateToScale:truncateToScale
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1180
    "common helper for printing (with round or truncate)
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1181
     and storing (neither rounding, nor truncating)"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1182
9133
2d3e3d083be8 storeOn fixed for negative numbers with 0 scale
Claus Gittinger <cg@exept.de>
parents: 9132
diff changeset
  1183
    |e integerPart fractionPart negative num rest|
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1184
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1185
    numerator < 0 ifTrue:[
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1186
        negative := true.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1187
        num := numerator negated.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1188
    ] ifFalse:[
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1189
        negative := false.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1190
        num := numerator.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1191
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1192
    integerPart := (num // denominator).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1193
    fractionPart := (num \\ denominator).
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1194
    (roundToScale or:[truncateToScale]) ifTrue:[    
9131
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1195
        e := 10 raisedTo:scale.
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1196
    ] ifFalse:[
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1197
        (denominator isPowerOf:10) ifTrue:[
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1198
            e := denominator.
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1199
        ] ifFalse:[
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1200
            e := 10 raisedTo:(denominator log:10) asInteger + scale + 2.
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1201
        ].
9131
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1202
    ].
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1203
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1204
    "/ the most common case is a denominator fitting the scale
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1205
    "/ (fixedPoint numbers are created this way)
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1206
    e = denominator ifFalse:[
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1207
        fractionPart := fractionPart * (e * 10) // denominator.
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1208
        roundToScale ifTrue:[
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1209
            fractionPart := (fractionPart roundTo:10) // 10.
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1210
        ] ifFalse:[
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1211
            fractionPart := fractionPart // 10.
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1212
        ].
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1213
        fractionPart >= e ifTrue:[
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1214
            integerPart := integerPart + 1.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1215
            fractionPart := 0.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1216
        ]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1217
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1218
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1219
    "/
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1220
    "/ add a 1000..., so we can (mis-)use integer-printString ...
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1221
    "/ the highest-1 will be cutoff after padding.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1222
    "/
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1223
    fractionPart := e + fractionPart.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1224
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1225
    negative ifTrue:[
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1226
        aStream nextPut:$-
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1227
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1228
    integerPart printOn:aStream.
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1229
    (roundToScale or:[truncateToScale]) ifTrue:[
9131
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1230
        scale > 0 ifTrue:[
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1231
            aStream nextPut: $..
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1232
            ((fractionPart printStringPaddedTo:scale with:$0) copyFrom:2) printOn:aStream
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1233
        ].
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1234
    ] ifFalse:[
9133
2d3e3d083be8 storeOn fixed for negative numbers with 0 scale
Claus Gittinger <cg@exept.de>
parents: 9132
diff changeset
  1235
        rest := ((fractionPart printString) copyFrom:2).
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1236
        scale > 0 ifTrue:[
9133
2d3e3d083be8 storeOn fixed for negative numbers with 0 scale
Claus Gittinger <cg@exept.de>
parents: 9132
diff changeset
  1237
            aStream nextPut: $..
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1238
            (rest paddedTo:scale with:$0) printOn:aStream
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1239
        ] ifFalse:[
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1240
            rest notEmpty ifTrue:[
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1241
                aStream nextPut: $..
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1242
                rest printOn:aStream
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1243
            ]
9133
2d3e3d083be8 storeOn fixed for negative numbers with 0 scale
Claus Gittinger <cg@exept.de>
parents: 9132
diff changeset
  1244
        ]
6897
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
  1245
    ].
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1246
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1247
    "
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1248
    ((FixedPoint fromString:'0.66666666') withScale:2)
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1249
        printDecimalOn:Transcript roundToScale:false truncateToScale:false
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1250
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1251
    ((FixedPoint fromString:'0.66666666') withScale:2)
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1252
        printDecimalOn:Transcript roundToScale:true truncateToScale:false
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1253
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1254
    ((FixedPoint fromString:'0.66666666') withScale:2)
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1255
        printDecimalOn:Transcript roundToScale:false truncateToScale:true
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1256
    "
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1257
!
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1258
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1259
printOn:aStream 
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1260
    "append to the argument, aStream, a printed representation of the receiver.
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1261
     For printout, only scale post-decimal digits are printed
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1262
     (By default, the printout is rounded to that many digits)"
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1263
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1264
    PrintTruncated == true ifTrue:[
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1265
        self printTruncatedOn:aStream
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1266
    ] ifFalse:[
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1267
        self printRoundedOn:aStream
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1268
    ].
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1269
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1270
    "
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1271
     (FixedPoint fromString:'0.66666666')               
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1272
     (FixedPoint fromString:'0.66666666') withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1273
     (FixedPoint fromString:'0.99999999')               
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1274
     (FixedPoint fromString:'0.99999999') withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1275
     (FixedPoint fromString:'1.00000001')               
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1276
     (FixedPoint fromString:'1.00000001') withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1277
     (FixedPoint fromString:'1.005')                    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1278
     (FixedPoint fromString:'1.005') withScale:2        
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1279
     (FixedPoint fromString:'1.005') withScale:1        
6897
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
  1280
     (FixedPoint fromString:'1.5')                    
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
  1281
     (FixedPoint fromString:'1.5') withScale:2        
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
  1282
     (FixedPoint fromString:'1.5') withScale:1        
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
  1283
     (FixedPoint fromString:'1.5') withScale:0        
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1284
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1285
     (FixedPoint fromString:'-0.66666666')              
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1286
     (FixedPoint fromString:'-0.66666666') withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1287
     (FixedPoint fromString:'-0.99999999')              
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1288
     (FixedPoint fromString:'-0.99999999') withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1289
     (FixedPoint fromString:'-1.00000001')              
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1290
     (FixedPoint fromString:'-1.00000001') withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1291
     (FixedPoint fromString:'-1.005')                   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1292
     (FixedPoint fromString:'-1.005') withScale:2       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1293
     (FixedPoint fromString:'-1.005') withScale:1       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1294
     (FixedPoint fromString:'-1.05')                    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1295
     (FixedPoint fromString:'-1.05') withScale:2      
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1296
     (FixedPoint fromString:'-1.05') withScale:1      
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1297
     (FixedPoint fromString:'-1.04')                  
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1298
     (FixedPoint fromString:'-1.04') withScale:2      
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1299
     (FixedPoint fromString:'-1.04') withScale:1      
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1300
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1301
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1302
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1303
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1304
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1305
     a := (FixedPoint fromString:'0.66666666') withScale:2.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1306
     b := (FixedPoint fromString:'0.33333333').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1307
     r := (a + b) withScale:4.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1308
     Transcript show:'printout with scale of 4 :'; showCR:r.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1309
     Transcript show:'more precise value       :'; showCR:(r withScale:8)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1310
    "
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1311
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1312
    "Modified: 12.4.1997 / 11:20:51 / cg"
9131
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1313
!
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1314
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1315
printRoundedOn:aStream 
9131
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1316
    "append to the argument, aStream, a printed representation of the receiver.
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1317
     For printout, only scale post-decimal digits are printed,
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1318
     the printout is rounded to that many digits"
9131
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1319
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1320
    self printDecimalOn:aStream roundToScale:true truncateToScale:false
9131
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1321
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1322
    "
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1323
     ((FixedPoint fromString:'0.66666666') withScale:2) printRoundedOn:Transcript   
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1324
     ((FixedPoint fromString:'0.66666666') withScale:2) printTruncatedOn:Transcript   
9131
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1325
    "
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1326
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1327
    "Modified: 12.4.1997 / 11:20:51 / cg"
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1328
!
9131
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1329
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1330
printTruncatedOn:aStream 
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1331
    "append to the argument, aStream, a printed representation of the receiver.
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1332
     For printout, only scale post-decimal digits are printed,
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1333
     the printout is truncated to that many digits"
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1334
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1335
    self printDecimalOn:aStream roundToScale:false truncateToScale:true
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1336
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1337
    "
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1338
     ((FixedPoint fromString:'0.66666666') withScale:2) printRoundedOn:Transcript   
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1339
     ((FixedPoint fromString:'0.66666666') withScale:2) printTruncatedOn:Transcript   
9131
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1340
    "
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1341
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1342
    "Modified: 12.4.1997 / 11:20:51 / cg"
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1343
!
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1344
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1345
storeOn:aStream
9132
df7fe5738d18 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9131
diff changeset
  1346
    "notice: we MUST preserve the full internal precision when storing/reloading"
df7fe5738d18 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9131
diff changeset
  1347
9137
9692c2409b38 printing fixed;
Claus Gittinger <cg@exept.de>
parents: 9133
diff changeset
  1348
    self printDecimalOn:aStream roundToScale:false truncateToScale:false.
9131
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1349
    aStream nextPut:$s.
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1350
    scale storeOn:aStream.
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1351
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1352
    "
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1353
     ((FixedPoint fromString:'0.66666666')              ) storeString 
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1354
     ((FixedPoint fromString:'0.66666666') withScale:2  ) storeString  
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1355
     ((FixedPoint fromString:'1.5')                     ) storeString 
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1356
     ((FixedPoint fromString:'1.5') withScale:2         ) storeString 
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1357
     ((FixedPoint fromString:'1.5') withScale:1         ) storeString 
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1358
     ((FixedPoint fromString:'1.5') withScale:0         ) storeString 
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1359
    "
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1360
!
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1361
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1362
storeString 
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1363
    ^ String streamContents:[:s | self storeOn:s]
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1364
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1365
    "
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1366
     ((FixedPoint fromString:'0.66666666')             ) storeString 
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1367
     ((FixedPoint fromString:'0.66666666') withScale:2 ) storeString  
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1368
     ((FixedPoint fromString:'1.5')                     ) storeString 
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1369
     ((FixedPoint fromString:'1.5') withScale:2         ) storeString 
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1370
     ((FixedPoint fromString:'1.5') withScale:1         ) storeString 
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1371
     ((FixedPoint fromString:'1.5') withScale:0         ) storeString 
fcbe5d33e038 storeOn / readFrom fixed
Claus Gittinger <cg@exept.de>
parents: 9129
diff changeset
  1372
    "
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1373
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1374
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1375
!FixedPoint methodsFor:'private'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1376
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1377
reduced
11251
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1378
    "reduce the receiver; divide the numerator and denominator by their
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1379
     greatest common divisor; if the result is integral, return an Integer.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1380
     Otherwise, return the normalized receiver.
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1381
     CAVEAT: bad name; should be called reduce, as it has a side effect
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1382
     (i.e. this is destructive wrt. the instance values)."
c7ff5dc56002 comments
Claus Gittinger <cg@exept.de>
parents: 11237
diff changeset
  1383
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1384
    |gc|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1385
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1386
    scale isNil ifTrue:[
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1387
        "/ to catch inherited Fraction reduce calls
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1388
        self error:'should not happen'.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1389
        scale := 3
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1390
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1391
6573
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1392
    (denominator < 0) ifTrue:[
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1393
        numerator := numerator negated.
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1394
        denominator := denominator negated
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1395
    ].
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1396
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1397
    denominator == 1 ifTrue:[^ numerator].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1398
    numerator == 1 ifTrue:[^ self].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1399
    numerator == 0 ifTrue:[^ 0].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1400
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1401
    gc := numerator gcd:denominator.
6573
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1402
    gc < 0 ifTrue:[
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1403
        gc := gc negated
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1404
    ].
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1405
    gc := gc gcd:(10 raisedToInteger:scale).
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1406
6573
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1407
    (gc ~~ 1) ifTrue:[
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1408
        numerator := numerator // gc.
6573
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1409
        denominator := denominator // gc.
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1410
        denominator == 1 ifTrue:[^ numerator].
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1411
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1412
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1413
    ^ self
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1414
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1415
3731
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1416
scale:newScale 
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1417
    "set the scale."
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1418
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1419
    scale := newScale.
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1420
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1421
    "Modified: / 12.4.1997 / 11:22:02 / cg"
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1422
    "Created: / 5.8.1998 / 13:28:49 / cg"
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1423
!
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1424
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1425
setNumerator:nInteger denominator:d scale:s 
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1426
    "initialize the instance variables.
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1427
     Assumes that the fraction as specified by numerator and denominator
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1428
     is already reduced."
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1429
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1430
    scale := s.
11237
ba77199427ce inlined private inst-setters to eliminate a message send
Claus Gittinger <cg@exept.de>
parents: 10192
diff changeset
  1431
    numerator := nInteger.
ba77199427ce inlined private inst-setters to eliminate a message send
Claus Gittinger <cg@exept.de>
parents: 10192
diff changeset
  1432
    denominator := d
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1433
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1434
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1435
setNumerator:nInteger scale:s 
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
  1436
    "initialize the instance variables, given an integer"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1437
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1438
    scale := s.
11237
ba77199427ce inlined private inst-setters to eliminate a message send
Claus Gittinger <cg@exept.de>
parents: 10192
diff changeset
  1439
    numerator := nInteger.
ba77199427ce inlined private inst-setters to eliminate a message send
Claus Gittinger <cg@exept.de>
parents: 10192
diff changeset
  1440
    denominator := (10 raisedTo:s)
21945
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
  1441
fb468e1aac6b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21818
diff changeset
  1442
    "Modified (comment): / 03-07-2017 / 12:34:21 / cg"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1443
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1444
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1445
setScale:newScale 
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1446
    "initialize the scale instance variables."
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1447
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1448
    scale := newScale.
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1449
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1450
    "Modified: 12.4.1997 / 11:22:02 / cg"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1451
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1452
6650
35de1d8400b2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6645
diff changeset
  1453
!FixedPoint methodsFor:'testing'!
1891
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1454
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1455
isFixedPoint
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1456
    "return true, if the receiver is some kind of fixedPoint number;
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1457
     true is returned here - the method is redefined from Object."
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1458
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1459
    ^ true
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1460
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1461
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1462
! !
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1463
7376
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1464
!FixedPoint methodsFor:'truncation & rounding'!
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1465
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1466
roundedToScale
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1467
    "return the receiver rounded to my scale 
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1468
     (i.e. return a number which has the value which is shown when printing)"
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1469
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1470
    |n scale10|
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1471
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1472
    scale10 := 10 raisedToInteger:scale.
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1473
    n := ((numerator * scale10) + (denominator bitShift:-1)) // denominator.
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1474
    ^ self class
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1475
        numerator:n
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1476
        denominator:scale10
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1477
        scale:scale.
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1478
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1479
    "
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1480
     ((2/3) asFixedPoint:2)                     
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1481
     ((2/3) asFixedPoint:2) rounded          
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1482
     ((2/3) asFixedPoint:2) roundedToScale     
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1483
    "
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1484
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1485
    "Modified: 5.11.1996 / 12:18:46 / cg"
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1486
!
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1487
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1488
truncatedToScale
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1489
    "return the receiver truncated towards zero to my scale"
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1490
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1491
    |n scale10|
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1492
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1493
    scale10 := 10 raisedToInteger:scale.
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1494
    n := (numerator * scale10) quo:denominator.
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1495
    ^ self class
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1496
        numerator:n
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1497
        denominator:scale10
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1498
        scale:scale.
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1499
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1500
    "
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1501
     ((2/3) asFixedPoint:2)                     
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1502
     ((2/3) asFixedPoint:2) truncated          
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1503
     ((2/3) asFixedPoint:2) truncatedToScale     
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1504
    "
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1505
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1506
    "Modified: 5.11.1996 / 12:18:46 / cg"
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1507
! !
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1508
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1509
!FixedPoint class methodsFor:'documentation'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1510
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1511
version
18837
f1835c2995f2 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17445
diff changeset
  1512
    ^ '$Header$'
12724
f4f74122418b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11900
diff changeset
  1513
!
f4f74122418b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11900
diff changeset
  1514
f4f74122418b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11900
diff changeset
  1515
version_CVS
18837
f1835c2995f2 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17445
diff changeset
  1516
    ^ '$Header$'
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1517
! !
15613
f3247f1af412 class: FixedPoint
Stefan Vogel <sv@exept.de>
parents: 13147
diff changeset
  1518