Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // floor(x)
- // Return x rounded toward -inf to integral value
- // Method: Bit twiddling.
- // Exception: Inexact flag raised if x not equal to floor(x)
- #include <iostream>
- #include <bitset>
- using namespace std;
- double florr(double x)
- {
- int i0,i1,j0;
- unsigned int i;
- i0 = *(1 + (int*)&x);
- i1 = *(int*)&x;
- j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
- i = (0x000fffff) >> j0;
- i0 += (0x00100000) >> j0;
- i0 &= (~i);
- i1=0;
- *(1 + (int*)&x) = i0;
- *(int*)&x = i1;
- return x;
- }
- int main() {
- double x = -4.5; // 11000000 00010010 00000000 00000000 00000000 00000000 00000000 00000000
- double y = florr(x);
- cout << "the floor of " << x << " is " << y << endl; // Prints -5
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement