converting to/from 32/64-bit float

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

converting to/from 32/64-bit float

Post by dbc » Fri Jul 07, 2017 10:52 pm

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...

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: converting to/from 32/64-bit float

Post by deshipu » Sat Jul 08, 2017 12:24 am

I think you are looking for the struct module.

https://docs.python.org/3.6/library/str ... characters

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: converting to/from 32/64-bit float

Post by pythoncoder » Sat Jul 08, 2017 6:53 am

Peter Hinch
Index to my micropython libraries.

User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Re: converting to/from 32/64-bit float

Post by dbc » Sat Jul 08, 2017 11:58 pm

@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:

Code: Select all

 	lib/libm/math: Remove implementations of float conversion functions. 
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.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: converting to/from 32/64-bit float

Post by Damien » Sun Jul 09, 2017 6:41 am

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

User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Re: converting to/from 32/64-bit float

Post by dbc » Mon Jul 10, 2017 3:33 pm

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.

Post Reply