author | Claus Gittinger <cg@exept.de> |
Wed, 14 Jan 2009 23:05:47 +0100 | |
changeset 11457 | 5ff638783ee0 |
parent 11456 | 3ab3ea28c8f4 |
child 11459 | 01ad9aa9217d |
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' |
|
16 |
classVariableNames:'' |
|
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. |
50 |
That means, that we can represent tha case where the absolute error values are different in |
|
51 |
the positive and negative directions. |
|
52 |
I am not sure if this is more flexibility than needed in the long run. |
|
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 |
||
120 |
!MeasurementValue methodsFor:'accessing'! |
|
121 |
||
122 |
maxValue |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
123 |
"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
|
124 |
|
10825 | 125 |
^ maxValue |
126 |
! |
|
127 |
||
128 |
minValue |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
129 |
"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
|
130 |
|
10825 | 131 |
^ minValue |
132 |
! |
|
133 |
||
134 |
value |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
135 |
"the measured value" |
10825 | 136 |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
137 |
^ value |
10825 | 138 |
! ! |
139 |
||
140 |
!MeasurementValue methodsFor:'arithmetic'! |
|
141 |
||
142 |
* aNumber |
|
143 |
^ MeasurementValue new |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
144 |
value:(value * aNumber value) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
145 |
minValue:(minValue * aNumber minValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
146 |
maxValue:(maxValue * aNumber maxValue) |
10825 | 147 |
! |
148 |
||
149 |
+ aNumber |
|
150 |
^ MeasurementValue new |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
151 |
value:(value + aNumber value) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
152 |
minValue:(minValue + aNumber minValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
153 |
maxValue:(maxValue + aNumber maxValue) |
10825 | 154 |
! |
155 |
||
156 |
- aNumber |
|
157 |
^ MeasurementValue new |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
158 |
value:(value - aNumber value) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
159 |
minValue:(minValue - aNumber maxValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
160 |
maxValue:(maxValue - aNumber minValue) |
10825 | 161 |
! |
162 |
||
163 |
/ aNumber |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
164 |
^ MeasurementValue new |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
165 |
value:(value / aNumber value) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
166 |
minValue:(minValue / aNumber maxValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
167 |
maxValue:(maxValue / aNumber minValue) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
168 |
! ! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
169 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
170 |
!MeasurementValue methodsFor:'coercing & converting'! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
171 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
172 |
+/- error |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
173 |
"return a MeasurementValue with the given error." |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
174 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
175 |
"/ what should I do here - take the new error, |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
176 |
"/ or multiply them ???? |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
177 |
self error. |
10825 | 178 |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
179 |
minValue := value - error. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
180 |
maxValue := value + error. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
181 |
! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
182 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
183 |
coerce:aNumber |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
184 |
^ MeasurementValue value:aNumber minValue:aNumber maxValue:aNumber |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
185 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
186 |
! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
187 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
188 |
generality |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
189 |
"/ 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
|
190 |
"/ for mixed type operaions (i.e. Int * MeasurementValue). |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
191 |
"/ try it. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
192 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
193 |
^ value generality + 1 |
10825 | 194 |
! ! |
195 |
||
196 |
!MeasurementValue methodsFor:'comparing'! |
|
197 |
||
198 |
< aNumber |
|
199 |
^ maxValue < aNumber |
|
200 |
! |
|
201 |
||
202 |
lessFromFloat:aFloat |
|
203 |
"aFloat < self ?" |
|
204 |
||
205 |
^ aFloat < minValue |
|
206 |
! |
|
207 |
||
208 |
lessFromInteger:anInteger |
|
209 |
"anInteger < self ?" |
|
210 |
||
211 |
^ anInteger < minValue |
|
212 |
! ! |
|
213 |
||
214 |
!MeasurementValue methodsFor:'printing & storing'! |
|
215 |
||
216 |
printOn:aStream |
|
217 |
value printOn:aStream. |
|
218 |
'(' printOn:aStream. |
|
219 |
minValue printOn:aStream. |
|
220 |
'..' printOn:aStream. |
|
221 |
maxValue printOn:aStream. |
|
222 |
')' printOn:aStream. |
|
223 |
! ! |
|
224 |
||
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
225 |
!MeasurementValue methodsFor:'private accessing'! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
226 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
227 |
value:valueArg error:errorFraction |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
228 |
self |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
229 |
value:valueArg |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
230 |
minValue:(valueArg * (1-errorFraction)) |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
231 |
maxValue:(valueArg * (1+errorFraction)). |
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 |
|
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
234 |
value:valueArg minValue:minValueArg maxValue:maxValueArg |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
235 |
value := valueArg. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
236 |
minValue := minValueArg. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
237 |
maxValue := maxValueArg. |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
238 |
! ! |
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
239 |
|
10825 | 240 |
!MeasurementValue methodsFor:'testing'! |
241 |
||
242 |
between:min and:max |
|
243 |
minValue < min ifTrue:[^ false]. |
|
244 |
maxValue > max ifTrue:[^ false]. |
|
245 |
^ true |
|
246 |
! ! |
|
247 |
||
248 |
!MeasurementValue class methodsFor:'documentation'! |
|
249 |
||
250 |
version |
|
11457
5ff638783ee0
double dispatch (actually coercing) support
Claus Gittinger <cg@exept.de>
parents:
11456
diff
changeset
|
251 |
^ '$Header: /cvs/stx/stx/libbasic/MeasurementValue.st,v 1.3 2009-01-14 22:05:47 cg Exp $' |
10825 | 252 |
! ! |