LimitedPrecisionReal.st
author Claus Gittinger <cg@exept.de>
Tue, 28 Oct 1997 20:12:59 +0100
changeset 3060 0faf242e1142
parent 2793 e40dedf51177
child 3137 f334b17f0347
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     1
"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1994 by Claus Gittinger
213
3b56a17534fd *** empty log message ***
claus
parents: 112
diff changeset
     3
	      All Rights Reserved
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     4
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    11
"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    12
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
    13
'From Smalltalk/X, Version:3.2.1 on 25-oct-1997 at 9:25:42 pm'                  !
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
    14
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    15
Number subclass:#LimitedPrecisionReal
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    16
	instanceVariableNames:''
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    17
	classVariableNames:''
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    18
	poolDictionaries:''
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    19
	category:'Magnitude-Numbers'
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    20
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    21
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    22
!LimitedPrecisionReal class methodsFor:'documentation'!
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    23
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    24
copyright
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    25
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    26
 COPYRIGHT (c) 1994 by Claus Gittinger
213
3b56a17534fd *** empty log message ***
claus
parents: 112
diff changeset
    27
	      All Rights Reserved
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    28
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    29
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    30
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    31
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    32
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    33
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    34
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    35
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    36
!
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    37
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    38
documentation
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    39
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    40
    Abstract superclass for single and double (and maybe more) 
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    41
    precision real numbers (i.e. Float and Double).
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    42
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    43
    Due to historic reasons, ST/X's Floats are what Doubles are in ST-80.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    44
    This may change soon (implementing LPReal is a first step towards this).
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1201
diff changeset
    45
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1201
diff changeset
    46
    [author:]
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1201
diff changeset
    47
        Claus Gittinger
1893
c66af5c46272 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1882
diff changeset
    48
c66af5c46272 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1882
diff changeset
    49
    [see also:]
c66af5c46272 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1882
diff changeset
    50
        Fraction FixedPoint
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    51
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    52
! !
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    53
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    54
!LimitedPrecisionReal class methodsFor:'instance creation'!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    56
new:aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    57
    "catch this message - not allowed for floats/doubles"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    58
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    59
    self error:'Floats/Doubles cannot be created with new:'
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    60
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    61
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    62
!LimitedPrecisionReal methodsFor:'accessing'!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    63
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    64
at:index
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    65
    "redefined to prevent access to individual bytes in a real."
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    66
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    67
    self error:'not allowed for floats/doubles'
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    68
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    69
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    70
at:index put:aValue
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    71
    "redefined to prevent access to individual bytes in a real"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    72
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    73
    self error:'not allowed for floats/doubles'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    74
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    75
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    76
size
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    77
   "redefined since reals are kludgy (ByteArry)"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    78
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    79
   ^ 0
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    80
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    81
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    82
!LimitedPrecisionReal methodsFor:'arithmetic'!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    83
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    84
* aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    85
    "return the product of the receiver and the argument, aNumber"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    86
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    87
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    88
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    89
"/    ^ aNumber productFromDouble:self asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    90
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    91
    ^ aNumber productFromFloat:self asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    92
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    93
    "Modified: 17.4.1996 / 12:35:36 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    94
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    95
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    96
+ aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    97
    "return the sum of the receiver and the argument, aNumber"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
    98
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    99
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   100
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   101
"/    ^ aNumber sumFromDouble:self asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   102
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   103
    ^ aNumber sumFromFloat:self asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   104
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   105
    "Modified: 17.4.1996 / 12:35:55 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   106
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   107
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   108
- aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   109
    "return the difference of the receiver and the argument, aNumber"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   110
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   111
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   112
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   113
"/    ^ aNumber differenceFromDouble:self asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   114
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   115
    ^ aNumber differenceFromFloat:self asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   116
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   117
    "Modified: 17.4.1996 / 12:36:07 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   118
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   119
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   120
/ aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   121
    "return the quotient of the receiver and the argument, aNumber"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   122
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   123
    ((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   124
        ^ DivisionByZeroSignal raise.
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   125
    ].
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   126
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   127
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   128
"/    ^ aNumber quotientFromDouble:self asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   129
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   130
    ^ aNumber quotientFromFloat:self asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   131
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   132
    "Modified: 17.4.1996 / 12:36:21 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   133
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   134
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   135
// aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   136
    "return the integer quotient of dividing the receiver by aNumber with
1882
f5f8a2f201e5 fixed #//
Claus Gittinger <cg@exept.de>
parents: 1865
diff changeset
   137
     truncation towards negative infinity."
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   138
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   139
    ^ (self / aNumber) floor asInteger
1882
f5f8a2f201e5 fixed #//
Claus Gittinger <cg@exept.de>
parents: 1865
diff changeset
   140
f5f8a2f201e5 fixed #//
Claus Gittinger <cg@exept.de>
parents: 1865
diff changeset
   141
    "Modified: 5.11.1996 / 11:45:37 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   142
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   143
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   144
\\ aNumber
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   145
    "return the integer remainder of dividing the receiver by aNumber with
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   146
    truncation towards negative infinity."
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   147
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   148
    ^ (self - ((self / aNumber) floor * aNumber)) floor asInteger
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   149
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   150
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   151
!LimitedPrecisionReal methodsFor:'coercion and converting'!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   152
2141
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   153
asFixedPoint
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   154
    "return the receiver as fixedPoint number.
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   155
     Q: what should the scale be here ?"
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   156
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   157
    ^ self asFraction asFixedPoint
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   158
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   159
    "
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   160
     0.3 asFixedPoint
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   161
     0.5 asFixedPoint
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   162
     (1/5) asFloat asFixedPoint 
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   163
     (1/3) asFloat asFixedPoint 
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   164
     (2/3) asFloat asFixedPoint 
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   165
     (1/8) asFloat asFixedPoint
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   166
     3.14159 asFixedPoint
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   167
     0.0000001 asFraction
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   168
     0.0000001 asFixedPoint
2141
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   169
    "
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   170
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   171
    "Modified: / 25.10.1997 / 15:36:54 / cg"
2141
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   172
!
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   173
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   174
asFixedPoint:scale
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   175
    "return the receiver as fixedPoint number with the given
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   176
     number of post-decimal-digits."
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   177
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   178
    ^ self asFraction asFixedPoint:scale
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   179
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   180
    "
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   181
     0.3 asFixedPoint:3     
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   182
     0.5 asFixedPoint:3     
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   183
     (1/5) asFloat asFixedPoint:1  
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   184
     (1/8) asFloat asFixedPoint:1  
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   185
     3.14159 asFixedPoint:2       
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   186
     3.14159 asFixedPoint:3       
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   187
     (3.14159 asFixedPoint:2) asFixedPoint:5  
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   188
    "
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   189
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   190
    "Modified: 10.1.1997 / 19:59:42 / cg"
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   191
!
bba0027204fd added #asFixedPoint:
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
   192
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   193
asFraction
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   194
    "return a corresponding fraction or integer
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   195
     (fractions with 1 as denominator are not allowed/useful)
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   196
     - notice, that 'aFract asFloat asFraction' does not always return
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   197
     a good fraction ... due to rounding errors when converting to float"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   198
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   199
    |fract digits power num denom eI|
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   200
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   201
    "we (indirectly) use printf which knows the precision of floats"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   202
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   203
    fract := self fractionPart.
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   204
    fract = 0.0 ifTrue:[
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   205
        ^ self asInteger.
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   206
    ].
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   207
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   208
    "/ use printf, so we dont need to know anything
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   209
    "/ about float representation here.
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   210
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   211
    digits := (fract printfPrintString:'%.16f').
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   212
    eI := digits size.
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   213
    [(digits at:eI) == $0] whileTrue:[
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   214
        eI := eI - 1
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   215
    ].
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   216
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   217
    power := eI - 3 + 1.
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   218
    num := (self - fract) asInteger.
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   219
    denom := (10 raisedToInteger:power).
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   220
    num := num * denom.
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   221
    num := num + (fract * denom) asInteger.
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   222
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2388
diff changeset
   223
    ^ Fraction numerator:num denominator:denom
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   224
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   225
    "
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   226
     0.3 asFraction 
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   227
     0.5 asFraction 
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   228
     (1/5) asFloat asFraction
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   229
     (1/8) asFloat asFraction
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   230
     (1/13) asFloat asFraction
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   231
     3.14159 asFraction 
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   232
     1.3 asFraction
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   233
     1.0 asFraction
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   234
    "
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2388
diff changeset
   235
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   236
    "Modified: / 25.10.1997 / 16:41:19 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   237
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   238
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   239
asInteger
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   240
    "return an integer with same value - might truncate"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   241
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   242
    |l v sign|
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   243
2388
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   244
    self isNaN ifTrue:[
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   245
        ^ DomainErrorSignal raise
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   246
    ].
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   247
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   248
    "this is stupid code - rounding errors accumulate; fix later"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   249
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   250
    sign := self sign.
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   251
    v := self abs.
85
claus
parents: 55
diff changeset
   252
    (v >= 10.0) ifTrue:[
2388
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   253
        l := (v / 10.0) asInteger * 10
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   254
    ] ifFalse:[
2388
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   255
        l := 0
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   256
    ].
85
claus
parents: 55
diff changeset
   257
    v := v - ((v / 10.0) floor * 10) floor.
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   258
    l := l + v truncated.
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   259
    ^ l * sign
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   260
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   261
    "12345.0 asInteger"
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   262
    "1e15 asInteger"
2388
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   263
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   264
    "Modified: 12.2.1997 / 16:45:07 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   265
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   266
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   267
coerce:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   268
    "return aNumber converted into receivers type"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   269
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   270
"/ as soon as Float are float & Double are doubles,
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   271
"/ use:
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   272
"/    ^ aNumber asDouble
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   273
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   274
    ^ aNumber asFloat
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   275
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   276
    "Modified: 17.4.1996 / 12:36:46 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   277
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   278
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   279
generality
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   280
    "return the generality value - see ArithmeticValue>>retry:coercing:"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   281
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   282
    ^ 80
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   283
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   284
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   285
!LimitedPrecisionReal methodsFor:'comparing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   286
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   287
< aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   288
    "return true, if the argument is greater"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   289
1201
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   290
"/ as soon as Float are float & Double are doubles,
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   291
"/ use:
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   292
"/    ^ aNumber lessFromDouble:self asDouble
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   293
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   294
    ^ aNumber lessFromFloat:self asFloat
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   295
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   296
    "Modified: 17.4.1996 / 13:34:50 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   297
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   298
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   299
<= aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   300
    "return true, if the argument is greater or equal"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   301
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   302
    ^ self retry:#<= coercing:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   303
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   304
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   305
= aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   306
    "return true, if the arguments value are equal"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   307
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   308
    ^ self retry:#= coercing:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   309
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   310
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   311
> aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   312
    "return true, if the argument is less"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   313
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   314
    ^ self retry:#> coercing:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   315
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   316
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   317
>= aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   318
    "return true, if the argument is less or equal"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   319
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   320
    ^ self retry:#>= coercing:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   321
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   322
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   323
~= aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   324
    "return true, if the arguments value are not equal"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   325
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   326
    ^ self retry:#~= coercing:aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   327
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   328
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   329
!LimitedPrecisionReal methodsFor:'copying'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   330
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   331
deepCopy
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   332
    "return a deep copy of myself
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   333
     - because storing into floats is not recommended/allowed, its ok to return the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   334
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   335
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   336
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   337
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   338
deepCopyUsing:aDictionary
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   339
    "return a deep copy of myself
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   340
     - because storing into floats is not recommended/allowed, its ok to return the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   341
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   342
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   343
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   344
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   345
shallowCopy
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   346
    "return a shallow copy of the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   347
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   348
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   349
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   350
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   351
simpleDeepCopy
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   352
    "return a deep copy of the receiver
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   353
     - because storing into floats is not recommended/allowed, its ok to return the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   354
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   355
    ^ self
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   356
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   357
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   358
!LimitedPrecisionReal methodsFor:'double dispatching'!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   359
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   360
differenceFromFraction:aFraction
213
3b56a17534fd *** empty log message ***
claus
parents: 112
diff changeset
   361
    "sent when a fraction does not know how to subtract the receiver, a float"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   362
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   363
    |d|
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   364
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   365
    d := aFraction denominator.
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   366
    ^ (aFraction numerator - (self * d)) / d
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   367
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   368
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   369
productFromFraction:aFraction
213
3b56a17534fd *** empty log message ***
claus
parents: 112
diff changeset
   370
    "sent when a fraction does not know how to multiply the receiver, a float"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   371
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   372
    ^ self * aFraction numerator / aFraction denominator
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   373
!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   374
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   375
quotientFromFraction:aFraction
213
3b56a17534fd *** empty log message ***
claus
parents: 112
diff changeset
   376
    "sent when a fraction does not know how to divide by the receiver, a float"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   377
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   378
    ^ aFraction numerator / (self * aFraction denominator)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   379
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   380
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   381
sumFromFraction:aFraction
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   382
    "sent when a fraction does not know how to add the receiver, a float"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   383
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   384
    |d|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   385
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   386
    d := aFraction denominator.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   387
    ^ (self * d + aFraction numerator) / d
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   388
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   389
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   390
!LimitedPrecisionReal methodsFor:'printing & storing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   391
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   392
printOn:aStream
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   393
    "append a printed representation of the receiver to
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   394
     the argument, aStream"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   395
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   396
    aStream nextPutAll:self printString
1200
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   397
!
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   398
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   399
printString
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   400
    "return a printed representation of the receiver"
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   401
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   402
    ^ self subclassResponsibility
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   403
cc16f7a00b52 limited shortFloat support
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   404
    "Created: 17.4.1996 / 12:12:20 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   405
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   406
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   407
!LimitedPrecisionReal methodsFor:'testing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   408
2388
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   409
isNaN
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   410
   ^ self subclassResponsibility
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   411
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   412
    "Modified: 12.2.1997 / 16:45:27 / cg"
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   413
!
df5e0eb88999 added isNaN-check
Claus Gittinger <cg@exept.de>
parents: 2141
diff changeset
   414
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   415
isReal
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   416
    "return true, if the receiver is some kind of real number;
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   417
     false is returned here - the method is redefined from Object."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   418
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   419
    ^ true
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   420
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   421
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   422
negative
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   423
    "return true if the receiver is less than zero"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   424
1201
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   425
"/ as soon as Float are float & Double are doubles,
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   426
"/ use:
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   427
"/    ^ self asDouble negative
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   428
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   429
    ^ self asFloat negative
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   430
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   431
    "Modified: 17.4.1996 / 13:35:00 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   432
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   433
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   434
positive
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   435
    "return true if the receiver is greater or equal to zero"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   436
1201
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   437
"/ as soon as Float are float & Double are doubles,
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   438
"/ use:
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   439
"/    ^ self asDouble positive
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   440
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   441
    ^ self asFloat positive
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   442
24a5faa7e305 oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents: 1200
diff changeset
   443
    "Modified: 17.4.1996 / 13:35:10 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   444
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   445
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   446
!LimitedPrecisionReal methodsFor:'truncation and rounding'!
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   447
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   448
fractionPart
1865
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   449
    "return a float with value from digits after the decimal point.
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   450
     (i.e. the receiver minus its truncated value)"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   451
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   452
    ^ self - self truncated asFloat
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   453
1865
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   454
    "
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   455
     1234.56789 fractionPart
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   456
     1.2345e6 fractionPart  
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   457
    "
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   458
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   459
    "Modified: 4.11.1996 / 20:26:54 / cg"
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   460
!
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   461
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   462
integerPart
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   463
    "return a float with value from digits before the decimal point
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   464
     (i.e. the truncated value)"
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   465
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   466
    ^ self truncated asFloat
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   467
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   468
    "
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   469
     1234.56789 integerPart 
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   470
     1.2345e6 integerPart   
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   471
    "
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   472
5168897c326f added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   473
    "Modified: 4.11.1996 / 20:26:21 / cg"
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   474
! !
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   475
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   476
!LimitedPrecisionReal class methodsFor:'documentation'!
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   477
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   478
version
3060
0faf242e1142 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
   479
    ^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.21 1997-10-28 19:12:03 cg Exp $'
55
4a82f332c3f8 Initial revision
claus
parents:
diff changeset
   480
! !