MeasurementValue.st
author Claus Gittinger <cg@exept.de>
Wed, 14 Jan 2009 22:06:25 +0100
changeset 11456 3ab3ea28c8f4
parent 10825 332b3ea2f8d6
child 11457 5ff638783ee0
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 2007 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic' }"

Number subclass:#MeasurementValue
	instanceVariableNames:'value minValue maxValue'
	classVariableNames:''
	poolDictionaries:''
	category:'Magnitude-Numbers'
!

!MeasurementValue class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2007 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    A MeasurementValue is a numeric value with an error, such as returned
    by measurement devices (Volt-Meter). For example, if a measurement-device has
    an error of 10%, a measured value of 20 could be any value between 18 and 22.

    Arithmetic operations keep track of the error; if any operand is a MeasurementValue,
    the operation returns a MeasurementValue as result.

    This class is possibly unfinished and needs more arithmetic methods.
    For now, the stuff found here represents our needs and more might be added in the future.

    Also notice, that instances do not keep the error as a fraction, but instead a min. and maxValue.
    That means, that we can represent tha case where the absolute error values are different in
    the positive and negative directions.
    I am not sure if this is more flexibility than needed in the long run.

    [author:]
        Claus Gittinger

    [see also:]
        Number
        Float ShortFloat Fraction FixedPoint Integer Complex
        FloatArray DoubleArray
"
!

examples
"
    |voltage current power|

    voltage := MeasurementValue value:10 error:0.05.
    current := MeasurementValue value:2 error:0.1.
    power := voltage * current.
    power
"
! !

!MeasurementValue class methodsFor:'instance creation'!

value:arg1 error:arg2
    "return a new measurementValue with a given value and an error (fraction)"

    ^ self new value:arg1 error:arg2

    "
     MeasurementValue value:10 error:0.2 
    "
!

value:arg1 minValue:arg2 maxValue:arg3
    "return a new measurementValue with a given value and an error given as min-max values.
     Use this, if the error is not the same in both directions"

    ^ self new value:arg1 minValue:arg2 maxValue:arg3

    "a power of 10 error:
     MeasurementValue value:5 minValue:1 maxValue:10   
    "
! !

!MeasurementValue methodsFor:'accessing'!

maxValue
    ^ maxValue
!

minValue
    ^ minValue
!

value
    ^ value
!

value:valueArg error:errorFraction 
    self 
        value:valueArg
        minValue:(valueArg * (1-errorFraction))
        maxValue:(valueArg * (1+errorFraction)). 
!

value:valueArg minValue:minValueArg maxValue:maxValueArg 
    value := valueArg.
    minValue := minValueArg.
    maxValue := maxValueArg.
! !

!MeasurementValue methodsFor:'arithmetic'!

* aNumber
    (aNumber isKindOf:MeasurementValue) ifTrue:[
        ^ MeasurementValue new
            value:(value * aNumber value)
            minValue:(minValue * aNumber minValue)
            maxValue:(maxValue * aNumber maxValue)
    ].

    ^ MeasurementValue new
        value:(value * aNumber)
        minValue:(minValue * aNumber)
        maxValue:(maxValue * aNumber)
!

+ aNumber
    (aNumber isKindOf:MeasurementValue) ifTrue:[
        ^ MeasurementValue new
            value:(value + aNumber value)
            minValue:(minValue + aNumber minValue)
            maxValue:(maxValue + aNumber maxValue)
    ].

    ^ MeasurementValue new
        value:(value + aNumber)
        minValue:(minValue + aNumber)
        maxValue:(maxValue + aNumber)
!

- aNumber
    (aNumber isKindOf:MeasurementValue) ifTrue:[
        ^ MeasurementValue new
            value:(value - aNumber value)
            minValue:(minValue - aNumber maxValue)
            maxValue:(maxValue - aNumber minValue)
    ].

    ^ MeasurementValue new
        value:(value - aNumber)
        minValue:(minValue - aNumber)
        maxValue:(maxValue - aNumber)
!

/ aNumber
    (aNumber isKindOf:MeasurementValue) ifTrue:[
        ^ MeasurementValue new
            value:(value / aNumber value)
            minValue:(minValue / aNumber maxValue)
            maxValue:(maxValue / aNumber minValue)
    ].

    ^ MeasurementValue new
        value:(value / aNumber)
        minValue:(minValue / aNumber)
        maxValue:(maxValue / aNumber)
! !

!MeasurementValue methodsFor:'comparing'!

< aNumber
    ^ maxValue < aNumber
!

lessFromFloat:aFloat
    "aFloat < self ?"

    ^ aFloat < minValue
!

lessFromInteger:anInteger
    "anInteger < self ?"

    ^ anInteger < minValue
! !

!MeasurementValue methodsFor:'printing & storing'!

printOn:aStream
    value printOn:aStream.
    '(' printOn:aStream.
    minValue printOn:aStream.
    '..' printOn:aStream.
    maxValue printOn:aStream.
    ')' printOn:aStream.
! !

!MeasurementValue methodsFor:'testing'!

between:min and:max
    minValue < min ifTrue:[^ false].
    maxValue > max ifTrue:[^ false].
    ^ true
! !

!MeasurementValue class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/MeasurementValue.st,v 1.2 2009-01-14 21:06:25 cg Exp $'
! !