#include <cstdio>
#include <cmath>
//Print-format a 32-bit float by exploiting exponent
//Blog post: http://a...content-available-to-author-only...b.io/print-fp.html
void print_fp( float number, int digits ) {
union {
float d;
int u;
};
d = number;
//deal with sign
bool minus = u >> 31;
u = u & ~(1 << 31);
//calculate base-10 exponent
double exp = (u >> 23) - 127;
double exp10 = exp / log2( 10.0 );
//adjust the number
u = (u & ~(0xFF << 23)) | (127 << 23);
double d2 = double(d) * pow( 10.0, exp10 - int(exp10) );
if( d2 >= 10.0 ) { d2 = d2 / 10.0; exp10+=1.0; }
if( d2 < 1.0 ) { d2 = d2 * 10.0; exp10-=1.0; }
//print sign
if( minus ) printf("-");
//print integer part
int i = int( d2 );
printf("%.1d.", i);
d2 = d2 - i;
while( digits-- ) {
d2 = d2 * 10.0;
int i = int( d2 );
printf("%.1d", i);
d2 = d2 - i;
}
//print exponent part
printf("e%d\n", int(exp10));
}
int main() {
double d;
d = 8.589973e9;
printf("%.10f\n", d);
print_fp( d , 10 );
d = 8.589974e9;
printf("%.10f\n", d);
print_fp( d , 10 );
}
CSNpbmNsdWRlIDxjc3RkaW8+CgkjaW5jbHVkZSA8Y21hdGg+CgogICAgLy9QcmludC1mb3JtYXQgYSAzMi1iaXQgZmxvYXQgYnkgZXhwbG9pdGluZyBleHBvbmVudAogICAgLy9CbG9nIHBvc3Q6IGh0dHA6Ly9hLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5iLmlvL3ByaW50LWZwLmh0bWwJCiAgICAKCXZvaWQgcHJpbnRfZnAoIGZsb2F0IG51bWJlciwgaW50IGRpZ2l0cyApIHsKCSAgICB1bmlvbiB7CgkgICAgICAgIGZsb2F0IGQ7CgkgICAgICAgIGludCB1OwoJICAgIH07CgkgICAgCgkgICAgZCA9IG51bWJlcjsKCQoJICAgIC8vZGVhbCB3aXRoIHNpZ24KCSAgICBib29sIG1pbnVzID0gdSA+PiAzMTsgCgkgICAgdSA9IHUgJiB+KDEgPDwgMzEpOyA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