IEEE754 Single precision 32-bit - Converting int to float

239 views
Skip to first unread message

TFO

unread,
Feb 19, 2018, 4:24:49 PM2/19/18
to mitappinv...@googlegroups.com
This discussion about IEEE754 floats ending with an aia sample how to transmit a float variable to APP via bluetooth? is not working properly. Values below 1 is magnified by 2 and so on.

I have a hex number 3EFB1435 having int value 1056642101. The converted float should be close to 0.49038854

I found it works if I replace all references to the user designed "power" function in the demo with app inventor built in "base ^ exp".

Inspired by the B32 bit handling in the sample I made a single function to convert from float int stored as 32-bit int to float representation.

The faulty sample also says negative values is not supported, but the formula works regardless of sign in the +/-30.000 range where I tested it.

The int to float conversion intends to follow the calculation steps outlined here  Wikipedia - Single-precision_floating-point_format



Abraham Getzler

unread,
Feb 20, 2018, 11:12:29 AM2/20/18
to MIT App Inventor Forum
Thanks for the update.

I wasn't expecting any one else to need that code,
given the possibility of text encoding floats using the printf()
statement.

Unfortunately, I had no (and still don't have) hardware to test 
that code, so I worked only from the limited test data supplied by the original poster.
and the loose Wikipedia standard page.

I updated that thread to include a forward reference to this thread,
where I hope you will post a .aia export for the benefit of the next 
person needing that code.

ABG


Evan Patton

unread,
Feb 20, 2018, 3:52:09 PM2/20/18
to MIT App Inventor Forum
It is also worth mentioning that certain features, such as the BLE component, will attempt to cast input data to the appropriate type. This allows you to rely on the runtime to make the translation from int to float rather than doing it (inefficiently) in blocks.

Regards,
Evan
Reply all
Reply to author
Forward
0 new messages