- float v;
- char d[sizeof(float)];
- };
-
- v = f;
-#if BYTE_ORDER == LITTLE_ENDIAN
- for(unsigned i = sizeof(float); i--;)
- out.put(d[i]);
-#else
- for(unsigned i = 0; i<sizeof(float); ++i)
- out.put(d[i]);
-#endif
+ /* The structure of the float is unknown, so we must use arithmetic to
+ reduce it to components. */
+ bf.sign = f<0;
+ bf.exponent = 0;
+ bf.mantissa = 0;
+
+ if(f<0)
+ f = -f;
+ if(!(f+f>f))
+ bf.infinity = true;
+ else if(f!=0)
+ {
+ for(; f<1; f*=2)
+ --bf.exponent;
+ for(; f>=2; f/=2)
+ ++bf.exponent;
+ for(unsigned i=0; i<64; ++i)
+ {
+ bf.mantissa <<= 1;
+ if(f>=1)
+ {
+ bf.mantissa |= 1;
+ f -= 1;
+ }
+ f *= 2;
+ }
+ }
+ }
+
+ UInt64 encoded = bf.compose(float_precision);
+ for(unsigned i=float_precision/8; i--; )
+ out.put((encoded>>(i*8))&0xFF);