fork download
  1. #include <iso646.h> // not, and
  2. #include <math.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5.  
  6. typedef bool (*func_t)(double);
  7.  
  8. bool is_negative_zero(double f) {
  9. return signbit(f) and f >= 0;
  10. }
  11.  
  12. bool is_negative_zero_2(double f) {
  13. return f == 0 and signbit(f);
  14. }
  15.  
  16. bool is_negative_zero_x(double f) {
  17. return f == 0 and 1/f < 0;
  18. }
  19.  
  20. int test(func_t f, char* msg) {
  21. int nerrors = 0;
  22. double a[] = {0., 1., -1., 0./0., 1/0., -1/0., 0./(1/0.), 1./(1/0.), };
  23. for (int i = 0; i < sizeof(a)/sizeof(*a); ++i) {
  24. if (f(a[i])) {
  25. ++nerrors;
  26. fprintf(stderr, "error: %s f=%lf\n", msg, a[i]);
  27. }
  28. }
  29. if (not (f(-0.) and f(-1./(1/0.)))) {
  30. ++nerrors;
  31. fprintf(stderr, "error: %s f=%s\n", msg, "-0.");
  32. }
  33. return nerrors;
  34. }
  35. int main() {
  36. int nerrors = 0;
  37. nerrors += test(is_negative_zero, "signbit");
  38. nerrors += test(is_negative_zero_2, "x == 0 and signbit(x)");
  39. nerrors += test(is_negative_zero_x, "1/x");
  40. return (bool)nerrors;
  41. }
Success #stdin #stdout 0.01s 1716KB
stdin
Standard input is empty
stdout

Standard output is empty