test_math.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Host test for the x87 math functions. */
  2. #include <stdio.h>
  3. #include <math.h>
  4. static int failures;
  5. static void ck(const char *name, double got, double want)
  6. {
  7. double d = got - want;
  8. if (d < 0)
  9. d = -d;
  10. if (!(d < 1e-9)) {
  11. printf("math mismatch %s: got %.17g want %.17g\n",
  12. name, got, want);
  13. failures++;
  14. }
  15. }
  16. int main(void)
  17. {
  18. int e;
  19. double ip;
  20. ck("sqrt", sqrt(9.0), 3.0);
  21. ck("fabs", fabs(-2.5), 2.5);
  22. ck("floor", floor(-1.2), -2.0);
  23. ck("ceil", ceil(-1.2), -1.0);
  24. ck("trunc", trunc(-1.2), -1.0);
  25. ck("round", round(2.5), 3.0);
  26. ck("fmod", fmod(7.5, 2.0), 1.5);
  27. ck("pow", pow(2.0, 10.0), 1024.0);
  28. ck("powneg", pow(-2.0, 3.0), -8.0);
  29. ck("powfrac", pow(9.0, 0.5), 3.0);
  30. ck("exp0", exp(0.0), 1.0);
  31. ck("exp1", exp(1.0), 2.718281828459045);
  32. ck("log1", log(1.0), 0.0);
  33. ck("logE", log(2.718281828459045), 1.0);
  34. ck("log10", log10(100.0), 2.0);
  35. ck("sin0", sin(0.0), 0.0);
  36. ck("cos0", cos(0.0), 1.0);
  37. ck("tan0", tan(0.0), 0.0);
  38. ck("atan1", atan(1.0), 0.7853981633974483);
  39. ck("atan2", atan2(1.0, 1.0), 0.7853981633974483);
  40. ck("asin", asin(0.5), 0.5235987755982989);
  41. ck("acos", acos(0.5), 1.0471975511965977);
  42. ck("ldexp", ldexp(1.5, 3), 12.0);
  43. ck("frexp", frexp(12.0, &e), 0.75);
  44. if (e != 4) {
  45. printf("math mismatch frexp exponent %d\n", e);
  46. failures++;
  47. }
  48. ck("modf", modf(2.75, &ip), 0.75);
  49. ck("modfip", ip, 2.0);
  50. ck("fmin", fmin(2.0, 3.0), 2.0);
  51. ck("fmax", fmax(2.0, 3.0), 3.0);
  52. ck("copysign", copysign(2.0, -1.0), -2.0);
  53. if (failures)
  54. printf("math test: %d failure(s)\n", failures);
  55. else
  56. printf("math test: OK\n");
  57. return failures != 0;
  58. }