[ create a new paste ] login | about

Link: http://codepad.org/7gNXqFHO    [ raw code | output | fork ]

pyrtsa_ - C++, pasted on Dec 16:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <boost/static_assert.hpp>
template <typename To, typename From>
inline To bitwise_cast(From const & from) {
    BOOST_STATIC_ASSERT(sizeof(To) == sizeof(From));
    union { To to; From from; } u;
    u.from = from;
    return u.to;
}

#include <iostream>
#include <limits>
int main() {
    std::cout << bitwise_cast<int>(123.456f) << std::endl;
    std::cout << bitwise_cast<int>(0.0f) << std::endl;
    std::cout << bitwise_cast<int>(1.0f) << std::endl;
    std::cout << bitwise_cast<int>(std::numeric_limits<float>::infinity()) << std::endl;
    std::cout << bitwise_cast<int>(std::numeric_limits<float>::quiet_NaN()) << std::endl;
}


Output:
1
2
3
4
5
1123477881
0
1065353216
2139095040
2143289344


Create a new paste based on this one


Comments: