fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. pair<double, int> normalize(double x)
  8. {
  9. double zero = 0.0;
  10. double ten = 10.0;
  11. double one = 1.0;
  12. pair<double, int> normal {x, 0};
  13.  
  14. if ( abs(x) > zero && abs(x) < one ) {
  15. while ( abs(x *= ten) < ten )
  16. --normal.second;
  17. normal.first = x / ten;
  18. }
  19. else {
  20. while ( abs(x) >= ten ) {
  21. x /= ten;
  22. ++normal.second;
  23. }
  24. normal.first = x;
  25. }
  26.  
  27. return normal;
  28. }
  29.  
  30. int main()
  31. {
  32. double x; cin >> x;
  33. auto norm = normalize(x);
  34. showpos(cout);
  35.  
  36. cout << fixed << setprecision(10) << norm.first << 'e' << norm.second << endl;
  37. cout << scientific << x;
  38. return 0;
  39. }
Success #stdin #stdout 0s 3032KB
stdin
-99999952.46
stdout
-9.9999952460e+7
-9.9999952460e+07