fork download
  1. #include <stddef.h>
  2. #include <float.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. double frexp10( double x, int * exp )
  7. {
  8. double tmp = .0 > x ? -x : x;
  9. *exp = 0;
  10.  
  11. if( x == .0 )
  12. return x;
  13.  
  14. if( tmp >= 10. ) {
  15.  
  16. *exp = 1;
  17. for( ; !( ( tmp /= 10. ) < 10. ); ++( *exp ) );
  18.  
  19. } else if( ! ( tmp >= 1. ) ) {
  20.  
  21. *exp = -1;
  22. for( ; !( ( tmp *= 10. ) > 1. ); --( *exp) );
  23. }
  24.  
  25. return .0 > x ? -tmp : tmp;
  26. }
  27.  
  28. int main( void )
  29. {
  30. double input = 1234567890;
  31. double result = .0;
  32. int exp = 0;
  33. int i = 0;
  34.  
  35. for( ; i < 16; input /= 10, ++i ) {
  36.  
  37. printf( "input = %20lf ", input );
  38. result = frexp10( input, &exp );
  39. printf( "result = %011.10lf x 10^%d\n", result, exp );
  40. }
  41.  
  42. return EXIT_SUCCESS;
  43. }
  44.  
Success #stdin #stdout 0.01s 1676KB
stdin
Standard input is empty
stdout
input =    1234567890.000000   result = 1.2345678900 x 10^9
input =     123456789.000000   result = 1.2345678900 x 10^8
input =      12345678.900000   result = 1.2345678900 x 10^7
input =       1234567.890000   result = 1.2345678900 x 10^6
input =        123456.789000   result = 1.2345678900 x 10^5
input =         12345.678900   result = 1.2345678900 x 10^4
input =          1234.567890   result = 1.2345678900 x 10^3
input =           123.456789   result = 1.2345678900 x 10^2
input =            12.345679   result = 1.2345678900 x 10^1
input =             1.234568   result = 1.2345678900 x 10^0
input =             0.123457   result = 1.2345678900 x 10^-1
input =             0.012346   result = 1.2345678900 x 10^-2
input =             0.001235   result = 1.2345678900 x 10^-3
input =             0.000123   result = 1.2345678900 x 10^-4
input =             0.000012   result = 1.2345678900 x 10^-5
input =             0.000001   result = 1.2345678900 x 10^-6