I have a situation which requires a float to be represented in a single char. The range that this ‘minifloat’ needs to represent is 0 to 10e-7, so we can always assume that the number is +ve, and the exponent -ve in order to save space.
The representation that I have thought about going with is 3 bits of exponent, and 5 bits mantissa (with 1 implied bit), with the exponent being in base 10, i.e. x = man * 10^exp.
To convert from a float to my minifloat, I plan to use frexp, and use some maths to convert from base 2 to base 10.
Is this a sensible approach? Or are there better ways to achieve this?
Do you actually need the value to be floating point (i.e. to have roughly constant precision as the value scales)? What are you going to do with these values?
A much simpler (and more efficient) idea would be to interpret 8 bits as an unsigned fixed-point number with an implicit scale of 1e-7. I.e.: