author | Claus Gittinger <cg@exept.de> |
Sat, 28 Feb 2009 12:51:14 +0100 | |
changeset 11606 | f657ce220093 |
parent 11466 | 3b67c6756dad |
child 11607 | 39bd3dc180df |
permissions | -rw-r--r-- |
10825 | 1 |
" |
2 |
COPYRIGHT (c) 2007 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
"{ Package: 'stx:libbasic' }" |
|
13 |
||
14 |
Number subclass:#MeasurementValue |
|
15 |
instanceVariableNames:'value minValue maxValue' |
|
11465 | 16 |
classVariableNames:'MeasurementValueZero' |
10825 | 17 |
poolDictionaries:'' |
18 |
category:'Magnitude-Numbers' |
|
19 |
! |
|
20 |
||
21 |
!MeasurementValue class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 2007 by eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
documentation |
|
38 |
" |
|
39 |
A MeasurementValue is a numeric value with an error, such as returned |
|
40 |
by measurement devices (Volt-Meter). For example, if a measurement-device has |
|
41 |
an error of 10%, a measured value of 20 could be any value between 18 and 22. |
|
42 |
||
43 |
Arithmetic operations keep track of the error; if any operand is a MeasurementValue, |
|
44 |
the operation returns a MeasurementValue as result. |
|
45 |
||
46 |
This class is possibly unfinished and needs more arithmetic methods. |
|
47 |
For now, the stuff found here represents our needs and more might be added in the future. |
|
48 |
||
11456 | 49 |
Also notice, that instances do not keep the error as a fraction, but instead a min. and maxValue. |
11606 | 50 |
That means, that we can handle the case where the error is different in |
11456 | 51 |
the positive and negative directions. |
11606 | 52 |
I am not sure if this is more flexibility than need in the long run. |
11456 | 53 |
|
10825 | 54 |
[author:] |
55 |
Claus Gittinger |
|
56 |
||
57 |
[see also:] |
|
58 |
Number |
|
59 |
Float ShortFloat Fraction FixedPoint Integer Complex |
|
60 |
FloatArray DoubleArray |
|
61 |
" |
|
62 |
! |
|
63 |
||
64 |
examples |
|
65 |
" |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
66 |
Notice, how the errors accumulate... |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
67 |
[exBegin] |
10825 | 68 |
|voltage current power| |
69 |
||
70 |
voltage := MeasurementValue value:10 error:0.05. |
|
71 |
current := MeasurementValue value:2 error:0.1. |
|
72 |
power := voltage * current. |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
73 |
power. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
74 |
power minValue. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
75 |
power maxValue. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
76 |
[exEnd] |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
77 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
78 |
[exBegin] |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
79 |
|voltage current power| |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
80 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
81 |
voltage := MeasurementValue value:10 error:0.05. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
82 |
current := 2. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
83 |
power := voltage * current. |
10825 | 84 |
power |
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
85 |
[exEnd] |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
86 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
87 |
[exBegin] |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
88 |
|voltage doubleVoltage| |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
89 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
90 |
voltage := MeasurementValue value:10 error:0.1. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
91 |
doubleVoltage := 2 * voltage. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
92 |
doubleVoltage |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
93 |
[exEnd] |
10825 | 94 |
" |
95 |
! ! |
|
96 |
||
97 |
!MeasurementValue class methodsFor:'instance creation'! |
|
98 |
||
99 |
value:arg1 error:arg2 |
|
100 |
"return a new measurementValue with a given value and an error (fraction)" |
|
101 |
||
102 |
^ self new value:arg1 error:arg2 |
|
103 |
||
104 |
" |
|
105 |
MeasurementValue value:10 error:0.2 |
|
106 |
" |
|
107 |
! |
|
108 |
||
109 |
value:arg1 minValue:arg2 maxValue:arg3 |
|
110 |
"return a new measurementValue with a given value and an error given as min-max values. |
|
111 |
Use this, if the error is not the same in both directions" |
|
112 |
||
113 |
^ self new value:arg1 minValue:arg2 maxValue:arg3 |
|
114 |
||
115 |
"a power of 10 error: |
|
116 |
MeasurementValue value:5 minValue:1 maxValue:10 |
|
117 |
" |
|
118 |
! ! |
|
119 |
||
11465 | 120 |
!MeasurementValue class methodsFor:'constants'! |
121 |
||
11466 | 122 |
unity |
123 |
"return the neutral element for multiplication" |
|
124 |
||
125 |
^ 1 |
|
126 |
||
127 |
" |
|
128 |
self unity |
|
129 |
" |
|
130 |
! |
|
131 |
||
11465 | 132 |
zero |
133 |
"return the neutral element for addition (0 +/- 0)" |
|
134 |
||
135 |
MeasurementValueZero isNil ifTrue:[ |
|
136 |
MeasurementValueZero := self value:0 minValue:0 maxValue:0 |
|
137 |
]. |
|
138 |
^ MeasurementValueZero |
|
139 |
||
140 |
" |
|
141 |
self zero |
|
142 |
" |
|
143 |
! ! |
|
144 |
||
10825 | 145 |
!MeasurementValue methodsFor:'accessing'! |
146 |
||
147 |
maxValue |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
148 |
"the maximum possible value, considerung what has been measured and what the measurement error is" |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
149 |
|
10825 | 150 |
^ maxValue |
151 |
! |
|
152 |
||
153 |
minValue |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
154 |
"the minimum possible value, considerung what has been measured and what the measurement error is" |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
155 |
|
10825 | 156 |
^ minValue |
157 |
! |
|
158 |
||
159 |
value |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
160 |
"the measured value" |
10825 | 161 |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
162 |
^ value |
10825 | 163 |
! ! |
164 |
||
165 |
!MeasurementValue methodsFor:'arithmetic'! |
|
166 |
||
167 |
* aNumber |
|
168 |
^ MeasurementValue new |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
169 |
value:(value * aNumber value) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
170 |
minValue:(minValue * aNumber minValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
171 |
maxValue:(maxValue * aNumber maxValue) |
10825 | 172 |
! |
173 |
||
174 |
+ aNumber |
|
175 |
^ MeasurementValue new |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
176 |
value:(value + aNumber value) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
177 |
minValue:(minValue + aNumber minValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
178 |
maxValue:(maxValue + aNumber maxValue) |
10825 | 179 |
! |
180 |
||
181 |
- aNumber |
|
182 |
^ MeasurementValue new |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
183 |
value:(value - aNumber value) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
184 |
minValue:(minValue - aNumber maxValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
185 |
maxValue:(maxValue - aNumber minValue) |
10825 | 186 |
! |
187 |
||
188 |
/ aNumber |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
189 |
^ MeasurementValue new |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
190 |
value:(value / aNumber value) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
191 |
minValue:(minValue / aNumber maxValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
192 |
maxValue:(maxValue / aNumber minValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
193 |
! ! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
194 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
195 |
!MeasurementValue methodsFor:'coercing & converting'! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
196 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
197 |
+/- error |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
198 |
"return a MeasurementValue with the given error." |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
199 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
200 |
"/ what should I do here - take the new error, |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
201 |
"/ or multiply them ???? |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
202 |
self error. |
10825 | 203 |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
204 |
minValue := value - error. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
205 |
maxValue := value + error. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
206 |
! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
207 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
208 |
coerce:aNumber |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
209 |
^ MeasurementValue value:aNumber minValue:aNumber maxValue:aNumber |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
210 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
211 |
! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
212 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
213 |
generality |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
214 |
"/ adding 1 has the subtle side effect of enforcing a call to cuerce: |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
215 |
"/ for mixed type operaions (i.e. Int * MeasurementValue). |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
216 |
"/ try it. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
217 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
218 |
^ value generality + 1 |
10825 | 219 |
! ! |
220 |
||
221 |
!MeasurementValue methodsFor:'comparing'! |
|
222 |
||
223 |
< aNumber |
|
224 |
^ maxValue < aNumber |
|
225 |
! |
|
226 |
||
11465 | 227 |
= aNumber |
228 |
"hard to tell, what we want here..." |
|
229 |
||
230 |
^ value = aNumber value |
|
231 |
and:[ minValue = aNumber minValue |
|
232 |
and:[ maxValue = aNumber maxValue ]] |
|
233 |
! |
|
234 |
||
10825 | 235 |
lessFromFloat:aFloat |
236 |
"aFloat < self ?" |
|
237 |
||
238 |
^ aFloat < minValue |
|
239 |
! |
|
240 |
||
241 |
lessFromInteger:anInteger |
|
242 |
"anInteger < self ?" |
|
243 |
||
244 |
^ anInteger < minValue |
|
245 |
! ! |
|
246 |
||
247 |
!MeasurementValue methodsFor:'printing & storing'! |
|
248 |
||
249 |
printOn:aStream |
|
11459 | 250 |
aStream nextPutAll:'('. |
251 |
(maxValue-value) = (value-minValue) ifTrue:[ |
|
252 |
value storeOn:aStream. |
|
253 |
aStream nextPutAll:' +/- '. |
|
254 |
(maxValue-value) storeOn:aStream |
|
255 |
] ifFalse:[ |
|
256 |
aStream nextPutAll:'MeasurementValue value:'. |
|
257 |
value storeOn:aStream. |
|
258 |
aStream nextPutAll:' minValue:'. |
|
259 |
minValue storeOn:aStream. |
|
260 |
aStream nextPutAll:' maxValue:'. |
|
261 |
maxValue storeOn:aStream. |
|
262 |
]. |
|
10825 | 263 |
')' printOn:aStream. |
11459 | 264 |
|
265 |
" |
|
266 |
(5 +/- 1) storeString |
|
267 |
(MeasurementValue value:5 minValue:3 maxValue:8) storeString |
|
268 |
" |
|
10825 | 269 |
! ! |
270 |
||
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
271 |
!MeasurementValue methodsFor:'private accessing'! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
272 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
273 |
value:valueArg error:errorFraction |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
274 |
self |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
275 |
value:valueArg |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
276 |
minValue:(valueArg * (1-errorFraction)) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
277 |
maxValue:(valueArg * (1+errorFraction)). |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
278 |
! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
279 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
280 |
value:valueArg minValue:minValueArg maxValue:maxValueArg |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
281 |
value := valueArg. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
282 |
minValue := minValueArg. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
283 |
maxValue := maxValueArg. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
284 |
! ! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
285 |
|
10825 | 286 |
!MeasurementValue methodsFor:'testing'! |
287 |
||
288 |
between:min and:max |
|
289 |
minValue < min ifTrue:[^ false]. |
|
290 |
maxValue > max ifTrue:[^ false]. |
|
291 |
^ true |
|
292 |
! ! |
|
293 |
||
294 |
!MeasurementValue class methodsFor:'documentation'! |
|
295 |
||
296 |
version |
|
11606 | 297 |
^ '$Header: /cvs/stx/stx/libbasic/MeasurementValue.st,v 1.7 2009-02-28 11:51:14 cg Exp $' |
10825 | 298 |
! ! |