fork(1) download
  1. #include <math.h>
  2. #include <stdio.h>
  3.  
  4. float sin_0(float x)
  5. {
  6. return sin(x);
  7. }
  8.  
  9. float cos_0(float x)
  10. {
  11. return cos(x);
  12. }
  13.  
  14. float cosm1_0(float x)
  15. {
  16. return cos(x)-1.0;
  17. }
  18.  
  19. float sinf_trad(float x)
  20. {
  21. float h = x - 0x1.2660bcp-6f;
  22.  
  23. return 0x1.265caep-6f * cos_0(h) + 0x1.ffead8p-1f * sin_0(h);
  24. }
  25.  
  26. float sinf_new(float x)
  27. {
  28. float h = x - 0x1.2660bcp-6f;
  29.  
  30. return 0x1.265caep-6f + (0x1.265caep-6f * cosm1_0(h) + 0x1.ffead8p-1f * sin_0(h));
  31. }
  32.  
  33. double m_err_trad, s_err_trad;
  34. double m_err_new, s_err_new;
  35.  
  36. int main(){
  37.  
  38. for (float f = 0.01f; f <= 0.025f; f = nextafterf(f, 3.0f))
  39. {
  40. double r = sin(f);
  41. float trad = sinf_trad(f);
  42. float new = sinf_new(f);
  43. double err_trad = fabs(r - trad);
  44. double err_new = fabs(r - new);
  45. s_err_trad += err_trad * err_trad;
  46. s_err_new += err_new * err_new;
  47. err_trad /= r;
  48. err_new /= r;
  49. if (err_trad > m_err_trad) m_err_trad = err_trad;
  50. if (err_new > m_err_new) m_err_new = err_new;
  51. }
  52.  
  53. printf("relative error, traditional: %e, new: %e\n", m_err_trad, m_err_new);
  54. printf("sum of squares of absolute error, traditional: %e, new: %e\n", s_err_trad, s_err_new);
  55. }
  56.  
Time limit exceeded #stdin #stdout 5s 2244KB
stdin
Standard input is empty
stdout
Standard output is empty