Skip to content

Commit

Permalink
Add floor and ceil RealFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Oct 25, 2023
1 parent f608319 commit e9ef065
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/fintamath/numbers/RealFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace fintamath {

Integer floor(const Real &rhs);

Integer ceil(const Real &rhs);

Real abs(const Real &rhs);

Real sqrt(const Real &rhs);
Expand Down
11 changes: 11 additions & 0 deletions src/fintamath/numbers/RealFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@
#include "fintamath/exceptions/UndefinedException.hpp"

using boost::multiprecision::cpp_dec_float_100;
using boost::multiprecision::cpp_int;

namespace fintamath {

Integer floor(const Real &rhs) {
cpp_dec_float_100 res = boost::multiprecision::floor(rhs.getBackend());
return res.convert_to<cpp_int>();
}

Integer ceil(const Real &rhs) {
cpp_dec_float_100 res = boost::multiprecision::ceil(rhs.getBackend());
return res.convert_to<cpp_int>();
}

Real abs(const Real &rhs) {
return rhs < 0 ? -rhs : rhs;
}
Expand Down
44 changes: 44 additions & 0 deletions tests/src/numbers/RealFunctionsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,50 @@

using namespace fintamath;

TEST(RealFunctionsTests, floorTest) {
EXPECT_EQ(floor(Real("-1000.8")).toString(), "-1001");
EXPECT_EQ(floor(Real("-1000.1")).toString(), "-1001");
EXPECT_EQ(floor(Real("-1000")).toString(), "-1000");
EXPECT_EQ(floor(Real("-1.9")).toString(), "-2");
EXPECT_EQ(floor(Real("-1.5")).toString(), "-2");
EXPECT_EQ(floor(Real("-1.3")).toString(), "-2");
EXPECT_EQ(floor(Real("-1")).toString(), "-1");
EXPECT_EQ(floor(Real("-0.9")).toString(), "-1");
EXPECT_EQ(floor(Real("-0.3")).toString(), "-1");
EXPECT_EQ(floor(Real("0")).toString(), "0");
EXPECT_EQ(floor(Real("0.3")).toString(), "0");
EXPECT_EQ(floor(Real("0.9")).toString(), "0");
EXPECT_EQ(floor(Real("1")).toString(), "1");
EXPECT_EQ(floor(Real("1.9")).toString(), "1");
EXPECT_EQ(floor(Real("1.5")).toString(), "1");
EXPECT_EQ(floor(Real("1.3")).toString(), "1");
EXPECT_EQ(floor(Real("1000")).toString(), "1000");
EXPECT_EQ(floor(Real("1000.1")).toString(), "1000");
EXPECT_EQ(floor(Real("1000.8")).toString(), "1000");
}

TEST(RealFunctionsTests, ceilTest) {
EXPECT_EQ(ceil(Real("-1000.8")).toString(), "-1000");
EXPECT_EQ(ceil(Real("-1000.1")).toString(), "-1000");
EXPECT_EQ(ceil(Real("-1000")).toString(), "-1000");
EXPECT_EQ(ceil(Real("-1.9")).toString(), "-1");
EXPECT_EQ(ceil(Real("-1.5")).toString(), "-1");
EXPECT_EQ(ceil(Real("-1.3")).toString(), "-1");
EXPECT_EQ(ceil(Real("-1")).toString(), "-1");
EXPECT_EQ(ceil(Real("-0.9")).toString(), "0");
EXPECT_EQ(ceil(Real("-0.3")).toString(), "0");
EXPECT_EQ(ceil(Real("0")).toString(), "0");
EXPECT_EQ(ceil(Real("0.3")).toString(), "1");
EXPECT_EQ(ceil(Real("0.9")).toString(), "1");
EXPECT_EQ(ceil(Real("1")).toString(), "1");
EXPECT_EQ(ceil(Real("1.9")).toString(), "2");
EXPECT_EQ(ceil(Real("1.5")).toString(), "2");
EXPECT_EQ(ceil(Real("1.3")).toString(), "2");
EXPECT_EQ(ceil(Real("1000")).toString(), "1000");
EXPECT_EQ(ceil(Real("1000.1")).toString(), "1001");
EXPECT_EQ(ceil(Real("1000.8")).toString(), "1001");
}

TEST(RealFunctionsTests, absTest) {
EXPECT_EQ(abs(Real("8465132.321651651")).toString(), "8465132.321651651");
EXPECT_EQ(abs(Real("-98465136846516354684651.351")).toString(), "98465136846516354684651.351");
Expand Down

0 comments on commit e9ef065

Please sign in to comment.