Skip to content

Commit

Permalink
Update exact_integer_sqrt to be member function of exact_integer
Browse files Browse the repository at this point in the history
Signed-off-by: yamacir-kit <[email protected]>
  • Loading branch information
yamacir-kit committed Sep 30, 2023
1 parent 0de422e commit 56c3135
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Subset of R7RS-small.
cmake -B build -DCMAKE_BUILD_TYPE=Release
cd build
make package
sudo apt install build/meevax_0.4.821_amd64.deb
sudo apt install build/meevax_0.4.822_amd64.deb
```

or
Expand Down Expand Up @@ -106,9 +106,9 @@ sudo rm -rf /usr/local/share/meevax

| Target Name | Description
|-------------|-------------
| `all` | Build shared-library `libmeevax.0.4.821.so` and executable `meevax`
| `all` | Build shared-library `libmeevax.0.4.822.so` and executable `meevax`
| `test` | Test executable `meevax`
| `package` | Generate debian package `meevax_0.4.821_amd64.deb`
| `package` | Generate debian package `meevax_0.4.822_amd64.deb`
| `install` | Copy files into `/usr/local` directly

## Usage
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.821
0.4.822
4 changes: 2 additions & 2 deletions include/meevax/kernel/exact_integer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ inline namespace kernel
explicit operator float() const;

explicit operator double() const;

auto square_root() const -> std::tuple<exact_integer, exact_integer>;
};

auto operator ==(exact_integer const&, int const) -> bool;
Expand Down Expand Up @@ -126,8 +128,6 @@ inline namespace kernel
}
};

auto exact_integer_sqrt(exact_integer const&) -> std::tuple<exact_integer, exact_integer>;

let extern const e0, e1; // Frequently used exact-integer values.
} // namespace kernel
} // namespace meevax
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ inline namespace kernel

library.define<function>("exact-integer-square-root", [](let const& xs)
{
auto&& [s, r] = exact_integer_sqrt(xs[0].as<exact_integer>());
auto&& [s, r] = xs[0].as<exact_integer>().square_root();

return cons(make(std::forward<decltype(s)>(s)),
make(std::forward<decltype(r)>(r)));
Expand Down
14 changes: 7 additions & 7 deletions src/kernel/exact_integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ inline namespace kernel
return mpz_get_d(value);
}

auto exact_integer::square_root() const -> std::tuple<exact_integer, exact_integer>
{
exact_integer s, r;
mpz_rootrem(s.value, r.value, value, 2);
return std::make_tuple(s, r);
}

auto operator ==(exact_integer const& a, int const b) -> bool { return a == static_cast<signed long>(b); }
auto operator !=(exact_integer const& a, int const b) -> bool { return a != static_cast<signed long>(b); }
auto operator < (exact_integer const& a, int const b) -> bool { return a < static_cast<signed long>(b); }
Expand All @@ -211,12 +218,5 @@ inline namespace kernel
{
return os << cyan(std::unique_ptr<char, gmp_free>(mpz_get_str(nullptr, 10, datum.value)).get());
}

auto exact_integer_sqrt(exact_integer const& x) -> std::tuple<exact_integer, exact_integer>
{
exact_integer s, r;
mpz_rootrem(s.value, r.value, x.value, 2);
return std::make_tuple(s, r);
}
} // namespace kernel
} // namespace meevax
2 changes: 1 addition & 1 deletion src/kernel/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ inline namespace number
{
if constexpr (std::is_same_v<T, exact_integer>)
{
auto const [s, r] = exact_integer_sqrt(x);
auto const [s, r] = x.square_root();

return r == 0 ? make(s) : make(std::sqrt(inexact_cast(x)));
}
Expand Down

0 comments on commit 56c3135

Please sign in to comment.