On Oct 21, 12:43*pm, "(2b|!2b)== ?" <void-s...@ursa-major.comwrote:
Ian Collins wrote:
(2b|!2b)==? wrote:
As part of a data storage project, I am storing floats in 3
byte ints as follows:
* First 21 bits represent number
* Last 3 bits represent the number of decimal places
What do you do if they don't fit?
they will fit (see below)Do they have a fixed range?
Yes, and the range is covered by the bit representation
prescribed>I need to write macros that will convert TO/FROM
THREE_BYTE_VALU E and float
Why macros?
I generellay subscribe to the 'macros are evil' mantra - but
in this case, using macros would be a lot easier (read quicker
to implement), than say using template functions (with
specializations ) for the data types to be stored.
Why would the functions have to be templates? What's wrong with
just a regular function?
Writing a safe macro to do this is relatively tricky, but a
function shouldn't be that hard, using standard functions like
ldexp and frexp. Something like:
unsigned int
toExternalRepre sentation(
float value )
{
int exp ;
value = frexpf( value, &exp ) ;
return static_cast< int >( value * 2097152 )
| (static_cast< unsigned >( exp ) << 21) ;
}
This obviously requires some additional error checking, and may
contain an off by one error---I'm two lazy to verify whether the
results of frexpf are in the range [0.5,1.0) or [1.0,2.0). But
you get the idea.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34