r/C_Programming•
Krotti83

Easiest way to convert floating point into structure or vice versa

Question

I'm working on a simple mathematics library for the purpose of education. Currently using a structure for the manipulation from the floating point value.

Example:

typedef struct {

unsigned int frac : 23; /* Fraction */

unsigned int expo : 8; /* Exponent */

unsigned char sign : 1; /* Sign */

} __attribute__((packed)) ieee754_bin32_t;

What is the easiest to convert a floating point value? For now I use a simple pointer:

float fval = 1.0;

ieee754_bin32_t *bval;

bval = (ieee754_bin32_t *) fval;

For a cleaner solution should I use memcpy instead?

Posted May 11, 2025, 4:48 PM UTC

723
Loading comments…