FixedPoint.st
author Claus Gittinger <cg@exept.de>
Fri, 17 Feb 2006 13:35:04 +0100
changeset 9129 f1451239cd4b
parent 7442 1b0a20747a64
child 9131 fcbe5d33e038
permissions -rw-r--r--
comment
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
"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
5570
e6e14f50d721 category change
Claus Gittinger <cg@exept.de>
parents: 3731
diff changeset
    15
"{ Package: 'stx:libbasic' }"
e6e14f50d721 category change
Claus Gittinger <cg@exept.de>
parents: 3731
diff changeset
    16
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
Fraction subclass:#FixedPoint
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
    18
	instanceVariableNames:'scale'
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
    19
	classVariableNames:'PI_1000'
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
    20
	poolDictionaries:''
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
    21
	category:'Magnitude-Numbers'
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
FixedPoint comment:'
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
Description: This class implements infinite precision fixed-point numbers. 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
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
    27
since its superclass Fraction does all the work.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
Test: "''123456789012345678901234567890.123456789'' asFixed * 1000000000 = 123456789012345678901234567890123456789"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
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
    32
          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
    33
!
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
!FixedPoint class methodsFor:'documentation'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
copyright
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
"
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
    39
     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
    40
     Code is provided 'as is', as a goody, without any warranty.
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
    41
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
     this comes from:
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
     Jan Steinman, Bytesmiths
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
     2002 Parkside Court, West Linn, OR 97068-2767 USA, +1 503 657 7703
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
     Friedlistrasse 19, CH-3006, Bern, Switzerland, +41 31 999 3946
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
     this code was published in comp.lang.smalltalk; 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
     added here as an example ...
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
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
documentation
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
"
7380
c704ff45bb80 documentation
Claus Gittinger <cg@exept.de>
parents: 7376
diff changeset
    55
    This class implements infinite precision fixed-point numbers,
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
    which internally hold exact (fractional) results, but print themself with
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
    a limited number of digits after the decimal point. 
7380
c704ff45bb80 documentation
Claus Gittinger <cg@exept.de>
parents: 7376
diff changeset
    58
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
    These can be used in computation, where rounding errors should not accumulate,
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
    but only a limited precision is required for the final result.
1898
883a1046ca9a commentary
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
    61
    (i.e. business applications)
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
    It doesn't really do anything too interesting except creating instances, 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
    converting, and printing, since its superclass Fraction does all the work.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
    Test: 
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
    67
        '123456789012345678901234567890.123456789' asFixedPoint * 1000000000
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
        -> 123456789012345678901234567890123456789'
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
    Notes: 1) The current implementation does not convert arbitrarily-based 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
              String representations, which shouldn't be too much a problem 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
              for financial types.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
           2) the implementation is a hack - it has not been optimized for speed
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
              in particular.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
    77
    Mixed mode arithmetic:
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
    78
        fix op fix       -> fix, scale is max. of operands
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
    79
        fix op fraction  -> fix; scale is fix's scale
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
    80
        fix op integer   -> fix; scale is fix's scale
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
    81
        fix op float     -> float
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
    82
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
    [author:]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
        Jan Steinman, Bytesmiths
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
    85
        adapted, modified & enhanced by Claus Gittinger
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
    [see also:]
9129
f1451239cd4b comment
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
    88
        Number Fraction Integer Float ShortFloat LongFloat Complex
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
examples
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
                                                                [exBegin]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
     a := (FixedPoint fromString:'123.456').
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
    98
     b := '1.10' asFixedPoint.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
     r := a + b.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
     Transcript showCR:r.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
     Transcript showCR:(r withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
     Transcript showCR:(r rounded).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
                                                                [exEnd]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
                                                                [exBegin]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
     a := (FixedPoint fromString:'0.9999999').
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   110
     b := 0.0000001 asFixedPoint. 
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
     r := a + b.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
     Transcript showCR:r.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
     Transcript showCR:(r withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
     Transcript showCR:(r rounded).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
                                                                [exEnd]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
                                                                [exBegin]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
     a := (FixedPoint fromString:'0.9999998').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
     r := a + b.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
     Transcript showCR:r.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
     Transcript showCR:(r withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
     Transcript showCR:(r rounded).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
                                                                [exEnd]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
                                                                [exBegin]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
     a := (FixedPoint fromString:'1.0').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
     r := a + b.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
     Transcript showCR:r.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
     Transcript showCR:(r withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
     Transcript showCR:(r rounded).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
                                                                [exEnd]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
                                                                [exBegin]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
     r := a + b.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
     Transcript showCR:r.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
     Transcript showCR:(r withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
     Transcript showCR:(r rounded).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
                                                                [exEnd]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
!FixedPoint class methodsFor:'instance creation'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
6237
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   159
numerator:n denominator:d 
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   160
    self shouldNotImplement. "use #numerator:denominator:scale"
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   161
    "/ ^ 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
   162
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   163
    "
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   164
     self numerator:123 denominator:100    
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   165
    "
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   166
!
4dc908af4f5c report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents: 5570
diff changeset
   167
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
numerator:n denominator:d scale:s
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
    ^ self basicNew
6894
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   170
        setNumerator:n denominator:d scale:s
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   171
        "/ ; reduced
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   174
readFrom:aStringOrStream 
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   175
    "return the next FixedPoint from the (character-)stream aStream. 
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   176
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   177
     NOTICE:   
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   178
       This behaves different from the default readFrom:, in returning
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   179
       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
   180
       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
   181
       - depending on the upcoming ANSI standard, this may change."
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   182
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   183
    ^ self readFrom:aStringOrStream onError:0
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   185
    "
6636
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   186
     FixedPoint readFrom:'123.456'
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   187
     FixedPoint readFrom:'3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788'
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   188
     FixedPoint readFrom:(ReadStream on:'foobar')     
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   189
     FixedPoint readFrom:(ReadStream on:'foobar') onError:nil  
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   190
    "
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   191
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2563
diff changeset
   192
    "Modified: / 25.10.1997 / 15:30:29 / cg"
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   193
!
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   194
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   195
readFrom:aStringOrStream onError:exceptionBlock
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   196
    "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
   197
     If an error occurs during conversion, return the result
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   198
     from evaluating exceptionBlock"
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   199
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   200
    | aStream sign integerPart fractionStream char fractionPart scale |
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   201
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   202
    aStream := aStringOrStream readStream.
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   203
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   204
    aStream peek == $- ifTrue:[
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   205
        sign := -1.
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   206
        aStream next.
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   207
    ] ifFalse:[
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   208
        sign := 1
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   209
    ].
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   210
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   211
    (aStream atEnd or:[aStream peek isLetter]) ifTrue: [^ exceptionBlock value].
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   212
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   213
    integerPart := (aStream upTo:$.) asNumber.
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   214
    (aStream atEnd or: [aStream peek isLetter]) ifTrue: [
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   215
        fractionPart := 0.
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   216
        scale := 1.
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   217
    ] ifFalse:[
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   218
        fractionStream := ReadWriteStream on:(String new: 10).
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   219
        [
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   220
            char := aStream next.
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   221
            char ~~ nil and:[char isDigit]
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   222
        ] whileTrue:[
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   223
            fractionStream nextPut:char
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   224
        ].
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   225
7053
13e04c48e23c prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 6898
diff changeset
   226
        scale := fractionStream position0Based.
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   227
        fractionStream reset.
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   228
        fractionPart := Number readFrom:fractionStream.
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   229
    ].
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
    ^ self basicNew 
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   232
        setNumerator:(integerPart * (10 raisedTo:scale) + fractionPart) * sign
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   233
        scale:scale
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   234
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   235
    "
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   236
     FixedPoint readFrom:'1.00' 
7053
13e04c48e23c prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 6898
diff changeset
   237
     FixedPoint readFrom:'123.456' 
6895
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   238
     FixedPoint readFrom:'-123.456' 
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   239
     FixedPoint readFrom:'123' 
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   240
     FixedPoint readFrom:'-123' 
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   241
     FixedPoint readFrom:'-123.abcd' onError:[47.5] 
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   242
     FixedPoint readFrom:'-1a.bcd' onError:[47.5] 
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   243
     FixedPoint readFrom:'foot' onError:['bad fixedpoint'] 
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   244
    "
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   245
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   246
    "Created: / 25.10.1997 / 15:28:59 / cg"
2434bee328c1 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6894
diff changeset
   247
    "Modified: / 25.10.1997 / 15:31:47 / cg"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
6636
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   250
!FixedPoint class methodsFor:'constants'!
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   251
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   252
pi
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   253
    "pi with 1000 valid digits..."
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   254
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   255
    PI_1000 isNil ifTrue:[
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   256
        PI_1000 := self
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   257
                     readFrom:'3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788'
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   258
    ].
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   259
    ^ PI_1000.
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   260
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   261
    "
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   262
     self pi squared    
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   263
     self pi reciprocal 
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   264
     1 / self pi 
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   265
     self pi * 1000000000000000000000000000000000000000000    
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   266
    "
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   267
! !
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   268
6898
d0182256f3ff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6897
diff changeset
   269
!FixedPoint class methodsFor:'queries'!
d0182256f3ff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6897
diff changeset
   270
d0182256f3ff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6897
diff changeset
   271
exponentCharacter
d0182256f3ff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6897
diff changeset
   272
    ^ $s
d0182256f3ff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6897
diff changeset
   273
! !
d0182256f3ff *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6897
diff changeset
   274
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
!FixedPoint methodsFor:'accessing'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
scale
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   278
    "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
   279
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
    ^ scale
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   281
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   282
    "Modified: 12.4.1997 / 11:21:05 / cg"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
!FixedPoint methodsFor:'arithmetic'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   287
* aNumber
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
    "return the product of the receiver and the argument, aNumber.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
     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
   290
     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
   291
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   292
    "/ notice:
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   293
    "/ the following code handles some common cases,
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   294
    "/ and exists as an optimization, to speed up those cases.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   295
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   296
    "/ a message send; otherwise double-dispatch is just as fast.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   297
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   298
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   299
        ^ self class 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
                numerator:(numerator * aNumber)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
                denominator:denominator
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
                scale:scale
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   303
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   304
    ^ aNumber productFromFixedPoint:self
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   305
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   306
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   307
     |a r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   308
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   309
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   310
     r := a * 5.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   311
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   315
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   316
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   317
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   318
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   319
     b := (FixedPoint fromString:'1.10').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   320
     r := a * b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   321
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   322
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   323
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   324
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   325
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   326
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   327
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   328
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   329
     b := (FixedPoint fromString:'-1.10').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   330
     r := a * b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   331
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   332
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   333
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   335
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   336
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   337
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   338
     a := (FixedPoint fromString:'0.9999999').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   339
     b := (FixedPoint fromString:'0.9999999').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   340
     r := a * b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   341
     Transcript show:'fixed (exact)  : '; showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   342
     Transcript show:'fixed (scale2) : '; showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   343
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   344
     Transcript show:'float (inexact): '; showCR:(0.9999999 * 0.9999999).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   345
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   346
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   347
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   348
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   349
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   350
     a := 1.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   351
     b := (FixedPoint fromString:'0.9999999').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   352
     r := a * b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   353
     Transcript show:'fixed (exact)  : '; showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   354
     Transcript show:'fixed (scale2) : '; showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   355
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   356
     Transcript show:'float (inexact): '; showCR:(0.9999999 * 0.9999999).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   357
    "
6636
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   358
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   359
    "                       
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   360
     |a r|
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   361
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   362
     a := (FixedPoint fromString:'123.456').
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   363
     r := a * 5.0.    
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   364
     Transcript showCR:r.    
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   365
     Transcript showCR:(r withScale:2).
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   366
    "
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   367
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   368
    "                       
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   369
     |a r|
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   370
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   371
     a := (FixedPoint fromString:'123.456').
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   372
     r := 5.0 * a.    
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   373
     Transcript showCR:r.    
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   374
     Transcript showCR:(r withScale:2).
dedc341eb3ab arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents: 6625
diff changeset
   375
    "
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   378
+ aNumber
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   379
    "return the sum of the receiver and the argument, aNumber.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   380
     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
   381
     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
   382
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   383
    |n|
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   384
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   385
    "/ notice:
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   386
    "/ the following code handles some common cases,
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   387
    "/ and exists as an optimization, to speed up those cases.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   388
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   389
    "/ a message send; otherwise double-dispatch is just as fast.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   390
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   391
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   392
        "save a multiplication if possible"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   393
        denominator == 1 ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   394
            n := numerator + aNumber.
6645
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   395
        ] ifFalse:[
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   396
            n := numerator + (aNumber * denominator).
6645
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   397
        ].
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   398
        ^ self class 
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   399
            numerator:n 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   400
            denominator:denominator
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   401
            scale:scale
6645
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   402
    ].
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
    ^ aNumber sumFromFixedPoint:self
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   404
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   405
    "
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   406
     ((1/3) asFixedPoint:2) + 1    
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   407
     ((1/3) asFixedPoint:2) + (1/3)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   408
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   409
    "
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   410
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   411
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   412
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   413
     b := (FixedPoint fromString:'1.10').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   414
     a + b
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   415
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   416
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   417
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   418
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   419
     a := (FixedPoint fromString:'0.9999999').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   420
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   421
     a + b                   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   422
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   423
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   424
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   425
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   426
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   427
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   428
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   429
     a + b                             
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   430
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   431
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   432
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   433
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   434
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   435
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   436
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   437
     (a + b) withScale:2  
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   438
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   439
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   440
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   441
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   442
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   443
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   444
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   445
     (a + b) withScale:1  
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   446
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   447
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   448
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   449
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   450
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   451
     a := 1.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   452
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   453
     Transcript showCR:((a + b) withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   454
     Transcript showCR:(a + b)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   455
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   456
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   457
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   458
- aNumber
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   459
    "return the difference of the receiver and the argument, aNumber.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   460
     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
   461
     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
   462
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   463
    |n|
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   464
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   465
    "/ notice:
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   466
    "/ the following code handles some common cases,
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   467
    "/ and exists as an optimization, to speed up those cases.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   468
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   469
    "/ a message send; otherwise double-dispatch is just as fast.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   470
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   471
    (aNumber isMemberOf:SmallInteger) ifTrue:[
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   472
        "save a multiplication if possible"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   473
        denominator == 1 ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   474
            n := numerator - aNumber.
6645
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   475
        ] ifFalse:[
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   476
            n := numerator - (aNumber * denominator).
6645
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   477
        ].
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   478
        ^ self class 
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   479
            numerator:n 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   480
            denominator:denominator
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   481
            scale:scale
6645
b25be4db9230 arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents: 6638
diff changeset
   482
    ].
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   483
    ^ aNumber differenceFromFixedPoint:self
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   484
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   485
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   486
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   487
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   488
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   489
     b := (FixedPoint fromString:'1.10').
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   490
     a - b     
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   491
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   492
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   493
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   494
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   495
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   496
     a := (FixedPoint fromString:'0.9999999').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   497
     b := (FixedPoint fromString:'0.0000009').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   498
     a - b                   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   499
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   500
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   501
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   502
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   503
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   504
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   505
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   506
     a - b                          
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   507
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   508
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   509
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   510
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   511
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   512
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   513
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   514
     (a - b) withScale:2  
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   515
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   516
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   517
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   518
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   519
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   520
     a := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   521
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   522
     (a - b) withScale:1  
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   523
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   524
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   525
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   526
     |a b|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   527
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   528
     a := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   529
     b := (FixedPoint fromString:'0.99').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   530
     (a - b) withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   531
    "
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 := 1.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   537
     b := (FixedPoint fromString:'0.0000001').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   538
     Transcript showCR:((a - b) withScale:1).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   539
     Transcript showCR:(a - b)
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
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   543
/ aNumber
1898
883a1046ca9a commentary
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   544
    "return the quotient of the receiver and the argument, aNumber.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   545
     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
   546
     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
   547
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   548
    "/ notice:
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   549
    "/ the following code handles some common cases,
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   550
    "/ and exists as an optimization, to speed up those cases.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   551
    "/ also notice, that checks for those cases must be inlinable without
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   552
    "/ a message send; otherwise double-dispatch is just as fast.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   553
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   554
    (aNumber isMemberOf:SmallInteger) ifTrue:[
6886
7acddeb0e526 checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6704
diff changeset
   555
        ^ self class 
6894
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   556
                numerator:numerator
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   557
                denominator:(denominator * aNumber)
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   558
                scale:scale
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   559
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   560
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   561
    ^ aNumber quotientFromFixedPoint:self
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   562
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   563
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   564
     |a r|                     
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 := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   567
     r := a / 5.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   568
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   569
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   570
     Transcript showCR:(r withScale:9).
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 r|
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:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   577
     b := (FixedPoint fromString:'1.10').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   578
     r := a / b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   579
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   580
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   581
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   582
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   583
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   584
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   585
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   586
     a := (FixedPoint fromString:'-123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   587
     b := (FixedPoint fromString:'-1.10').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   588
     r := a / b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   589
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   590
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   591
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   593
    "                       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   594
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   595
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   596
     a := (FixedPoint fromString:'123.456').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   597
     b := (FixedPoint fromString:'-1.10').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   598
     r := a / b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   599
     Transcript showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   600
     Transcript showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   601
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   602
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   603
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   604
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   605
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   606
     a := 1.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   607
     b := (FixedPoint fromString:'0.9999999').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   608
     r := a / b.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   609
     Transcript show:'fixed (exact)  : '; showCR:r.    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   610
     Transcript show:'fixed (scale2) : '; showCR:(r withScale:2).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   611
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   612
     Transcript show:'float (inexact): '; showCR:(1 / 0.9999999).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   613
    "
6570
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   614
!
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   615
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   616
negated
6894
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   617
    "redefined from Fraction to preserve scale"
6570
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   618
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   619
    ^ self class 
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   620
        numerator:(numerator negated)
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   621
        denominator:denominator
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   622
        scale:scale
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   623
!
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   624
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   625
reciprocal
6894
64f27ed2753f do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents: 6886
diff changeset
   626
    "redefined from Fraction to preserve scale"
6570
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   627
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   628
    numerator == 1 ifTrue:[^ denominator].
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   629
    ^ self class 
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   630
        numerator:denominator
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   631
        denominator:numerator
1d115f1687e3 Fix #negated and #reciprocal
Stefan Vogel <sv@exept.de>
parents: 6237
diff changeset
   632
        scale:scale
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   633
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   634
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   635
!FixedPoint methodsFor:'coercing & converting'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   636
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   637
asFixedPoint
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   638
    "return the receiver as a fixedPoint number - thats the receiver itself"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   639
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   640
    ^ self
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   641
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   642
    "Modified: 10.1.1997 / 19:53:14 / cg"
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   643
!
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   644
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   645
asFixedPoint:newScale
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   646
    "return a fixedPoint with the same value as the receiver, 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   647
     and newScale number of post-decimal digits"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   648
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   649
    newScale == scale ifTrue:[^ self].
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   650
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   651
    ^ self class
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   652
        numerator:numerator
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   653
        denominator:denominator
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   654
        scale:newScale
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   655
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   656
    "
6638
7523142a1152 comment
Claus Gittinger <cg@exept.de>
parents: 6636
diff changeset
   657
     '12345.12345' asFixedPoint:2   
2140
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   658
     (FixedPoint fromString:'12345.12345') asFixedPoint:2 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   659
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   660
     ((FixedPoint fromString:'0.33333333')
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   661
      + 
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   662
      (FixedPoint fromString:'0.33333333')
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   663
     ) asFixedPoint:2   
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   664
    "
5e2def558185 added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   665
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   666
    "Modified: 12.4.1997 / 11:20:37 / cg"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   667
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   668
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   669
asFraction
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   670
    "return the receiver as a fraction"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   671
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   672
    ^ Fraction
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   673
        numerator:numerator
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   674
        denominator:denominator
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   675
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   676
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   677
     (FixedPoint fromString:'0.2')           
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   678
     (FixedPoint fromString:'0.2') asFraction
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   679
     (FixedPoint fromString:'0.2') asFloat
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   680
     (FixedPoint fromString:'0.2') asShortFloat
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   681
     (FixedPoint fromString:'0.2') asInteger
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   682
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   683
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   684
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   685
coerce:aNumber
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   686
    "return aNumber converted into receivers type"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   687
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   688
    ^ aNumber asFixedPoint
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   689
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   690
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   691
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   692
generality
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   693
    "return the generality value - see ArithmeticValue>>retry:coercing:"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   694
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   695
    ^ 65
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   696
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   697
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   698
     (FixedPoint fromString:'1.001') + 1      
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   699
     (FixedPoint fromString:'1.001') + 1.0    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   700
     (FixedPoint fromString:'1.001') + (1/2)   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   701
     (FixedPoint fromString:'1.001') + 1.0 asShortFloat 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   702
     (FixedPoint fromString:'1.001') + 1 asLargeInteger 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   703
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   704
     1 + (FixedPoint fromString:'1.001') 
1892
d3564145c15c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1891
diff changeset
   705
     1.0 + (FixedPoint fromString:'1.001')      
1891
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
   706
     (1/2) + (FixedPoint fromString:'1.001')    
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   707
     1.0 asShortFloat + (FixedPoint fromString:'1.001')
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   708
     1 asLargeInteger + (FixedPoint fromString:'1.001')
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   709
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   710
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   711
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   712
!FixedPoint methodsFor:'double dispatching'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   713
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   714
differenceFromFixedPoint:aFixedPoint
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   715
    |n d otherNumerator otherDenominator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   716
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   717
    otherNumerator := aFixedPoint numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   718
    otherDenominator := aFixedPoint denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   719
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   720
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   721
    denominator = otherDenominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   722
        n := otherNumerator - numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   723
        d := otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   724
    ] ifFalse:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   725
        n := (otherNumerator * denominator) - (numerator * otherDenominator).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   726
        d := denominator * otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   727
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   728
    ^ self class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   729
            numerator:n 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   730
            denominator:d
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   731
            scale:(scale max:aFixedPoint scale)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   732
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   733
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   734
     ((1/3) asFixedPoint:2) - ((1/3) asFixedPoint:2)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   735
     ((1/3) asFixedPoint:2) - ((2/3) asFixedPoint:2)     
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   736
     (1/3) - ((2/3) asFixedPoint:2)     
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   737
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   738
     ((1/3) asFixedPoint:2) - ((1/3) asFixedPoint:4)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   739
     ((1/3) asFixedPoint:2) - ((2/3) asFixedPoint:4)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   740
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   741
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   742
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   743
differenceFromFraction:aFraction
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   744
    |n d otherNumerator otherDenominator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   745
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   746
    otherNumerator := aFraction numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   747
    otherDenominator := aFraction denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   748
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   749
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   750
    denominator = otherDenominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   751
        n := otherNumerator - numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   752
        d := otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   753
    ] ifFalse:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   754
        n := (otherNumerator * denominator) - (numerator * otherDenominator).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   755
        d := denominator * otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   756
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   757
    ^ self class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   758
            numerator:n 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   759
            denominator:d
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   760
            scale:scale
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   761
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   762
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   763
     (1/3) - ((2/3) asFixedPoint:2)     
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   764
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   765
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   766
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   767
differenceFromInteger:anInteger
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   768
    "sent when an integer does not know how to subtract the receiver.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   769
     Redefined here to preserve the scale."
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   770
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   771
    ^ self class 
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   772
        numerator:((anInteger * denominator) - numerator)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   773
        denominator:denominator
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   774
        scale:scale
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   775
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   776
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   777
productFromFixedPoint:aFixedPoint
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   778
    ^ aFixedPoint class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   779
        numerator:(aFixedPoint numerator * numerator) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   780
        denominator:(aFixedPoint denominator * denominator)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   781
        scale:(scale max:aFixedPoint scale)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   782
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   783
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   784
     ((1/3) asFixedPoint:2) * ((1/3) asFixedPoint:4)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   785
     (1/3) * ((1/3) asFixedPoint:4)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   786
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   787
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   788
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   789
productFromFraction:aFraction
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:(aFraction numerator * numerator) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   792
        denominator:(aFraction denominator * denominator)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   793
        scale: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) * ((1/3) asFixedPoint:4) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   797
    "
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   798
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   799
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   800
productFromInteger:anInteger
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   801
    "sent when an integer does not know how to multiply the receiver.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   802
     Redefined here to preserve the scale."
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   803
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   804
    ^ self class 
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   805
        numerator:(anInteger * numerator)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   806
        denominator:denominator
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   807
        scale:scale
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   808
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   809
    "Modified: 5.11.1996 / 10:32:28 / cg"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   810
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   811
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   812
quotientFromFixedPoint:aFixedPoint
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
   813
    "Return the quotient of the argument, aFixedPoint and the receiver."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
   814
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   815
    ^ aFixedPoint class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   816
        numerator:(aFixedPoint numerator * denominator) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   817
        denominator:(aFixedPoint denominator * numerator)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   818
        scale:(scale max:aFixedPoint scale)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   819
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   820
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   821
     ((1/3) asFixedPoint:2) / ((1/3) asFixedPoint:2) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   822
     ((1/3) asFixedPoint:2) / ((1/3) asFixedPoint:4) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   823
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   824
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   825
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   826
quotientFromFraction:aFraction
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
   827
    "Return the quotient of the argument, aFraction and the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
   828
     Sent when aFraction does not know how to divide by the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
   829
     Redefined here to preserve the scale."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
   830
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   831
    ^ aFraction class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   832
        numerator:(aFraction numerator * denominator) 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   833
        denominator:(aFraction denominator * numerator)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   834
        scale:scale
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   835
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   836
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   837
     ((1/3) asFixedPoint:2) / ((1/3) asFixedPoint:2)  
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   838
     ((1/3) asFixedPoint:2) / ((1/3) asFixedPoint:4)  
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   839
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   840
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   841
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   842
quotientFromInteger:anInteger
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
   843
    "Return the quotient of the argument, anInteger and the receiver.
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7380
diff changeset
   844
     Sent when anInteger does not know how to divide by the receiver.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   845
     Redefined here to preserve the scale."
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   846
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   847
    ^ self class 
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   848
        numerator:(anInteger * denominator)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   849
        denominator:numerator
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   850
        scale:scale
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   851
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   852
    "Modified: 5.11.1996 / 10:32:35 / cg"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   853
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   854
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   855
sumFromFixedPoint:aFixedPoint
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   856
    |n d otherNumerator otherDenominator|
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
    otherNumerator := aFixedPoint numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   859
    otherDenominator := aFixedPoint denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   860
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   861
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   862
    denominator = otherDenominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   863
        n := numerator + otherNumerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   864
        d := otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   865
    ] ifFalse:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   866
        n := (numerator * otherDenominator) + (otherNumerator * denominator).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   867
        d := denominator * otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   868
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   869
    ^ self class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   870
            numerator:n 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   871
            denominator:d
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   872
            scale:(scale max:aFixedPoint scale)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   873
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   874
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   875
     ((1/3) asFixedPoint:2) + ((1/3) asFixedPoint:2)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   876
     ((1/3) asFixedPoint:2) + ((2/3) asFixedPoint:2)     
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   877
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   878
     ((1/3) asFixedPoint:2) + ((1/3) asFixedPoint:4)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   879
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   880
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   881
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   882
sumFromFraction:aFraction
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   883
    |n d otherNumerator otherDenominator|
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   884
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   885
    otherNumerator := aFraction numerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   886
    otherDenominator := aFraction denominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   887
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   888
    "save a multiplication if possible"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   889
    denominator = otherDenominator ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   890
        n := numerator + otherNumerator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   891
        d := otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   892
    ] ifFalse:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   893
        n := (numerator * otherDenominator) + (otherNumerator * denominator).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   894
        d := denominator * otherDenominator.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   895
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   896
    ^ self class 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   897
            numerator:n 
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   898
            denominator:d
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   899
            scale:scale
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
     (1/3) + ((1/3) asFixedPoint:2)        
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   903
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   904
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   905
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   906
sumFromInteger:anInteger
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   907
    "sent when an integer does not know how to add the receiver.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   908
     Redefined here to preserve the scale."
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   909
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   910
    ^ self class 
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   911
        numerator:(numerator + (anInteger * denominator))
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   912
        denominator:denominator
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   913
        scale:scale
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   914
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   915
    "Modified: 5.11.1996 / 10:32:43 / cg"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   916
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   917
5570
e6e14f50d721 category change
Claus Gittinger <cg@exept.de>
parents: 3731
diff changeset
   918
!FixedPoint methodsFor:'printing & storing'!
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   919
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   920
printOn: aStream 
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
   921
    "append to the argument, aStream, a printed representation of the receiver.
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   922
     For printout, only scale post-decimal digits are printed
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   923
     (and the printout is rounded to that many digits)"
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   924
2563
094145e2366f removed unsued var
Claus Gittinger <cg@exept.de>
parents: 2538
diff changeset
   925
    |e integerPart fractionPart negative num|
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   926
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   927
    numerator < 0 ifTrue:[
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   928
        negative := true.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   929
        num := numerator negated.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   930
    ] ifFalse:[
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   931
        negative := false.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   932
        num := numerator.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   933
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   934
    integerPart := (num // denominator).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   935
    e := 10 raisedTo:scale.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   936
    fractionPart := (num \\ denominator).
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   937
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   938
    "/ the most common case is a denominator fitting the scale
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   939
    "/ (fixedPoint numbers are created this way)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   940
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   941
    e == denominator ifFalse:[
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   942
        fractionPart := fractionPart * (e * 10) // denominator.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   943
        fractionPart := (fractionPart roundTo:10) // 10.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   944
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   945
        fractionPart >= e ifTrue:[
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   946
            integerPart := integerPart + 1.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   947
            fractionPart := 0.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   948
        ]
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   949
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   950
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   951
    "/
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   952
    "/ add a 1000..., so we can (mis-)use integer-printString ...
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   953
    "/ the highest-1 will be cutoff after padding.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   954
    "/
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   955
    fractionPart := e + fractionPart.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   956
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   957
    negative ifTrue:[
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   958
        aStream nextPut:$-
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   959
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   960
    integerPart printOn:aStream.
6897
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
   961
    scale > 0 ifTrue:[
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
   962
        aStream nextPut: $..
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
   963
        ((fractionPart printStringPaddedTo:scale with:$0) copyFrom:2) printOn:aStream
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
   964
    ].
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   965
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   966
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   967
     (FixedPoint fromString:'0.66666666')               
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   968
     (FixedPoint fromString:'0.66666666') withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   969
     (FixedPoint fromString:'0.99999999')               
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   970
     (FixedPoint fromString:'0.99999999') withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   971
     (FixedPoint fromString:'1.00000001')               
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   972
     (FixedPoint fromString:'1.00000001') withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   973
     (FixedPoint fromString:'1.005')                    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   974
     (FixedPoint fromString:'1.005') withScale:2        
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   975
     (FixedPoint fromString:'1.005') withScale:1        
6897
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
   976
     (FixedPoint fromString:'1.5')                    
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
   977
     (FixedPoint fromString:'1.5') withScale:2        
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
   978
     (FixedPoint fromString:'1.5') withScale:1        
7282c0dc54d3 fixed printing for 0-scale
Claus Gittinger <cg@exept.de>
parents: 6895
diff changeset
   979
     (FixedPoint fromString:'1.5') withScale:0        
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   980
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   981
     (FixedPoint fromString:'-0.66666666')              
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   982
     (FixedPoint fromString:'-0.66666666') withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   983
     (FixedPoint fromString:'-0.99999999')              
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   984
     (FixedPoint fromString:'-0.99999999') withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   985
     (FixedPoint fromString:'-1.00000001')              
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   986
     (FixedPoint fromString:'-1.00000001') withScale:2   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   987
     (FixedPoint fromString:'-1.005')                   
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   988
     (FixedPoint fromString:'-1.005') withScale:2       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   989
     (FixedPoint fromString:'-1.005') withScale:1       
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   990
     (FixedPoint fromString:'-1.05')                    
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   991
     (FixedPoint fromString:'-1.05') withScale:2      
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   992
     (FixedPoint fromString:'-1.05') withScale:1      
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   993
     (FixedPoint fromString:'-1.04')                  
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   994
     (FixedPoint fromString:'-1.04') withScale:2      
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   995
     (FixedPoint fromString:'-1.04') withScale:1      
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   996
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   997
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   998
    "
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   999
     |a b r|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1000
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1001
     a := (FixedPoint fromString:'0.66666666') withScale:2.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1002
     b := (FixedPoint fromString:'0.33333333').
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1003
     r := (a + b) withScale:4.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1004
     Transcript show:'printout with scale of 4 :'; showCR:r.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1005
     Transcript show:'more precise value       :'; showCR:(r withScale:8)
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1006
    "
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1007
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1008
    "Modified: 12.4.1997 / 11:20:51 / cg"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1009
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1010
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1011
!FixedPoint methodsFor:'private'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1012
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1013
reduced
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1014
    |gc|
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1015
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1016
    scale isNil ifTrue:[
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1017
        "/ to catch inherited Fraction reduce calls
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1018
        self error:'should not happen'.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1019
        scale := 3
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1020
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1021
6573
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1022
    (denominator < 0) ifTrue:[
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1023
        numerator := numerator negated.
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1024
        denominator := denominator negated
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1025
    ].
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1026
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1027
    denominator == 1 ifTrue:[^ numerator].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1028
    numerator == 1 ifTrue:[^ self].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1029
    numerator == 0 ifTrue:[^ 0].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1030
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1031
    gc := numerator gcd:denominator.
6573
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1032
    gc < 0 ifTrue:[
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1033
        gc := gc negated
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1034
    ].
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1035
    gc := gc gcd:(10 raisedToInteger:scale).
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1036
6573
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1037
    (gc ~~ 1) ifTrue:[
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1038
        numerator := numerator // gc.
6573
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1039
        denominator := denominator // gc.
4ea758ea3d4b reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents: 6570
diff changeset
  1040
        denominator == 1 ifTrue:[^ numerator].
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1041
    ].
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1042
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1043
    ^ self
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1044
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1045
3731
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1046
scale:newScale 
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1047
    "set the scale."
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1048
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1049
    scale := newScale.
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1050
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1051
    "Modified: / 12.4.1997 / 11:22:02 / cg"
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1052
    "Created: / 5.8.1998 / 13:28:49 / cg"
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1053
!
8c2ef8f76670 added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents: 3060
diff changeset
  1054
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1055
setNumerator:nInteger denominator:d scale:s 
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1056
    "initialize the instance variables.
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1057
     Assumes that the fraction as specified by numerator and denominator
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1058
     is already reduced."
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1059
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1060
    scale := s.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1061
    super
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1062
        setNumerator:nInteger 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1063
        denominator:d
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1064
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1065
    "Modified: 12.4.1997 / 11:21:47 / cg"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1066
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1067
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1068
setNumerator:nInteger scale:s 
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1069
    "initialize the instance variables.
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1070
     Assumes that the fraction as specified by numerator and denominator
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1071
     is already reduced."
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1072
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1073
    scale := s.
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1074
    super
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1075
        setNumerator:nInteger 
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1076
        denominator:(10 raisedTo:s)
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1077
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1078
    "Modified: 12.4.1997 / 11:21:55 / cg"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1079
!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1080
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1081
setScale:newScale 
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1082
    "initialize the scale instance variables."
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1083
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1084
    scale := newScale.
2538
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1085
6bbfd3558458 comments
Claus Gittinger <cg@exept.de>
parents: 2140
diff changeset
  1086
    "Modified: 12.4.1997 / 11:22:02 / cg"
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1087
! !
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1088
6650
35de1d8400b2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6645
diff changeset
  1089
!FixedPoint methodsFor:'testing'!
1891
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1090
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1091
isFixedPoint
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1092
    "return true, if the receiver is some kind of fixedPoint number;
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1093
     true is returned here - the method is redefined from Object."
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1094
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1095
    ^ true
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1096
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1097
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1098
! !
58073a4e3859 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1099
7376
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1100
!FixedPoint methodsFor:'truncation & rounding'!
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1101
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1102
roundedToScale
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1103
    "return the receiver rounded to my scale 
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1104
     (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
  1105
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1106
    |n scale10|
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1107
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1108
    scale10 := 10 raisedToInteger:scale.
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1109
    n := ((numerator * scale10) + (denominator bitShift:-1)) // denominator.
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1110
    ^ self class
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1111
        numerator:n
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1112
        denominator:scale10
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1113
        scale:scale.
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1114
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1115
    "
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1116
     ((2/3) asFixedPoint:2)                     
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1117
     ((2/3) asFixedPoint:2) rounded          
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1118
     ((2/3) asFixedPoint:2) roundedToScale     
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1119
    "
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1120
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1121
    "Modified: 5.11.1996 / 12:18:46 / cg"
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1122
!
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1123
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1124
truncatedToScale
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1125
    "return the receiver truncated towards zero to my scale"
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1126
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1127
    |n scale10|
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1128
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1129
    scale10 := 10 raisedToInteger:scale.
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1130
    n := (numerator * scale10) quo:denominator.
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1131
    ^ self class
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1132
        numerator:n
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1133
        denominator:scale10
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1134
        scale:scale.
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1135
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1136
    "
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1137
     ((2/3) asFixedPoint:2)                     
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1138
     ((2/3) asFixedPoint:2) truncated          
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1139
     ((2/3) asFixedPoint:2) truncatedToScale     
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1140
    "
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1141
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1142
    "Modified: 5.11.1996 / 12:18:46 / cg"
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1143
! !
a166651de99e truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
  1144
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1145
!FixedPoint class methodsFor:'documentation'!
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1146
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1147
version
9129
f1451239cd4b comment
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
  1148
    ^ '$Header: /cvs/stx/stx/libbasic/FixedPoint.st,v 1.31 2006-02-17 12:35:04 cg Exp $'
1886
7c58ef7c2d78 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1149
! !