Calculation of Float data type

Professional Windows® software to communicate with CAN and CAN FD busses and its add-ins: Plotter, CANdb Import, Instruments Panel, and J1939
Post Reply
Ruud van Dijk
Posts: 6
Joined: Thu 21. Mar 2019, 10:31

Calculation of Float data type

Post by Ruud van Dijk » Wed 13. Nov 2019, 12:12

Hello PEAK,

I am creating a python script that converts CAN messages to the right information. Now we have a unit that uses the data type float. How are these converted to the correct decimal value in the PCAN Symbol Editor?
Because we get the data well in PCAN Explorer 6, but not if we want to convert a trc file to csv.

I have already found something how to convert the 4 bytes for float to decimal, but I do not come to the value that we see with PCAN Explorer 6.
I know that the 1st bit is used for the sign (VZ)(positive or negative). The following 8 bits are for the Exponent (e) and the remaining 23 bits for the Mantisse (M).

When I am going to calculate the following float 2D 40 2C 42 (0010 1101 0100 0000 0010 1100 0100 0010) this should be 43,06267166 in decimal. The 1st bit indicates 0 = VZ and means positive. Then the next 8 bits must be 'e'.
That is in hex 5A and in decimal 90 = e. The remaining bits give the value 1,5013506 =M . When I put this in the formula (-1)^VZ * M * 2^e = result. I get 1,0923764e^-11 but not 43,06267166.

Am I doing something wrong or is there another calculation that will get me the result we see with PCAN Explorer 6?

Kind regards,
Ruud van Dijk

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: Calculation of Float data type

Post by M.Heidemann » Wed 13. Nov 2019, 13:03

Hello Ruud,

Since you use python you can probably simplify this:

Use the Python Library "Struct" and try the following:

Code: Select all

# Python 3.7

from struct import *

hexbytes = b"\x2D\x40\x2C\x42"

print(unpack("<f",hexbytes)[0])
Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

Ruud van Dijk
Posts: 6
Joined: Thu 21. Mar 2019, 10:31

Re: Calculation of Float data type

Post by Ruud van Dijk » Wed 13. Nov 2019, 15:18

Hello Marvin,

Thank you for the response.
We are going to try this in python.

In the meantime I have also found why the value is incorrect compared to what we see with PCAN Explorer 6.
It was the byte order Float - Big Endian (ABCD) or Float - Little Endian (DCBA), or as can be chosen in the symbol editor under "Data Format" between Motorola or Intel (Big Endian or Litlle Endian). It is a pity that I have not noticed it before. :oops:

When I perform the calculation on paper according to the byte order "Little Endian", the result is 100% correct.

Kind regards,
Ruud

Post Reply