mbpoll icon indicating copy to clipboard operation
mbpoll copied to clipboard

Problem with float output in exponential: different from what was written

Open tadzil opened this issue 4 years ago • 1 comments

$ /usr/local/bin/mbpoll -t4:float -0 -B -1 -q -r 10 127.0.0.1 -p 1502 -- 1269349 Written 1 references.

$ /usr/local/bin/mbpoll -t4:float -0 -B -1 -q -r 10 127.0.0.1 -p 1502 -- Polling slave 1... [10]: 1.26935e+06

$ /usr/local/bin/mbpoll -t4:hex -c2 -0 -B -1 -q -r 10 127.0.0.1 -p 1502 -- Polling slave 1... [10]: 0x499A [11]: 0xF328

As you can see - if I convert read (output to shell) value "1.26935e+06" to normal number, it will be 1269350 instead of 1269349. Using online IEEE-754 converter hex is decoded normally to 1269349. Why is it displayed in exponential form?

tadzil avatar Feb 14 '22 05:02 tadzil

If I do conversation from hex to iee-754 float in bash, I get correct value:

$ /usr/local/bin/mbpoll -t4:float -0 -B -1 -q -r 10 127.0.0.1 -p 1502 -- Polling slave 1... [10]: 1.26935e+06

$ /usr/local/bin/mbpoll -t4:int -0 -B -1 -q -r 10 127.0.0.1 -p 1502 -- Polling slave 1... [10]: 1234891560

$ /usr/local/bin/mbpoll -t4:hex -c2 -0 -B -1 -q -r 10 127.0.0.1 -p 1502 -- Polling slave 1... [10]: 0x499A [11]: 0xF328

$ printf '%x\n' $(/usr/local/bin/mbpoll -t4:int -0 -B -1 -q -r 10 127.0.0.1 -p 1502 | awk 'FNR == 2 {print $2}') 499af328 $ printf '%x\n' $(/usr/local/bin/mbpoll -t4:int -0 -B -1 -q -r 10 127.0.0.1 -p 1502 | awk 'FNR == 2 {print $2}') | python3 -c 'import struct; print(struct.unpack("!f", bytes.fromhex(input()))[0])' 1269349.0

tadzil avatar Feb 14 '22 06:02 tadzil