cerevra / int

38 stars 6 forks source link

Some operation errors #17

Open dix75 opened 7 years ago

dix75 commented 7 years ago

I've found some errors. And I wrote a new test function

    static inline void testBigNumbers() {
        uint128_t a1 = std::numeric_limits<uint128_t>::max() * 1;
        assert2(a1, std::numeric_limits<uint128_t>::max());  // Ok

        a1 = std::numeric_limits<uint128_t>::max() * 1.;
        assert2(a1, std::numeric_limits<uint128_t>::max());
        //a1 = 340282366920938463454151235394913435648                                                           
        // have to be 340282366920938463463374607431768211455

        a1 = std::numeric_limits<uint128_t>::max() / 2;
        uint128_t a2 = std::numeric_limits<uint128_t>::max() / 2.;
        assert2(a1, a2);
        // a1 = 0
        // a2 = 340282366920938463454151235394913435648 
        // have to be 1.7014118346046923e+38

        uint256_t a3 = std::numeric_limits<uint128_t>::max();
        uint128_t a4 = a3 * 1.1;
        assert2(a4, 37431060361303234_uint256);
        // a4 = 340282366920938463454151235394913435648  
        // must be 3.7431060361303234e+38

        uint256_t c1 = 100000000000000000000_uint256;
        uint256_t c2 = 100000000000000000000_uint256;
        uint256_t c3 = c1 * c2;
        assert2(c3, 10000000000000000000000000000000000000000_uint256);
    }
#define assert2(a, b)                                                                             \
    {                                                                                             \
        if ((a) != (b)) {                                                                         \
            std::cerr << "first = " << a << std::endl;                                            \
            std::cerr << "second = " << b << std::endl;                                           \
            throw std::runtime_error(#a ", line:" + std::to_string(__LINE__));                    \
        }                                                                                         \
    }