Normalize floating point value

I have a problem with a PLC (programmable logic controller) that does not handle denormalized floating points.

Here are some hexadecimal representations of the numbers (denormalized) that I get from the controller on the opposite side of my PLC: 0x00004180, 0x0000C180, 0x00006FA0

Would anyone be so kind to share an example of a little code (C ++ / C # or similar) on how to bitwise normalize a value like the one above? I cannot use any floating point operations on numbers, since they are not recognized in the PLC, therefore only HEX / BIN operations.

Accuracy is not a problem.

+4
source share
2

, :

2 ^ {- 126}, , 0.mantissa * 2 ^ { -126}

int32_t, int_val * 2 ^ {- 126 - 23} = int_val * 2 ^ {- 149}. 23 , binary32 23 . , .

+3

, ( f - ):

int32_t x = f & 0x7F800000; // mask out sign and mantissa
x += 0x7F700000; // overflows unless exponent is zero; MSB now indicates normalized
x >> 31; // sign-extend the MSB to all bits
f &= x; // flush denormals to zero
+1

Source: https://habr.com/ru/post/1608722/


All Articles