fraction.cpp (1120B)
1 /** 2 * KFR (https://www.kfrlib.com) 3 * Copyright (C) 2016-2023 Dan Cazarin 4 * See LICENSE.txt for details 5 */ 6 7 #include <kfr/base/fraction.hpp> 8 #include <kfr/io.hpp> 9 10 namespace kfr 11 { 12 inline namespace CMT_ARCH_NAME 13 { 14 15 TEST(fraction) 16 { 17 fraction f = 1; 18 f = f / 3; 19 CHECK(f == fraction{ 1, 3 }); 20 CHECK(+f == fraction{ 1, 3 }); 21 CHECK(-f == fraction{ -1, 3 }); 22 CHECK(static_cast<double>(f) == 1.0 / 3.0); 23 CHECK(static_cast<float>(f) == 1.0f / 3.0f); 24 CHECK(static_cast<i64>(f) == 0); 25 26 fraction x{ 1, 5 }; 27 CHECK(f + x == fraction{ 8, 15 }); 28 CHECK(f - x == fraction{ 2, 15 }); 29 CHECK(f * x == fraction{ 1, 15 }); 30 CHECK(f / x == fraction{ 5, 3 }); 31 32 CHECK(f > x); 33 CHECK(f >= x); 34 CHECK(!(f < x)); 35 CHECK(!(f <= x)); 36 37 f += fraction(1, 3); 38 CHECK(f == fraction{ 2, 3 }); 39 f -= fraction(1, 6); 40 CHECK(f == fraction{ 1, 2 }); 41 f *= fraction(1, 7); 42 CHECK(f == fraction{ 1, 14 }); 43 f /= fraction(1, 2); 44 CHECK(f == fraction{ 1, 7 }); 45 46 CHECK(fraction{ 100, 200 } == fraction{ 1, 2 }); 47 } 48 } // namespace CMT_ARCH_NAME 49 } // namespace kfr