fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. vector<pair<double,const char*>> numbers = {
  10. { 1234567, "12e5" },
  11. { 12345.6, "12346" },
  12. { 1234.56, "1235" },
  13. { 123.456, "123.5" },
  14. { 12.3456, "12.35" },
  15. { 1.23456, "1.235" },
  16. { 1.23450, "1.235" },
  17. { 1.23400, "1.234" },
  18. { 1.23000, "1.23" },
  19. { 1.20000, "1.2" },
  20. { 1.00000, "1" },
  21. { 0.11111, "0.111" },
  22. { 0.01111, "0.011" },
  23. { 0.00111, "0.001" },
  24. { 0.00011, "11e-4" },
  25. { 0.00001, "1e-5" },
  26. { 0.11111, "0.111" },
  27. { 0.01111, "0.011" },
  28. { 0.00111, "0.001" },
  29. { 0.00011, "11e-4" },
  30. { 0.00001, "1e-5" }
  31. };
  32.  
  33. cout << "printf(\"%.3f\");" << endl << endl;
  34.  
  35. for (auto& pair : numbers)
  36. {
  37. char buf[255];
  38. std::fill(begin(buf), end(buf), 0);
  39. sprintf(buf, "%.3f", pair.first);
  40. bool match = strcmp(buf, pair.second) == 0;
  41. cout << "match " << match << " in " << pair.first << " out " << buf << " expected " << pair.second << endl;
  42. }
  43.  
  44. cout << endl << "printf(\"%.3g\");" << endl << endl;
  45.  
  46. for (auto& pair : numbers)
  47. {
  48. char buf[255];
  49. std::fill(begin(buf), end(buf), 0);
  50. sprintf(buf, "%.3g", pair.first);
  51. bool match = strcmp(buf, pair.second) == 0;
  52. cout << "match " << match << " in " << pair.first << " out " << buf << " expected " << pair.second << endl;
  53. }
  54.  
  55. cout << endl << "printf(\"%.4g\");" << endl << endl;
  56.  
  57. for (auto& pair : numbers)
  58. {
  59. char buf[255];
  60. std::fill(begin(buf), end(buf), 0);
  61. sprintf(buf, "%.4g", pair.first);
  62. bool match = strcmp(buf, pair.second) == 0;
  63. cout << "match " << match << " in " << pair.first << " out " << buf << " expected " << pair.second << endl;
  64. }
  65.  
  66. return 0;
  67. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
printf("%.3f");

match 0 in 1.23457e+06 out 1234567.000 expected 12e5
match 0 in 12345.6 out 12345.600 expected 12346
match 0 in 1234.56 out 1234.560 expected 1235
match 0 in 123.456 out 123.456 expected 123.5
match 0 in 12.3456 out 12.346 expected 12.35
match 1 in 1.23456 out 1.235 expected 1.235
match 0 in 1.2345 out 1.234 expected 1.235
match 1 in 1.234 out 1.234 expected 1.234
match 0 in 1.23 out 1.230 expected 1.23
match 0 in 1.2 out 1.200 expected 1.2
match 0 in 1 out 1.000 expected 1
match 1 in 0.11111 out 0.111 expected 0.111
match 1 in 0.01111 out 0.011 expected 0.011
match 1 in 0.00111 out 0.001 expected 0.001
match 0 in 0.00011 out 0.000 expected 11e-4
match 0 in 1e-05 out 0.000 expected 1e-5
match 1 in 0.11111 out 0.111 expected 0.111
match 1 in 0.01111 out 0.011 expected 0.011
match 1 in 0.00111 out 0.001 expected 0.001
match 0 in 0.00011 out 0.000 expected 11e-4
match 0 in 1e-05 out 0.000 expected 1e-5

printf("%.3g");

match 0 in 1.23457e+06 out 1.23e+06 expected 12e5
match 0 in 12345.6 out 1.23e+04 expected 12346
match 0 in 1234.56 out 1.23e+03 expected 1235
match 0 in 123.456 out 123 expected 123.5
match 0 in 12.3456 out 12.3 expected 12.35
match 0 in 1.23456 out 1.23 expected 1.235
match 0 in 1.2345 out 1.23 expected 1.235
match 0 in 1.234 out 1.23 expected 1.234
match 1 in 1.23 out 1.23 expected 1.23
match 1 in 1.2 out 1.2 expected 1.2
match 1 in 1 out 1 expected 1
match 1 in 0.11111 out 0.111 expected 0.111
match 0 in 0.01111 out 0.0111 expected 0.011
match 0 in 0.00111 out 0.00111 expected 0.001
match 0 in 0.00011 out 0.00011 expected 11e-4
match 0 in 1e-05 out 1e-05 expected 1e-5
match 1 in 0.11111 out 0.111 expected 0.111
match 0 in 0.01111 out 0.0111 expected 0.011
match 0 in 0.00111 out 0.00111 expected 0.001
match 0 in 0.00011 out 0.00011 expected 11e-4
match 0 in 1e-05 out 1e-05 expected 1e-5

printf("%.4g");

match 0 in 1.23457e+06 out 1.235e+06 expected 12e5
match 0 in 12345.6 out 1.235e+04 expected 12346
match 1 in 1234.56 out 1235 expected 1235
match 1 in 123.456 out 123.5 expected 123.5
match 1 in 12.3456 out 12.35 expected 12.35
match 1 in 1.23456 out 1.235 expected 1.235
match 0 in 1.2345 out 1.234 expected 1.235
match 1 in 1.234 out 1.234 expected 1.234
match 1 in 1.23 out 1.23 expected 1.23
match 1 in 1.2 out 1.2 expected 1.2
match 1 in 1 out 1 expected 1
match 0 in 0.11111 out 0.1111 expected 0.111
match 0 in 0.01111 out 0.01111 expected 0.011
match 0 in 0.00111 out 0.00111 expected 0.001
match 0 in 0.00011 out 0.00011 expected 11e-4
match 0 in 1e-05 out 1e-05 expected 1e-5
match 0 in 0.11111 out 0.1111 expected 0.111
match 0 in 0.01111 out 0.01111 expected 0.011
match 0 in 0.00111 out 0.00111 expected 0.001
match 0 in 0.00011 out 0.00011 expected 11e-4
match 0 in 1e-05 out 1e-05 expected 1e-5