author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 29 Sep 2011 16:44:37 +0100 | |
branch | jv |
changeset 17869 | 9610c6c94e71 |
parent 17865 | 598963c6ff8e |
child 17892 | d86c8bd5ece3 |
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. |
11607 | 52 |
I am not sure if this is more flexibility than needed 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 |
|
11726 | 168 |
"return the product of the receiver and the argument. |
169 |
Care for the error to propagate into the result." |
|
170 |
||
10825 | 171 |
^ MeasurementValue new |
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
172 |
value:(value * aNumber value) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
173 |
minValue:(minValue * aNumber minValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
174 |
maxValue:(maxValue * aNumber maxValue) |
10825 | 175 |
! |
176 |
||
177 |
+ aNumber |
|
11726 | 178 |
"return the sum of the receiver and the argument. |
179 |
Care for the error to propagate into the result." |
|
180 |
||
10825 | 181 |
^ MeasurementValue new |
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
182 |
value:(value + aNumber value) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
183 |
minValue:(minValue + aNumber minValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
184 |
maxValue:(maxValue + aNumber maxValue) |
10825 | 185 |
! |
186 |
||
187 |
- aNumber |
|
11726 | 188 |
"return the difference of the receiver and the argument. |
189 |
Care for the error to propagate into the result." |
|
190 |
||
10825 | 191 |
^ MeasurementValue new |
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
192 |
value:(value - aNumber value) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
193 |
minValue:(minValue - aNumber maxValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
194 |
maxValue:(maxValue - aNumber minValue) |
10825 | 195 |
! |
196 |
||
197 |
/ aNumber |
|
11726 | 198 |
"return the quotient of the receiver and the argument. |
199 |
Care for the error to propagate into the result." |
|
200 |
||
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
201 |
^ MeasurementValue new |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
202 |
value:(value / aNumber value) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
203 |
minValue:(minValue / aNumber maxValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
204 |
maxValue:(maxValue / aNumber minValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
205 |
! ! |
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 |
!MeasurementValue methodsFor:'coercing & converting'! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
208 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
209 |
+/- error |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
210 |
"return a MeasurementValue with the given error." |
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 |
"/ what should I do here - take the new error, |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
213 |
"/ or multiply them ???? |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
214 |
self error. |
10825 | 215 |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
216 |
minValue := value - error. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
217 |
maxValue := value + error. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
218 |
! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
219 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
220 |
coerce:aNumber |
11717 | 221 |
"convert the argument aNumber into an instance of the receivers class and return it." |
222 |
||
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
223 |
^ MeasurementValue value:aNumber minValue:aNumber maxValue:aNumber |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
224 |
! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
225 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
226 |
generality |
11717 | 227 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
228 |
||
229 |
"/ adding 1 to the values generality has the subtle side effect of enforcing |
|
230 |
"/ a call to coerce: for mixed type operations (i.e. Int * MeasurementValue). |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
231 |
"/ try it. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
232 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
233 |
^ value generality + 1 |
10825 | 234 |
! ! |
235 |
||
236 |
!MeasurementValue methodsFor:'comparing'! |
|
237 |
||
238 |
< aNumber |
|
11726 | 239 |
"return true, if the argument is greater than the receiver. |
240 |
Care for the error - i.e. compare against my maximum-value" |
|
241 |
||
10825 | 242 |
^ maxValue < aNumber |
243 |
! |
|
244 |
||
11465 | 245 |
= aNumber |
11726 | 246 |
"hard to tell, what we want here... |
247 |
How about: aNumber between:minValue and:maxValue ???" |
|
11465 | 248 |
|
249 |
^ value = aNumber value |
|
250 |
and:[ minValue = aNumber minValue |
|
251 |
and:[ maxValue = aNumber maxValue ]] |
|
252 |
! |
|
253 |
||
10825 | 254 |
lessFromFloat:aFloat |
255 |
"aFloat < self ?" |
|
256 |
||
257 |
^ aFloat < minValue |
|
258 |
! |
|
259 |
||
260 |
lessFromInteger:anInteger |
|
261 |
"anInteger < self ?" |
|
262 |
||
263 |
^ anInteger < minValue |
|
264 |
! ! |
|
265 |
||
266 |
!MeasurementValue methodsFor:'printing & storing'! |
|
267 |
||
268 |
printOn:aStream |
|
11459 | 269 |
aStream nextPutAll:'('. |
270 |
(maxValue-value) = (value-minValue) ifTrue:[ |
|
271 |
value storeOn:aStream. |
|
272 |
aStream nextPutAll:' +/- '. |
|
273 |
(maxValue-value) storeOn:aStream |
|
274 |
] ifFalse:[ |
|
275 |
aStream nextPutAll:'MeasurementValue value:'. |
|
276 |
value storeOn:aStream. |
|
277 |
aStream nextPutAll:' minValue:'. |
|
278 |
minValue storeOn:aStream. |
|
279 |
aStream nextPutAll:' maxValue:'. |
|
280 |
maxValue storeOn:aStream. |
|
281 |
]. |
|
10825 | 282 |
')' printOn:aStream. |
11459 | 283 |
|
284 |
" |
|
285 |
(5 +/- 1) storeString |
|
286 |
(MeasurementValue value:5 minValue:3 maxValue:8) storeString |
|
287 |
" |
|
10825 | 288 |
! ! |
289 |
||
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
290 |
!MeasurementValue methodsFor:'private accessing'! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
291 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
292 |
value:valueArg error:errorFraction |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
293 |
self |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
294 |
value:valueArg |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
295 |
minValue:(valueArg * (1-errorFraction)) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
296 |
maxValue:(valueArg * (1+errorFraction)). |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
297 |
! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
298 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
299 |
value:valueArg minValue:minValueArg maxValue:maxValueArg |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
300 |
value := valueArg. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
301 |
minValue := minValueArg. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
302 |
maxValue := maxValueArg. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
303 |
! ! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
304 |
|
10825 | 305 |
!MeasurementValue methodsFor:'testing'! |
306 |
||
307 |
between:min and:max |
|
308 |
minValue < min ifTrue:[^ false]. |
|
309 |
maxValue > max ifTrue:[^ false]. |
|
310 |
^ true |
|
311 |
! ! |
|
312 |
||
313 |
!MeasurementValue class methodsFor:'documentation'! |
|
314 |
||
315 |
version |
|
17869 | 316 |
^ '$Id: MeasurementValue.st 10700 2011-09-29 15:44:37Z vranyj1 $' |
17865 | 317 |
! |
318 |
||
319 |
version_SVN |
|
17869 | 320 |
^ '$Id: MeasurementValue.st 10700 2011-09-29 15:44:37Z vranyj1 $' |
17846 | 321 |
! ! |