Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 18673

Creating floats from binary value in Python

$
0
0

I'm currently working on a project which requires me to modify bit values of float variables. To do this, I am using the struct package to acquire the binary values of a float as a string, like so:

num = -5.661166216897089s = struct.pack('!f', num)b = ''.join(format(c, '08b')for c in s)

I will then modify the desired bits with splicing. My question is:

After modifying the string of bits as needed, how can I use it to create a new float value with the modified bit sequence? Defining it as such:

new_float = 0b11000000101101010010100001000110

does not work, I suspect as it is being interpreted as an integer. Is there a way to force float creation?

Thanks ^^


Viewing all articles
Browse latest Browse all 18673

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>