fork(3) download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <math.h>
  4. /* use FMA as correction*/
  5. float foo1(float a, float k, float *res)
  6. {
  7. float ret=(a-k)/(a+k);
  8. *res = fmaf(-ret,a+k,a-k)/(a+k);
  9. return ret;
  10. }
  11. /* do double precision quote, divide the double precision quote two two floats*/
  12. float foo2(float a, float k, float *res)
  13. {
  14. double rD=(double)(a-k)/(a+k);
  15. float ret =rD;
  16. *res = rD - ret;
  17. return ret;
  18. }
  19. /* naive method*/
  20. float fooFloat(float a, float k)
  21. {
  22. return (a-k)/(a+k);
  23. }
  24. /*reference*/
  25. double fooDouble(float a, float k)
  26. {
  27. return ((double)a-(double)k)/((double)a+(double)k);
  28. }
  29. int main(void) {
  30. // your code goes here
  31. float res1, res2;
  32. float a=1024*96/97.0f;
  33. float b =1024.0f;
  34. float ret1 = foo1(a, b, &res1);
  35. float ret2 = foo2(a, b, &res2);
  36.  
  37. printf("diff_float = %.40f\n"
  38. "diff_foo1 = %.40f\n"
  39. "diff_foo2 = %0.40f\n\n",
  40. fooDouble(a,b)-fooFloat(a, b),
  41. fooDouble(a,b)-((double)(ret1)+(double)(res1)),
  42. fooDouble(a,b)-((double)(ret2)+(double)(res2)));
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
diff_float = 0.0000000000249401124752246516891318606213
diff_foo1  = -0.0000000001552162773310605814458540407941
diff_foo2  = 0.0000000000000000000000000000000000000000