Experimental Physics and Industrial Control System
Hi Geyang,
One should generally avoid to accumulate floating point values which have an
infinite number digits in dual representation, such as 0.1 or 0.2. It is like
adding 0.3333 instead of 1/3 in decimal. Integer values are much better for
counting than floats.
Floating point values are very bad in:
* accumulating small numbers to a large sum
* subtracting two numbers with a small difference
* deciding if two numbers are equal
Adding and subtracting is much more sensitive to rounding errors than
multiplying and dividing.
Dirk
Tim Mooney wrote:
> This is not actually a problem. 1.38777878078145e-16 is an excellent
> approximation to the desired value of zero, and an excellent approximation
> is the best you can ever hope for, with finite-precision arithmetic.
> You're accumulating round-off error each time you add or subtract 0.1.
> You could set PREC to, say, 6, and clients would only show 6 digits
> of the result. (I don't know right off if dbpr honors PREC, but it's
> just a diagnostic routine, so it won't usually matter.)
>
> Silver wrote:
>> hi, all:
>>
>> I am trying to use the record suggested by Ned Arnold. It's good work when
>> increment is 1. but when I change to 0.1, it has a strange problem. the db
>> file is like following:
>>
>> record(ai, "$(user):aiExample") { field(DESC, "Analog input") field(INP,
>> "$(user):calcExample.VAL NPP NMS") } record(calc, "$(user):calcExample") {
>> field(DESC, "Counter") field(SCAN,"1 second") field(FLNK,
>> "$(user):aiExample") field(CALC, "A-B>0?A>=0.9?0.8:A+0.1:A<0.1?0.1:A-0.1")
>> field(INPA, "$(user):calcExample.VAL NPP NMS") field(INPB,
>> "$(user):calcExample.LA NPP NMS") field(EGU, "Counts") }
>>
>> Following is dbtr message, we can see when the value change to 0.1, after
>> caculation, it's value change to VAL: 1.38777878078145e-16, can anyone give
>> some suggestion? thanks a lot in advance.
>>
>> epics> dbtr tcl:calcExample A: 0.2 ACKS: NO_ALARM ACKT: YES
>> ADEL: 0 ALST: 0.1 ASG: B: 0.3 BKPT:
>> 0x00 C: 0 CALC: A-B>0?A>=0.9?0.8:A+0.1:A<0.1?0.1:A-0.1
>> D: 0 DESC: Counter DISA: 0 DISP: 0
>> DISS: NO_ALARM DISV: 1 DTYP: <nil> E: 0
>> EGU: Counts EVNT: 0 F: 0 FLNK:DB_LINK tcl:aiExample
>> G: 0 H: 0 HHSV: NO_ALARM HIGH: 0 HIHI: 0
>> HOPR: 0 HSV: NO_ALARM HYST: 0 I: 0 INPA:DB_LINK
>> tcl:calcExample.VAL NPP NMS INPB:DB_LINK tcl:calcExample.LA NPP NMS
>> INPC:CONSTANT INPD:CONSTANT INPE:CONSTANT INPF:CONSTANT
>> INPG:CONSTANT INPH:CONSTANT INPI:CONSTANT INPJ:CONSTANT
>> INPK:CONSTANT INPL:CONSTANT J: 0 K: 0 L:
>> 0 LA: 0.2 LALM: 0.1 LB: 0.3 LC: 0
>> LCNT: 0 LD: 0 LE: 0 LF: 0 LG: 0
>> LH: 0 LI: 0 LJ: 0 LK: 0
>> LL: 0 LLSV: NO_ALARM LOLO: 0 LOPR: 0
>> LOW: 0 LSV: NO_ALARM MDEL: 0 MLST: 0.1
>> NAME: tcl:calcExample NSEV: NO_ALARM NSTA: NO_ALARM
>> PACT: 0 PHAS: 0 PINI: NO PREC: 0
>> PRIO: LOW PROC: 0 PUTF: 0 RPRO: 0
>> SCAN: 1 second SDIS:CONSTANT SEVR: NO_ALARM STAT: NO_ALARM
>> TPRO: 0 TSE: 0 TSEL:CONSTANT UDF: 0
>> VAL: 0.1 epics> dbtr tcl:calcExample A: 0.1 ACKS: NO_ALARM
>> ACKT: YES ADEL: 0 ALST: 1.38777878078145e-16 ASG:
>> B: 0.2 BKPT: 0x00 C: 0 CALC: A-B>0?A>=0.9?0.8:A+0.1:A<0.1?0.1:A-0.1
>> D: 0 DESC: Counter DISA: 0 DISP: 0 DISS:
>> NO_ALARM DISV: 1 DTYP: <nil> E: 0 EGU:
>> Counts EVNT: 0 F: 0 FLNK:DB_LINK tcl:aiExample
>> G: 0 H: 0 HHSV: NO_ALARM HIGH: 0
>> HIHI: 0 HOPR: 0 HSV: NO_ALARM HYST: 0
>> I: 0 INPA:DB_LINK tcl:calcExample.VAL NPP NMS
>> INPB:DB_LINK tcl:calcExample.LA NPP NMS INPC:CONSTANT INPD:CONSTANT
>> INPE:CONSTANT INPF:CONSTANT INPG:CONSTANT INPH:CONSTANT
>> INPI:CONSTANT INPJ:CONSTANT INPK:CONSTANT INPL:CONSTANT
>> J: 0 K: 0 L: 0 LA: 0.1
>> LALM: 1.38777878078145e-16 LB: 0.2 LC: 0
>> LCNT: 0 LD: 0 LE: 0 LF: 0
>> LG: 0 LH: 0 LI: 0 LJ: 0
>> LK: 0 LL: 0 LLSV: NO_ALARM LOLO: 0
>> LOPR: 0 LOW: 0 LSV: NO_ALARM MDEL: 0
>> MLST: 1.38777878078145e-16 NAME: tcl:calcExample
>> NSEV: NO_ALARM NSTA: NO_ALARM PACT: 0 PHAS: 0
>> PINI: NO PREC: 0 PRIO: LOW PROC: 0
>> PUTF: 0 RPRO: 0 SCAN: 1 second SDIS:CONSTANT
>> SEVR: NO_ALARM STAT: NO_ALARM TPRO: 0 TSE: 0
>> TSEL:CONSTANT UDF: 0 VAL: 1.38777878078145e-16
>> epics> dbtr tcl:calcExample A: 1.38777878078145e-16 ACKS:
>> NO_ALARM ACKT: YES ADEL: 0 ALST: 0.1 ASG:
>> B: 0.1 BKPT: 0x00 C: 0 CALC: A-B>0?A>=0.9?0.8:A+0.1:A<0.1?0.1:A-0.1
>> D: 0 DESC: Counter DISA: 0 DISP: 0 DISS:
>> NO_ALARM DISV: 1 DTYP: <nil> E: 0 EGU:
>> Counts EVNT: 0 F: 0 FLNK:DB_LINK tcl:aiExample
>> G: 0 H: 0 HHSV: NO_ALARM HIGH: 0
>> HIHI: 0 HOPR: 0 HSV: NO_ALARM HYST: 0
>> I: 0 INPA:DB_LINK tcl:calcExample.VAL NPP NMS
>> INPB:DB_LINK tcl:calcExample.LA NPP NMS INPC:CONSTANT INPD:CONSTANT
>> INPE:CONSTANT INPF:CONSTANT INPG:CONSTANT INPH:CONSTANT
>> INPI:CONSTANT INPJ:CONSTANT INPK:CONSTANT INPL:CONSTANT
>> J: 0 K: 0 L: 0 LA: 1.38777878078145e-16
>> LALM: 0.1 LB: 0.1 LC: 0 LCNT: 0 LD: 0
>> LE: 0 LF: 0 LG: 0 LH: 0 LI: 0
>> LJ: 0 LK: 0 LL: 0 LLSV: NO_ALARM
>> LOLO: 0 LOPR: 0 LOW: 0 LSV: NO_ALARM
>> MDEL: 0 MLST: 0.1 NAME: tcl:calcExample
>> NSEV: NO_ALARM NSTA: NO_ALARM PACT: 0 PHAS: 0
>> PINI: NO PREC: 0 PRIO: LOW PROC: 0
>> PUTF: 0 RPRO: 0 SCAN: 1 second SDIS:CONSTANT
>> SEVR: NO_ALARM STAT: NO_ALARM TPRO: 0 TSE: 0
>> TSEL:CONSTANT UDF: 0 VAL: 0.1 epics>
>>
>> Best regards Geyang 2008-11-23
>>
>>
>
--
Dr. Dirk Zimoch
Paul Scherrer Institut, WBGB/006
5232 Villigen PSI, Switzerland
Phone +41 56 310 5182
- References:
- A bug of calc record? Silver
- Re: A bug of calc record? Tim Mooney
- Navigate by Date:
- Prev:
Re: compile error about gateway Dirk Zimoch
- Next:
Records not in Base David Dudley
- Index:
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
<2008>
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
- Navigate by Thread:
- Prev:
Re: A bug of calc record? Tim Mooney
- Next:
CSS 1.1.0 Hatje, Jan
- Index:
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
<2008>
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025