Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tadzil opened this issue Feb 14, 2022 · 1 comment
Open

Comments

@tadzil
Copy link

tadzil commented Feb 14, 2022

$ /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
Copy link
Author

tadzil commented Feb 14, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant