diff --git a/src/bignum/bignum.cc b/src/bignum/bignum.cc new file mode 100644 index 0000000..6a1f62c --- /dev/null +++ b/src/bignum/bignum.cc @@ -0,0 +1,37 @@ +#include "bignum.hh" + +#include + +#include + +namespace abacus::bignum { + +BigNum::BigNum(std::int64_t number) { + if (number == 0) { + return; + } + + if (number < 0) { + sign_ = -1; + } else { + sign_ = 1; + } + + auto abs = static_cast(std::abs(number)); + do { + digits_.push_back(abs % 10); + abs /= 10; + } while (abs); +} + +void BigNum::canonicalize() { + auto const it = std::find_if(digits_.rbegin(), digits_.rend(), + [](auto v) { return v != 0; }); + digits_.erase(it.base(), digits_.end()); + + if (digits_.size() == 0) { + sign_ = 0; + } +} + +} // namespace abacus::bignum diff --git a/src/bignum/bignum.hh b/src/bignum/bignum.hh new file mode 100644 index 0000000..54db82b --- /dev/null +++ b/src/bignum/bignum.hh @@ -0,0 +1,20 @@ +#pragma once + +#include + +#include + +namespace abacus::bignum { + +class BigNum { +public: + explicit BigNum(std::int64_t number); + +private: + void canonicalize(); + + std::vector digits_{}; + int sign_ = 0; +}; + +} // namespace abacus::bignum diff --git a/src/bignum/meson.build b/src/bignum/meson.build new file mode 100644 index 0000000..7346d21 --- /dev/null +++ b/src/bignum/meson.build @@ -0,0 +1,16 @@ +bignum_sources = files( + 'bignum.cc', + 'bignum.hh', +) + +bignum_inc = include_directories('.') + +bignum_lib = library( + 'bignum', + sources: bignum_sources, +) + +bignum = declare_dependency( + link_with: bignum_lib, + include_directories: bignum_inc, +) diff --git a/src/meson.build b/src/meson.build index da90f6c..50721d2 100644 --- a/src/meson.build +++ b/src/meson.build @@ -2,6 +2,7 @@ abacus_sources = files( 'abacus.cc', ) +subdir('bignum') subdir('parse') abacus = executable(