Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <float.h>
  3.  
  4. int main(int, char**)
  5. {
  6. {
  7. size_t match[2] = {};
  8. for (unsigned i = 0; i != ~0; ++i)
  9. {
  10. float a = *(float*)(&i);
  11. match[(1.f / a) * a == 1.f] += 1;
  12. }
  13.  
  14. fprintf(stderr, "na %zu, ma %zu, ra %f\n", match[0], match[1], (double)match[0] / (match[0] + match[1]));
  15. }
  16.  
  17. {
  18. size_t match[2] = {};
  19. for (float a = -1.f; a < 1.f; a += FLT_EPSILON)
  20. {
  21. match[(1.f / a) * a == 1.f] += 1;
  22. }
  23.  
  24. fprintf(stderr, "na %zu, ma %zu, ra %f\n", match[0], match[1], (double)match[0] / (match[0] + match[1]));
  25. }
  26. }
  27.  
  28. :!g++ -O2 check_float.cpp && ./a.out
  29. na 692211593, ma 3602755702, ra 0.161168
  30. na 2574161, ma 14203055, ra 0.153432
  31.  
  32. :!g++ -O2 -ffast-math check_float.cpp && ./a.out
  33. na 682391430, ma 3612575865, ra 0.158882
  34. na 2574160, ma 14203056, ra 0.153432
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement