fork download
  1. #include <stdio.h>
  2.  
  3. int near_equal(double, double);
  4. int main(void) {
  5. double value1, value2;
  6. while (scanf("%lf, %lf", &value1, &value2) == 2) {
  7. if(near_equal(value1, value2)) {
  8. puts("Tolerance is OKAY\n");
  9. } else {
  10. puts("Tolerance is NOT OKAY\n");
  11. }
  12. }
  13. return 0;
  14. }
  15.  
  16. int near_equal(double value1, double value2) {
  17. double max, min, tolerance, difference;
  18. if (value1 >= value2) {
  19. max = value1;
  20. min = value2;
  21. } else {
  22. max = value2;
  23. min = value1;
  24. }
  25.  
  26. printf("MAX = %f and min = %f\n", max, min);
  27. tolerance = 0.01 * max;
  28. difference = max - min;
  29. printf("The tolerance is %f, and the difference is %f\n", tolerance, difference);
  30. if(difference <= tolerance) {
  31. return 1;
  32. }
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 1724KB
stdin
0.0, 0.00001
0.0, 0.0
1.01567, 1.01568
stdout
MAX = 0.000010 and min = 0.000000
The tolerance is 0.000000, and the difference is 0.000010
Tolerance is NOT OKAY

MAX = 0.000000 and min = 0.000000
The tolerance is 0.000000, and the difference is 0.000000
Tolerance is OKAY

MAX = 1.015680 and min = 1.015670
The tolerance is 0.010157, and the difference is 0.000010
Tolerance is OKAY