Avoiding Measurement Errors from Manipulating Data in Software

Download Report

Transcript Avoiding Measurement Errors from Manipulating Data in Software

Avoiding Measurement Errors from
Manipulating Data in Software
Are you sure your software isn’t adding uncertainty
to your measurements???
Speaker/Author:
Logan Kunitz
Staff Calibration Engineer
National Instruments R&D
ni.com
Sources of Measurement Error
+
_
Signal Source
ADC
Offset / Bias
and Noise Errors
ni.com
Acquire Data
External
Conditioning and
Cabling
Process and
Display Reading
Digitization and
Sampling Errors
2
Software
Errors
Sources of Software Errors
•
Coding Mistakes / Logic Errors
•
•
•
Data Processing Errors
•
•
Often a logic error in software will result in very large errors, but
sometimes these errors can be small enough to go undetected
Example – units conversion issues (imperial vs. metric)
i.e. Linear Approximation, Data Extrapolation, Averaging, etc…
Data Manipulation Errors
•
•
•
Artifacts of how data is handled by the compiler and processor
Quantify to ensure negligible impact to the measurement
Examples:
o
o
ni.com
Rounding Errors
Numerical Errors
3
Defining “Negligible” Error Impact
•
Software Error Expectation: “Negligible Impact”
•
•
Ok… so, how do I define negligible?
•
•
•
•
•
Should not need to account for these errors in uncertainty
analysis
“Really Small”
Just go a ‘couple’ digits past the least significant digit
“10 times smaller is enough”
As long as it doesn’t affect the application
Unable to find a quantifiable and objective definition
•
ni.com
So… I came up with my own definition
4
Logan’s Definition of “Negligible” Error Impact
•
Negligble U(x) Definition
•
•
A number that does not affect reported measurement uncertainty
U(y) with >95% confidence
If Uncertainty U(y) = 2.3
•
ni.com
U(x) <0.005

95% chance of not affecting uncertainty U(y)
5
Rounding Errors
•
When the number of displayed digits is truncated (and
rounded)
•
•
Error from Rounding
•
•
1.2468642 -> 1.2469 -> 1.2 -> 1
+/- ½ Least Significant Digit or 5x10(-Digits)
1.3
Example:
•
•
•
2 Digits Displayed
1.246 -> 1.2
Error = +/- 0.05
Actual Value
1.2
Reading = 1.2
1.1
1.0
•
Negligible = Equivalent to rounding to 1 digit past Least
Significant Digit of Uncertainty
ni.com
6
Rounding Error Use Cases
•
Numeric -> String Conversions
•
•
When numeric data gets converted to a string, least
significant digits will be rounded/truncated based on
the application requirements
Examples:
•
Displaying measurement results in a user interface
o
•
•
•
ni.com
i.e. DMM # of Digits
Logging results to a report
Logging data to a file (such as .csv, XML, Excel,
etc..), to be read back by the same or different
application
Converting a number to string for data transfer (i.e.
GPIB / Serial communication with and instrument, or
transferring to another application over TCP/IP)
7
Selecting Format for Numeric Representation
Format Type
Example
Advantages/Disadvantag
es
Standard Floating Point
Representation
1234.56789
Advantage: Simple
Disadvantage: Wasted 0’s
displayed for large and small
numbers (i.e. 120000000, or
0.000000456)
SI notation
1.23456789k
Advantage: Reduce # 0’s reported
for large and small numbers
Disadvantage: Limited set of
prefixes (i.e. no prefix for 10^-5)
Engineering/
Scientific Notation
1.23456789E3 or
1.23456789x103
Advantage: More flexible
exponent over SI notation
Advantage: Common format for
instrument communication and
data storage
Binary
@“JE„ôÆç
Advantage: No precision loss
Representing the binary equivalent
of the number in a string (byte
array)
ni.com
Advantage: Smaller data footprint,
lower storage space required
Disadvantage: Implementation is
application-specific
8
Numerical Errors
•
Errors resulting from choice of datatype
•
Floating Point Numbers
o
o
o
•
•
Single Precision
Double Precision
Extended Precision
Integers (i16, i32, i64, u8, etc…)
Numeric Datatypes are Approximations
•
Limitation of computers’ representation of the numeric values
High Resolution vs. Low Resolution
10.00
8.75
7.50
6.25
5.00
3.75
2.50
1.25
0|
0
ni.com
|
50
|
100
9
|
150
Floating Point Numeric Details
Single ->
Double->
•
IEEE 754 standardizes the floating point implementation
•
•
•
Single (32 bit) and Double (64 bit) are most common and
consistent implementations
Extended precision (80 bit)
Other Numeric Data-types for High Precision in Software
•
•
ni.com
128 bit Extended Precision
Arbitrary-Precision Arithmetic
10
Numerical Error Example
•
Error representing Pi using different numeric datatypes
Datatype
Value of Pi
Error
3.1415926535897932384626433832795028…
Aprox.
Digits
-
Actual
Single Precision
Float (32 bit)
3.1415927410125732400
7
2.78E-6%
Double Precision
Float (64 bit)
3.1415926535897926700
15
1.8E-14%
Extended Double
3.1415926535897932380
Precision Float (80
bit)
19
1.4E-17%
Integer
-
4.5%
ni.com
3
11
-
Machine Epsilon:
•
•
•
Quantified error for a floating point number
Value is the ratio of maximum error to the stored value
Development Environments (ADEs) often have a function
to calculate 
Datatype
Machine Epsilon
Single Precision
Double Precision
Extended Double (80bit)
2 -23 = 1.19e-07
ni.com
2 -52 = 2.22e-16
Decimal Digits of
Precision
~7.2 Digits
~15.9 digits
2 -63 = 1.08e-19
~19.2 digits
12
Using Machine Epsilon
•
The following functions describe basic methods for applying machine
epsilon based on the math operations of the components
•
Note: x and y will only be different if X and Y are different datatypes
Function Examples
Error Equations
Error for a Single Value
Addition Operations
Multiplication Operations
ni.com
13
Issues Comparing Floating Point Numbers
•
Using “equals” operation with floating numbers can fail to
return expected resulted
•
•
•
•
•
Example:
Evaluates to “False”
C version
G version
Why?
•
•
ni.com
Complicated
Because 0.1 is an approximation
when represented as floating
number
14
Use Machine Epsilon to Compare Floating Point
Numbers
•
Apply calculated machine epsilon error to the floating
point results
•
•
•
ni.com
Example:
C version
G version
15
Datatype Conversions
•
•
Errors associated with conversion operations on
datatypes in an application
Many conversion handled “automatically” by the ADE
•
•
Example: function z = x + y
C Example:
Single z; Double x; Double y;
z = x + y;
•
•
G Example (LabVIEW)
•
Double precision numbers get coerced to Single precision after
addition operation
Use machine epsilon of coerced datatype to determine
error
ni.com
16
Conclusion
•
•
•
•
•
Quantify if an error is ‘negligible’ for your applications
Avoid rounding errors when converting from numeric -> string
Numeric datatype selection should match precision
requirements of your data
Use machine epsilon to quantify error of floating precision
numbers
Never compare two floating point numbers using the ‘equals’
operator
References:
1.
2.
3.
4.
5.
6.
7.
ni.com
"IEEE Floating Point." Wikipedia. <http://en.wikipedia.org/wiki/IEEE_floating_point>.
"Extended Precision." Wikipedia. <http://en.wikipedia.org/wiki/Extended_precision>.
"Machine Epsilon." Wikipedia. <http://en.wikipedia.org/wiki/Machine_epsilon>.
"Floating-point Errors." UAlberta.ca. <http://www.ualberta.ca/~kbeach/comp_phys/fp_err.html>.
"Arbitrary-precision Arithmetic." Wikipedia. <http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic>.
"Digital Multimeter Measurement Fundamentals." NI.com. <http://www.ni.com/white-paper/3295/en/>.
Schmidt, Darren. "An Introduction to Floating-Point Behavior in LabVIEW." NI.com.
<ftp://ftp.ni.com/pub/devzone/tut/floating_point_in_labview.pdf>.
17
Questions?
ni.com
18