Matching Floating-Point Numbers [message #1766521] |
Thu, 22 June 2017 15:16 |
|
Hey everyone,
currently, I'm building a Test System which simply pushes data over MQTT into our cloud, reads them back over HTTP and compares the data subsequently. The basic flow of the data looks as follows
+------------+ +-------------+
| TestSystem | MQTT publish | SUT |
| +--------------------> |
| | | |
| | HTTP GET Request | |
| <--------------------+ |
+------------+ +-------------+
And here my problem begins.
When I publish integers everything is fine and works as intended. But when I publish float values the matching logic does not work anymore.
Because the data is processed in the back-end before I get it back the reason is quite obvious. The difference between the sent and received floats is roughly 2.0e-7.
I'm aware of that comparing floating-point numbers should be done somehow like if(abs(floatA - floatB) < threshold)
But in my case, I'm using the match function to compare templates with the responses coming from the SUT which may or may not contain floating-point numbers.
Is it still possible to use the match function in such cases?
Kind regards,
Alex
|
|
|
Re: Matching Floating-Point Numbers [message #1766541 is a reply to message #1766521] |
Thu, 22 June 2017 19:54 |
|
Hi Alex,
you can use a template expressed as a range of float values
which could be (value-2.0e-7..value+2.0e-7)
e.g. something like:
template float t_float:=(0.99..1.1);
and a float variable which will store the received value
myPort.receive(float:?) -> value v_float;
and match them as below:
module f_test
{
template float t_float:=(0.99..1.1);
control {
var float v_float:=1.0;
var float v_float1:=0.98;
if(match(v_float, t_float)) {log("match")} //this will match
else {log("no match")}
if(match(v_float1, t_float)) {log("match1")}
else {log("no match1")} //this will not
}
}
or directly at the receive statement:
myPort.receive(t_float) -> value v_float; //will match only if the received value is within range
I hope this helps
BR
Elemer
[Updated on: Fri, 23 June 2017 05:56] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03017 seconds