Advertisement
Lorfa

sunfloor_stripped

May 29th, 2023
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. // floor(x)
  2. // Return x rounded toward -inf to integral value
  3. // Method: Bit twiddling.
  4. // Exception: Inexact flag raised if x not equal to floor(x)
  5.  
  6. #include <iostream>
  7. #include <bitset>
  8. using namespace std;
  9.  
  10. double florr(double x)
  11.  
  12. {
  13.     int i0,i1,j0;
  14.    
  15.     unsigned int i;
  16.    
  17.     i0 =  *(1 + (int*)&x);
  18.    
  19.     i1 =  *(int*)&x;
  20.    
  21.     j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
  22.                                            
  23.     i = (0x000fffff) >> j0;
  24.                                        
  25.     i0 += (0x00100000) >> j0;
  26.                    
  27.     i0 &= (~i);
  28.                            
  29.     i1=0;
  30.            
  31.     *(1 + (int*)&x) = i0;
  32.    
  33.     *(int*)&x = i1;
  34.        
  35.     return x;
  36.    
  37. }
  38.  
  39. int main() {
  40.        
  41.     double x = -4.5; // 11000000 00010010 00000000 00000000 00000000 00000000 00000000 00000000
  42.    
  43.     double y = florr(x);
  44.    
  45.     cout << "the floor of " << x << " is " << y << endl; // Prints -5
  46.    
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement