Skip to content

Commit

Permalink
Update ratio::(numerator|denominator) to be return 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 Aug 15, 2022
1 parent ad67f24 commit 265005f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 49 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ sudo rm -rf /usr/local/share/meevax

| Target Name | Description
|:-------------------|:--
| `all` (default) | Build shared-library `libmeevax.0.4.207.so` and executable `meevax`.
| `all` (default) | Build shared-library `libmeevax.0.4.208.so` and executable `meevax`.
| `test` | Test executable `meevax`.
| `package` | Generate debian package `meevax_0.4.207_amd64.deb`.
| `package` | Generate debian package `meevax_0.4.208_amd64.deb`.
| `install` | Copy files into `/usr/local` __(1)__.
| `install.deb` | `all` + `package` + `sudo apt install <meevax>.deb`
| `safe-install.deb` | `all` + `test` + `package` + `sudo apt install <meevax>.deb`
Expand All @@ -122,7 +122,7 @@ __(1)__ Meevax installed by `make install` cannot be uninstalled by the system's
## Usage

```
Meevax Lisp System, version 0.4.207
Meevax Lisp System, version 0.4.208
Usage: meevax [OPTION...] [FILE...]
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.207
0.4.208
18 changes: 8 additions & 10 deletions include/meevax/kernel/exact_integer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ inline namespace kernel
{
mpz_t value;

explicit exact_integer() noexcept;

explicit exact_integer(mpz_t const) noexcept;
exact_integer() noexcept;

exact_integer(exact_integer const&) noexcept;

explicit exact_integer(exact_integer &&) noexcept;
exact_integer(exact_integer &&) noexcept;

~exact_integer();

explicit exact_integer(mpz_t const) noexcept;

explicit exact_integer(int);

Expand All @@ -47,18 +49,12 @@ inline namespace kernel

explicit exact_integer(external_representation const&, int = 0);

~exact_integer();

auto operator=(exact_integer const&) -> exact_integer &;

auto operator=(exact_integer &&) noexcept -> exact_integer &;

auto operator=(external_representation const&) -> exact_integer &;

auto swap(exact_integer &) noexcept -> void;

explicit operator bool() const;

operator int() const;

operator signed long() const;
Expand All @@ -68,6 +64,8 @@ inline namespace kernel
explicit operator float() const;

explicit operator double() const;

explicit operator bool() const;
};

auto operator ==(exact_integer const&, int const) -> bool;
Expand Down
6 changes: 3 additions & 3 deletions include/meevax/kernel/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ inline namespace kernel
}
else if constexpr (std::is_same_v<std::decay_t<decltype(z)>, ratio>)
{
if (auto i = exact_integer(mpq_denref(z.value)); i == 1)
if (z.denominator() == 1)
{
return z.numerator();
return make(z.numerator());
}
else
{
Expand Down Expand Up @@ -380,7 +380,7 @@ inline namespace kernel
}
else if constexpr (std::is_same_v<std::decay_t<T>, ratio>)
{
return x.denominator().template as<exact_integer>() == 1;
return x.denominator() == 1;
}
else
{
Expand Down
9 changes: 4 additions & 5 deletions include/meevax/kernel/ratio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#define INCLUDED_MEEVAX_KERNEL_RATIO_HPP

#include <gmp.h>

#include <meevax/kernel/pair.hpp>

namespace meevax
Expand All @@ -35,6 +34,8 @@ inline namespace kernel

ratio(ratio &&);

~ratio();

explicit ratio(exact_integer const&);

explicit ratio(exact_integer const&, exact_integer const&);
Expand All @@ -43,11 +44,9 @@ inline namespace kernel

explicit ratio(external_representation const&, int = 10);

~ratio();

auto denominator() const -> value_type;
auto denominator() const -> exact_integer;

auto numerator() const -> value_type;
auto numerator() const -> exact_integer;

explicit operator double() const;
};
Expand Down
29 changes: 12 additions & 17 deletions src/kernel/exact_integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ inline namespace kernel
mpz_init(value);
}

exact_integer::exact_integer(mpz_t const z) noexcept
{
mpz_init_set(value, z);
}

exact_integer::exact_integer(exact_integer const& other) noexcept
{
mpz_init_set(value, other.value);
Expand All @@ -47,6 +42,16 @@ inline namespace kernel
mpz_swap(value, other.value);
}

exact_integer::~exact_integer()
{
mpz_clear(value);
}

exact_integer::exact_integer(mpz_t const z) noexcept
{
mpz_init_set(value, z);
}

exact_integer::exact_integer(int rhs)
: exact_integer(static_cast<signed long>(rhs))
{}
Expand Down Expand Up @@ -75,20 +80,15 @@ inline namespace kernel
}
}

exact_integer::~exact_integer()
{
mpz_clear(value);
}

auto exact_integer::operator=(exact_integer const& rhs) -> exact_integer &
{
mpz_set(value, rhs.value);
return *this;
}

auto exact_integer::operator=(exact_integer && rhs) noexcept -> exact_integer &
auto exact_integer::operator=(exact_integer && other) noexcept -> exact_integer &
{
swap(rhs);
mpz_swap(value, other.value);
return *this;
}

Expand All @@ -104,11 +104,6 @@ inline namespace kernel
}
}

auto exact_integer::swap(exact_integer & rhs) noexcept -> void
{
std::swap(*value, *rhs.value);
}

exact_integer::operator bool() const
{
return (*value)._mp_size;
Expand Down
4 changes: 2 additions & 2 deletions src/kernel/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,12 +571,12 @@ inline namespace kernel

library.define<procedure>("ratio-numerator", [](let const& xs)
{
return car(xs).as<ratio>().numerator();
return make(car(xs).as<ratio>().numerator());
});

library.define<procedure>("ratio-denominator", [](let const& xs)
{
return car(xs).as<ratio>().denominator();
return make(car(xs).as<ratio>().denominator());
});

library.define<procedure>("floor", [](let const& xs)
Expand Down
16 changes: 8 additions & 8 deletions src/kernel/ratio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ inline namespace kernel

ratio::ratio(external_representation const& token, int radix)
{
std::regex static const pattern { "([+-]?[0-9a-f]+)/([0-9a-f]+)" };
// std::regex static const pattern { "([+-]?[0-9a-f]+)/([0-9a-f]+)" };

if (mpq_init(value); not std::regex_match(token, pattern) or mpq_set_str(value, token.c_str(), radix))
if (mpq_init(value); mpq_set_str(value, token.c_str(), radix))
{
mpq_clear(value);
throw error();
}
else // TEMPORARY!!!
else
{
mpq_canonicalize(value);
}
Expand All @@ -81,19 +81,19 @@ inline namespace kernel
mpq_clear(value);
}

auto ratio::denominator() const -> value_type
auto ratio::denominator() const -> exact_integer
{
return make<exact_integer>(mpq_denref(value));
return exact_integer(mpq_denref(value));
}

auto ratio::numerator() const -> value_type
auto ratio::numerator() const -> exact_integer
{
return make<exact_integer>(mpq_numref(value));
return exact_integer(mpq_numref(value));
}

ratio::operator double() const
{
return static_cast<double>(numerator().as<exact_integer>()) / static_cast<double>(denominator().as<exact_integer>());
return mpq_get_d(value);
}

auto operator <<(std::ostream & os, ratio const& datum) -> std::ostream &
Expand Down

0 comments on commit 265005f

Please sign in to comment.