Can I compare two numbers using Unit in the Last Place (ulps) in MATLAB?

6 views (last 30 days)
How can I compare two streams of floating-point numbers in MATLAB? Is there a ULP routine in MATLAB?
I have a Simulink model producing a stream a numbers. I generated C code and compiled it into a binary application. This binary application is a safety-critical system. The stream of numbers generated by the binary application cannot differ too much from those generated by the Simulink model (our mathematically correct reference). I would like to measure the differences between corresponding numbers in terms of Units in the Last Place (ulps) so that no two numbers differ by more than 128 ulps.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Feb 2021
Edited: MathWorks Support Team on 24 Feb 2021
There is no consensus on the definition of Unit in the Last Place. It is sometimes defined as the magnitude of the most significant bit that is different between two numbers. For example, the two following numbers differ by 6 ulps:
A = 0011111110111001100110011001100110011001100110011001100110100000
B = 0011111110111001100110011001100110011001100110011001100110011010
Another common definition is the number of floating-point numbers between A and B.
If your goal is to measure how much the stream generated by the C code differs from the stream generated by the Simulink model, then the second definition would be easier to use and more appropriate.
In MATLAB, the "eps" function returns the distance between a floating-point number "x" and the next floating-point number. It can be used to measure the distance between two floating-point numbers with respect to the second definition of ULP -- the number of points separating A and B.
Let us assume that the stream of floating-point numbers generated by the Simulink model is the reference. Ideally, the stream of numbers generated by the C code should be equal to the reference. However, a true real number (with infinite precision) might be rounded up in one implementation and down in the other, thus leading to numerical differences, which can add up.
For each pair in the output stream, you could compare the relative error between number A from the C code and the reference B (from the model) to the distance between B and the next floating-point number. In other words, we want to answer the question:
    How does the relative error between A and B compare to eps(B)?
or:
    abs(A-B) / abs(B) compared to eps(B)
For example, you could write a MATLAB routine that computes
u = abs(A-B) / (abs(B) * eps(B))
If "u" is small, then A and B are close to each other.
Most MATLAB algorithms try to keep the number "u" under 1 (where the reference would be the true number, if we had infinite precision). A reasonable "u" would be about 4 or 5, given that numerical errors can add up.
Using this method you could check whether the differences between the model and the generated C code are due to numerical errors or due to coding errors introduced when generating C code from the model.
You can read more about the "eps" function at the following link:

More Answers (0)

Categories

Find more on Create Large-Scale Model Components in Help Center and File Exchange

Tags

Products


Release

R2014a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!