converting to/from 32/64-bit float
converting to/from 32/64-bit float
So STM32F4 parts have only single float in hardware. As far as I can tell, there is no hardware support for conversion from single to double and back either... unless I missed something? I'm looking at a problem where I want to send 64 bit floats across the communication wire, and demote/promote to/from 32-bit floats on the Micropython side. It looks like I have to do that the bit-fiddly hard way unless someone has a better suggestion...
Re: converting to/from 32/64-bit float
I think you are looking for the struct module.
https://docs.python.org/3.6/library/str ... characters
https://docs.python.org/3.6/library/str ... characters
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: converting to/from 32/64-bit float
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: converting to/from 32/64-bit float
@deshipu -- Well, I want to pack/unpack from an I/O buffer, certainly. But I don't think struct.pack/unpack support the conversion code that I need: 1) take a native single float and pack it as a double into a bytes string, 2) unpack a double from a bytes string and demote it to a 32-bit single float, saturating at infinity or rounding towards zero at the extremities. Am I missing something there?
@pythoncoder, Thanks for the pointer to the 64-bit PR. In a few minutes of poking around I haven't found any 32/64 conversion -- which isn't to say that they aren't there -- I need to spend more time looking. I did find this amusing, 24-day old, commit message on libm:
I suppose if Micropython assumes either 32 bit or 64 bit floats at all times, there is no need for conversion.
deshipu gives me the idea, though, that the most desirable solution to my specific problem is to patch struct with a new format conversion code.
@pythoncoder, Thanks for the pointer to the 64-bit PR. In a few minutes of poking around I haven't found any 32/64 conversion -- which isn't to say that they aren't there -- I need to spend more time looking. I did find this amusing, 24-day old, commit message on libm:
Code: Select all
lib/libm/math: Remove implementations of float conversion functions.
deshipu gives me the idea, though, that the most desirable solution to my specific problem is to patch struct with a new format conversion code.
Re: converting to/from 32/64-bit float
The array module allows you to convert between bytes/float/double, but it's pretty similar to struct, and won't do the 32/64 bit conversion that you want.
The C functions to do conversion between 32/64 bit are available in the libgcc.a library, so that's why the (broken) implementations were removed from libm.
Side note: pyboard now has firmware builds with (software) double precision support, see: https://micropython.org/download/#pyboard
The C functions to do conversion between 32/64 bit are available in the libgcc.a library, so that's why the (broken) implementations were removed from libm.
Side note: pyboard now has firmware builds with (software) double precision support, see: https://micropython.org/download/#pyboard
Re: converting to/from 32/64-bit float
So I think I have been having a severely under-caffeinated moment here. Python, the language, supports only a single internal floating point representation. So, struct d and f formats must always convert from/to the single internal fp format to/from the specified buffer format. Nothing else makes sense. I'll go peek at the code for ustruct, but I suspect the d format is doing exactly what I want with respect to fp format conversions.