cancel
Showing results for 
Search instead for 
Did you mean: 

float numbers in IEEE 754

jaimadafaca
Associate II
Posted on June 29, 2015 at 09:37

Hello Sirs,

Doesanybody use IEEE 754 format to represent float numbers?

I need toconvert values to this standard format, and I was wondering if someone of youhas already done it with any STM32F. 

I�d bepleased if you can share a sample code showing conversion.

For exmaple:

3.141516 

 0x40490e99

 

http://www.h-schmidt.net/FloatConverter/IEEE754.html

How to do it with C code?

thank youin advance!

Bestregards.

Jai 

#ieee-754-float-numbers
3 REPLIES 3
qwer.asdf
Senior
Posted on June 29, 2015 at 11:44

I'm sure your compiler is supporting the IEEE 754 standard, just use the float type and tell your compiler to use software floating point ABI (if your microcontroller has hardware FPU then use hardware floating point ABI for accelerated calculations). If you are using GCC then see the

-mfloat-abi options 

https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html

. These are the options of GCC that I'm using for STM32F4 with FPU to use hardware FPU: arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 Then you should also initialize the FPU of the MCU in your code before using it.Read more about hardware FPU

http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00047pdf

.
Posted on June 29, 2015 at 22:07

Look for source code of the atof() or atod() functions, which can be used directly, or indirectly through sscanf(), etc.

If you just want the 32-bit hex version, learn how to use casting of pointers.

void floattohex(float f)

{

   printf(''%08X\n'', *((unsigned long *)&f) );

}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on June 29, 2015 at 22:31

Could you do the conversion yourself if it was an integer? Do you have any familiarity with other fixed or floating point representations of data? Exponents?

Trying to understand where you're knowledge breaks down.

A full-up implementation of atod() is very complex. If you have a smaller numeric ranges, and want to understand the mechanics of the mantissa/exponent representation, you could ignore the decimal point, convert to an integer, counting the digits after the point, and multiply the answer via powf(), or divide using a power-of-ten accumulator.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..