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