fork(6) download
  1. #include <stdio.h>
  2.  
  3. float Q_rsqrt( float number )
  4. {
  5. long i;
  6. float x2, y;
  7. const float threehalfs = 1.5F;
  8.  
  9. x2 = number * 0.5F;
  10. y = number;
  11. i = * ( long * ) &y; // evil floating point bit level hacking
  12. i = 0x5f3759df - ( i >> 1 ); // what the fuck?
  13. y = * ( float * ) &i;
  14. y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
  15. y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
  16.  
  17. return y;
  18. }
  19.  
  20. int main(void) {
  21. printf("%f\n", 1.0f/Q_rsqrt(144.0f)); // 12
  22. printf("%f\n", 1.0f/Q_rsqrt(289.0f)); // 17
  23. printf("%f\n", 1.0f/Q_rsqrt(25472209.0f)); // 5047
  24. printf("%f\n", 1.0f/Q_rsqrt(95775582.25f)); // 9786,5
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
12.000025
17.000000
5047.015625
9786.516602