Sign |
Exponent |
Fraction (or
"Mantissa") |
1 bit-- 0 for positive 1 for negative |
8 unsigned bits-- 127 means 20 137 means 210 |
23 bits-- a binary fraction. Don't forget the implicit leading 1! |
/* IEEE floating-point number's bits: sign exponent mantissa */(Executable NetRun link)
struct float_bits {
unsigned int fraction:23; /**< Value is binary 1.fraction ("mantissa") */
unsigned int exp:8; /**< Value is 2^(exp-127) */
unsigned int sign:1; /**< 0 for positive, 1 for negative */
};
/* A union is a struct where all the fields *overlap* each other */
union float_dissector {
float f;
float_bits b;
};
float_dissector s;
s.f=8.0;
std::cout<<s.f<<"= sign "<<s.b.sign<<" exp "<<s.b.exp<<" fract "<<s.b.fraction<<"\n";
return 0;
C Datatype |
Size |
Approx. Precision |
Approx. Range |
Exponent Bits |
Fraction Bits |
+-1 range |
float |
4 bytes (everywhere) |
1.0x10-7 |
1038 |
8 |
23 |
224 |
double |
8 bytes (everywhere) |
2.0x10-15 |
10308 |
11 |
52 |
253 |
long double |
12-16 bytes (if it even exists) |
2.0x10-20 |
104932 |
15 |
64 |
265 |
Sign |
Exponent |
Mantissa (or Fraction) |
1 bit-- 0 for positive 1 for negative |
8 bits-- 127 means 20 137 means 210 |
23 bits-- a binary fraction. |
float z=0.0;Arithmetic on infinities works just the way you'd expect:infinity plus 1.0 gives infinity, etc. (See tables below). Positive and negative infinities exist, and work as you'd expect. Note that while divide-by-integer-zero causes a crash (divide by zero error), divide-by-floating-point-zero just happily returns infinity by default.
float f=1.0/z;
std::cout<<f<<"\n";
return (int)f;
float f=sqrt(-1.0);This is a "NaN", which is represented with a huge exponent and a *nonzero* fraction field. Positive and negative nans exist, but like zeros both signs seem to work the same. x86 seems to rewrite the bits of all NaNs to a special pattern it prefers (0x7FC00000 for float, with exponent bits and the leading fraction bit all set to 1).
std::cout<<f<<"\n";
return (int)f;
enum {n_vals=1000};On my P4, this gives 3ns for zeros and ordinary values, 300ns for denormals (a 100x slowdown), and 700ns for infinities and NaNs (a 200x slowdown)!
double vals[n_vals];
int average_vals(void) {
for (int i=0;i<n_vals-1;i++)
vals[i]=0.5*(vals[i]+vals[i+1]);
return 0;
}
int foo(void) {
int i;
for (i=0;i<n_vals;i++) vals[i]=0.0;
printf(" Zeros: %.3f ns/float\n",time_function(average_vals)/n_vals*1.0e9);
for (i=0;i<n_vals;i++) vals[i]=1.0;
printf(" Ones: %.3f ns/float\n",time_function(average_vals)/n_vals*1.0e9);
for (i=0;i<n_vals;i++) vals[i]=1.0e-310;
printf(" Denorm: %.3f ns/float\n",time_function(average_vals)/n_vals*1.0e9);
float x=0.0;
for (i=0;i<n_vals;i++) vals[i]=1.0/x;
printf(" Inf: %.3f ns/float\n",time_function(average_vals)/n_vals*1.0e9);
for (i=0;i<n_vals;i++) vals[i]=x/x;
printf(" NaN: %.3f ns/float\n",time_function(average_vals)/n_vals*1.0e9);
return 0;
}
+ | -nan | -inf | -big | -1 | -lil | -0 | +0 | +lil | +1 | +big | +inf | +nan |
-nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan |
-inf | nan | -inf | -inf | -inf | -inf | -inf | -inf | -inf | -inf | -inf | nan | nan |
-big | nan | -inf | -2e+30 | -big | -big | -big | -big | -big | -big | 0 | +inf | nan |
-1 | nan | -inf | -big | -2 | -1 | -1 | -1 | -1 | 0 | +big | +inf | nan |
-lil | nan | -inf | -big | -1 | -2e-40 | -lil | -lil | 0 | +1 | +big | +inf | nan |
-0 | nan | -inf | -big | -1 | -lil | -0 | 0 | +lil | +1 | +big | +inf | nan |
+0 | nan | -inf | -big | -1 | -lil | 0 | 0 | +lil | +1 | +big | +inf | nan |
+lil | nan | -inf | -big | -1 | 0 | +lil | +lil | 2e-40 | +1 | +big | +inf | nan |
+1 | nan | -inf | -big | 0 | +1 | +1 | +1 | +1 | 2 | +big | +inf | nan |
+big | nan | -inf | 0 | +big | +big | +big | +big | +big | +big | 2e+30 | +inf | nan |
+inf | nan | nan | +inf | +inf | +inf | +inf | +inf | +inf | +inf | +inf | +inf | nan |
+nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan |
- | -nan | -inf | -big | -1 | -lil | -0 | +0 | +lil | +1 | +big | +inf | +nan |
-nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan |
-inf | nan | nan | -inf | -inf | -inf | -inf | -inf | -inf | -inf | -inf | -inf | nan |
-big | nan | +inf | 0 | -big | -big | -big | -big | -big | -big | -2e+30 | -inf | nan |
-1 | nan | +inf | +big | 0 | -1 | -1 | -1 | -1 | -2 | -big | -inf | nan |
-lil | nan | +inf | +big | +1 | 0 | -lil | -lil | -2e-40 | -1 | -big | -inf | nan |
-0 | nan | +inf | +big | +1 | +lil | 0 | -0 | -lil | -1 | -big | -inf | nan |
+0 | nan | +inf | +big | +1 | +lil | 0 | 0 | -lil | -1 | -big | -inf | nan |
+lil | nan | +inf | +big | +1 | 2e-40 | +lil | +lil | 0 | -1 | -big | -inf | nan |
+1 | nan | +inf | +big | 2 | +1 | +1 | +1 | +1 | 0 | -big | -inf | nan |
+big | nan | +inf | 2e+30 | +big | +big | +big | +big | +big | +big | 0 | -inf | nan |
+inf | nan | +inf | +inf | +inf | +inf | +inf | +inf | +inf | +inf | +inf | nan | nan |
+nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan |
* | -nan | -inf | -big | -1 | -lil | -0 | +0 | +lil | +1 | +big | +inf | +nan |
-nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan |
-inf | nan | +inf | +inf | +inf | +inf | nan | nan | -inf | -inf | -inf | -inf | nan |
-big | nan | +inf | +inf | +big | 1e-10 | 0 | -0 | -1e-10 | -big | -inf | -inf | nan |
-1 | nan | +inf | +big | +1 | +lil | 0 | -0 | -lil | -1 | -big | -inf | nan |
-lil | nan | +inf | 1e-10 | +lil | 0 | 0 | -0 | -0 | -lil | -1e-10 | -inf | nan |
-0 | nan | nan | 0 | 0 | 0 | 0 | -0 | -0 | -0 | -0 | nan | nan |
+0 | nan | nan | -0 | -0 | -0 | -0 | 0 | 0 | 0 | 0 | nan | nan |
+lil | nan | -inf | -1e-10 | -lil | -0 | -0 | 0 | 0 | +lil | 1e-10 | +inf | nan |
+1 | nan | -inf | -big | -1 | -lil | -0 | 0 | +lil | +1 | +big | +inf | nan |
+big | nan | -inf | -inf | -big | -1e-10 | -0 | 0 | 1e-10 | +big | +inf | +inf | nan |
+inf | nan | -inf | -inf | -inf | -inf | nan | nan | +inf | +inf | +inf | +inf | nan |
+nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan |
/ | -nan | -inf | -big | -1 | -lil | -0 | +0 | +lil | +1 | +big | +inf | +nan |
-nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan |
-inf | nan | nan | +inf | +inf | +inf | +inf | -inf | -inf | -inf | -inf | nan | nan |
-big | nan | 0 | +1 | +big | +inf | +inf | -inf | -inf | -big | -1 | -0 | nan |
-1 | nan | 0 | 1e-30 | +1 | +inf | +inf | -inf | -inf | -1 | -1e-30 | -0 | nan |
-lil | nan | 0 | 0 | +lil | +1 | +inf | -inf | -1 | -lil | -0 | -0 | nan |
-0 | nan | 0 | 0 | 0 | 0 | nan | nan | -0 | -0 | -0 | -0 | nan |
+0 | nan | -0 | -0 | -0 | -0 | nan | nan | 0 | 0 | 0 | 0 | nan |
+lil | nan | -0 | -0 | -lil | -1 | -inf | +inf | +1 | +lil | 0 | 0 | nan |
+1 | nan | -0 | -1e-30 | -1 | -inf | -inf | +inf | +inf | +1 | 1e-30 | 0 | nan |
+big | nan | -0 | -1 | -big | -inf | -inf | +inf | +inf | +big | +1 | 0 | nan |
+inf | nan | nan | -inf | -inf | -inf | -inf | +inf | +inf | +inf | +inf | nan | nan |
+nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan | nan |
== | -nan | -inf | -big | -1 | -lil | -0 | +0 | +lil | +1 | +big | +inf | +nan |
-nan | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
-inf | 0 | +1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
-big | 0 | 0 | +1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
-1 | 0 | 0 | 0 | +1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
-lil | 0 | 0 | 0 | 0 | +1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
-0 | 0 | 0 | 0 | 0 | 0 | +1 | +1 | 0 | 0 | 0 | 0 | 0 |
+0 | 0 | 0 | 0 | 0 | 0 | +1 | +1 | 0 | 0 | 0 | 0 | 0 |
+lil | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +1 | 0 | 0 | 0 | 0 |
+1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +1 | 0 | 0 | 0 |
+big | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +1 | 0 | 0 |
+inf | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +1 | 0 |
+nan | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
< | -nan | -inf | -big | -1 | -lil | -0 | +0 | +lil | +1 | +big | +inf | +nan |
-nan | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
-inf | 0 | 0 | +1 | +1 | +1 | +1 | +1 | +1 | +1 | +1 | +1 | 0 |
-big | 0 | 0 | 0 | +1 | +1 | +1 | +1 | +1 | +1 | +1 | +1 | 0 |
-1 | 0 | 0 | 0 | 0 | +1 | +1 | +1 | +1 | +1 | +1 | +1 | 0 |
-lil | 0 | 0 | 0 | 0 | 0 | +1 | +1 | +1 | +1 | +1 | +1 | 0 |
-0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +1 | +1 | +1 | +1 | 0 |
+0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +1 | +1 | +1 | +1 | 0 |
+lil | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +1 | +1 | +1 | 0 |
+1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +1 | +1 | 0 |
+big | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +1 | 0 |
+inf | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+nan | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |