fork download
  1. #include <stdio.h>
  2. #include <float.h>
  3. #include <math.h>
  4.  
  5. static inline int gdigits(double d, int precision)
  6. {
  7. return precision - (int) log10(d) - 1;
  8. }
  9.  
  10. static void test_scanf(const char *s)
  11. {
  12. double x1, x2;
  13.  
  14. if ((sscanf(s, "%lf %lf", &x1, &x2)) == 2)
  15. {
  16. printf("Input: %s\n", s);
  17. printf("Exponential form: %e, %e\n", x1, x2);
  18. printf("Digits for default precision (6): %d %d\n", gdigits(x1, 6), gdigits(x2, 6));
  19. printf("With default precision (6): %g %g\n", x1, x2);
  20. printf("Digits for precision (7): %d %d\n", gdigits(x1, 7), gdigits(x2, 7));
  21. printf("With custom precision (7): %#.7g %#.7g\n", x1, x2);
  22. printf("With even more precision (32): %.32g %.32g\n", x1, x2);
  23. puts("");
  24. }
  25. else
  26. {
  27. printf("%s: invalid input.\n", s);
  28. }
  29. }
  30.  
  31. int main(void)
  32. {
  33. test_scanf("1495.952 934.023");
  34. test_scanf("6369.015 66159.129");
  35. }
  36.  
Success #stdin #stdout 0s 10320KB
stdin
Standard input is empty
stdout
Input: 1495.952 934.023
Exponential form: 1.495952e+03, 9.340230e+02
Digits for default precision (6): 2 3
With default precision (6): 1495.95 934.023
Digits for precision (7): 3 4
With custom precision (7): 1495.952 934.0230
With even more precision (32): 1495.9519999999999981810105964541 934.02300000000002455635694786906

Input: 6369.015 66159.129
Exponential form: 6.369015e+03, 6.615913e+04
Digits for default precision (6): 2 1
With default precision (6): 6369.02 66159.1
Digits for precision (7): 3 2
With custom precision (7): 6369.015 66159.13
With even more precision (32): 6369.0150000000003274180926382542 66159.129000000000814907252788544