diff --git a/include/9float.hpp b/include/9float.hpp index 8028f7e..13b7fed 100644 --- a/include/9float.hpp +++ b/include/9float.hpp @@ -1,147 +1,156 @@ -#pragma once -#include -#include - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wnarrowing" -namespace ninefloat{ - -struct private_t{}; - -private_t priv; - -template -class Q{ - integer_type backend; - static constexpr int degen=fractionals; - - - constexpr Q (const integer_type value, private_t) - : backend{value} - {{std::cout< - constexpr Q (const srcT value) - : backend{(integer_type)(value*(1<::degen; - backend/=oth.backend; - return this; - } - - Q constexpr operator*=(const Q& oth) - { - if constexpr(Q::degen%2==0) - { - backend=(backend>>(Q::degen/2))*(oth.backend>>(Q::degen/2)); - }else{ - backend=(backend>>(Q::degen>>1))*(oth.backend>>((Q::degen>>1)+1)); - } - return this; - } - - Q constexpr operator+(const Q& oth) - { - return Q{backend+oth.backend, priv}; - } - - Q constexpr operator-(const Q& oth) - { - return Q{backend-oth.backend, priv}; - } - - Q constexpr operator/(const Q& oth) - { - return Q{(1<::degen)*backend/oth.backend, priv}; - } - - Q constexpr operator*(const Q& oth) - { - if constexpr(Q::degen%2==0) - { - return Q{(backend>>(Q::degen/2))*(oth.backend>>(Q::degen/2)),priv}; - }else{ - return Q{(backend>>(Q::degen>>1))*(oth.backend>>((Q::degen>>1)+1)),priv}; - } - } - - bool constexpr operator==(const Q& oth) - { - return backend == oth.backend; - } - - bool constexpr operator!=(const Q& oth) - { - return backend != oth.backend; - } - - bool constexpr operator>=(const Q& oth) - { - return backend >= oth.backend; - } - - bool constexpr operator<=(const Q& oth) - { - return backend <= oth.backend; - } - - bool constexpr operator>(const Q& oth) - { - return backend > oth.backend; - } - - bool constexpr operator<(const Q& oth) - { - return backend < oth.backend; - } - - constexpr operator float() - { - float n=backend; - n/=(1<::degen); - return n; - } - - constexpr operator double() - { - double n=backend; - n/=(1<::degen); - return n; - } -}; -#pragma GCC diagnostic pop - +#pragma once +#include +#include + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wnarrowing" +namespace ninefloat{ + +struct private_t{}; + +private_t priv; + +template +class Q{ + integer_type backend; + static constexpr int degen=fractionals; + + + constexpr Q (const integer_type value, private_t) + : backend{value} + {} + +public: + constexpr Q(const Q& value) + : backend{value.backend} + {} + + template + constexpr Q (const srcT value) + : backend{(integer_type)(value*(1<& operator+=(const Q& oth) + { + backend+=oth.backend; + return *this; + } + + constexpr Q& operator-=(const Q& oth) + { + backend-=oth.backend; + return *this; + } + + constexpr Q& operator/=(const Q& oth) + { + backend*=1<::degen; + backend/=oth.backend; + return *this; + } + + constexpr Q& operator*=(const Q& oth) + { + if constexpr(Q::degen%2==0) + { + backend=(backend>>(Q::degen/2))*(oth.backend>>(Q::degen/2)); + }else{ + backend=(backend>>(Q::degen>>1))*(oth.backend>>((Q::degen>>1)+1)); + } + return *this; + } + + constexpr Q operator+(const Q& oth) + { + return Q{backend+oth.backend, priv}; + } + + constexpr Q operator-(const Q& oth) + { + return Q{backend-oth.backend, priv}; + } + + constexpr Q operator/(const Q& oth) + { + return Q{(1<::degen)*backend/oth.backend, priv}; + } + + constexpr Q operator*(const Q& oth) + { + if constexpr(Q::degen%2==0) + { + return Q{(backend>>(Q::degen/2))*(oth.backend>>(Q::degen/2)),priv}; + }else{ + return Q{(backend>>(Q::degen>>1))*(oth.backend>>((Q::degen>>1)+1)),priv}; + } + } + + constexpr bool operator==(const Q& oth) + { + return backend == oth.backend; + } + + constexpr bool operator!=(const Q& oth) + { + return backend != oth.backend; + } + + constexpr bool operator>=(const Q& oth) + { + return backend >= oth.backend; + } + + constexpr bool operator<=(const Q& oth) + { + return backend <= oth.backend; + } + + constexpr bool operator>(const Q& oth) + { + return backend > oth.backend; + } + + constexpr bool operator<(const Q& oth) + { + return backend < oth.backend; + } + + constexpr operator float() + { + float n=backend; + n/=(1<::degen); + return n; + } + + constexpr operator double() + { + double n=backend; + n/=(1<::degen); + return n; + } +}; +#pragma GCC diagnostic pop + } \ No newline at end of file diff --git a/src/tests.cpp b/src/tests.cpp index 66a6fe3..7d7ece5 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -1,141 +1,141 @@ -#include "catch.hpp" -#include -#include "9float.hpp" - -using namespace ninefloat; - -TEST_CASE("Addition") -{ - SECTION("with positive numbers") - { - int i = 1; - float j = 0.5; - Q target = 1.5; - Q qi = i; - Q qj = j; - Q qres = qi + qj; - REQUIRE(qres==target); - } - SECTION("with negative numbers") - { - int i = -1; - float j = -0.5; - Q target = -1.5; - Q qi = i; - Q qj = j; - Q qres = qi + qj; - REQUIRE(qres==target); - } - SECTION("with mixed numbers") - { - int i = 1; - float j = -0.5; - Q target = 0.5; - Q qi = i; - Q qj = j; - Q qres = qi + qj; - REQUIRE(qres==target); - } -} - -TEST_CASE("Substraction") -{ - SECTION("with positive numbers") - { - int i = 1; - float j = 0.5; - Q target = 0.5; - Q qi = i; - Q qj = j; - Q qres = qi - qj; - REQUIRE(qres==target); - } - SECTION("with negative numbers") - { - int i = -1; - float j = -0.5; - Q target = -0.5; - Q qi = i; - Q qj = j; - Q qres = qi - qj; - REQUIRE(qres==target); - } - SECTION("with mixed numbers") - { - int i = 1; - float j = -0.5; - Q target = 1.5; - Q qi = i; - Q qj = j; - Q qres = qi - qj; - REQUIRE(qres==target); - } -} - -TEST_CASE("Multiplication") -{ - SECTION("with positive numbers") - { - int i = 2; - float j = 2.5; - Q target = 5; - Q qi = i; - Q qj = j; - Q qres = qi * qj; - REQUIRE(qres==target); - } - SECTION("with negative numbers") - { - int i = -2; - float j = -2.5; - Q target = 5; - Q qi = i; - Q qj = j; - Q qres = qi * qj; - REQUIRE(qres==target); - } - SECTION("with mixed numbers") - { - int i = -2; - float j = 2.5; - Q target = -5; - Q qi = i; - Q qj = j; - Q qres = qi * qj; - REQUIRE(qres==target); - } -} - -TEST_CASE("Division") -{ - SECTION("with positive numbers") - { - int i = 10; - float j = 2; - Q target = 5; - Q qi = i; - Q qj = j; - Q qres = qi / qj; - REQUIRE(qres==target); - } - SECTION("with negative numbers") - { - int i = -10; - float j = -5; - Q target = 2; - Q qi = i; - Q qj = j; - Q qres = qi / qj; - REQUIRE(qres==target); - } - SECTION("with mixed numbers") - { - int i = -18; - float j = 3; - Q target = -6; - Q qi = i; - Q qj = j; - Q qres = qi / qj; - REQUIRE(qres==target); - } +#include "catch.hpp" +#include +#include "9float.hpp" + +using namespace ninefloat; + +TEST_CASE("Addition") +{ + SECTION("with positive numbers") + { + int i = 1; + float j = 0.5; + Q target = 1.5; + Q qi = i; + Q qj = j; + Q qres = qi + qj; + REQUIRE(qres==target); + } + SECTION("with negative numbers") + { + int i = -1; + float j = -0.5; + Q target = -1.5; + Q qi = i; + Q qj = j; + Q qres = qi + qj; + REQUIRE(qres==target); + } + SECTION("with mixed numbers") + { + int i = 1; + float j = -0.5; + Q target = 0.5; + Q qi = i; + Q qj = j; + Q qres = qi + qj; + REQUIRE(qres==target); + } +} + +TEST_CASE("Substraction") +{ + SECTION("with positive numbers") + { + int i = 1; + float j = 0.5; + Q target = 0.5; + Q qi = i; + Q qj = j; + Q qres = qi - qj; + REQUIRE(qres==target); + } + SECTION("with negative numbers") + { + int i = -1; + float j = -0.5; + Q target = -0.5; + Q qi = i; + Q qj = j; + Q qres = qi - qj; + REQUIRE(qres==target); + } + SECTION("with mixed numbers") + { + int i = 1; + float j = -0.5; + Q target = 1.5; + Q qi = i; + Q qj = j; + Q qres = qi - qj; + REQUIRE(qres==target); + } +} + +TEST_CASE("Multiplication") +{ + SECTION("with positive numbers") + { + int i = 2; + float j = 2.5; + Q target = 5; + Q qi = i; + Q qj = j; + Q qres = qi * qj; + REQUIRE(qres==target); + } + SECTION("with negative numbers") + { + int i = -2; + float j = -2.5; + Q target = 5; + Q qi = i; + Q qj = j; + Q qres = qi * qj; + REQUIRE(qres==target); + } + SECTION("with mixed numbers") + { + int i = -2; + float j = 2.5; + Q target = -5; + Q qi = i; + Q qj = j; + Q qres = qi * qj; + REQUIRE(qres==target); + } +} + +TEST_CASE("Division") +{ + SECTION("with positive numbers") + { + int i = 10; + float j = 2; + Q target = 5; + Q qi = i; + Q qj = j; + Q qres = qi / qj; + REQUIRE(qres==target); + } + SECTION("with negative numbers") + { + int i = -10; + float j = -5; + Q target = 2; + Q qi = i; + Q qj = j; + Q qres = qi / qj; + REQUIRE(qres==target); + } + SECTION("with mixed numbers") + { + int i = -18; + float j = 3; + Q target = -6; + Q qi = i; + Q qj = j; + Q qres = qi / qj; + REQUIRE(qres==target); + } } \ No newline at end of file