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