Skip to content

Commit

Permalink
Merge pull request #412 from yamacir-kit/ratio
Browse files Browse the repository at this point in the history
Ratio
  • Loading branch information
yamacir-kit authored Aug 16, 2022
2 parents ae4c5f1 + 265005f commit 2aef62b
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 181 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.197.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.197_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.197
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.197
0.4.208
7 changes: 4 additions & 3 deletions basis/r4rs.ss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(define-library (scheme r4rs)
(import (meevax inexact)
(only (meevax number) exact-integer? expt exact inexact ratio?)
(only (meevax number) exact-integer? expt exact inexact ratio? ratio-numerator ratio-denominator)
(only (meevax port) get-ready? standard-input-port standard-output-port)
(only (meevax string) string-copy)
(only (meevax syntax) define-syntax)
Expand Down Expand Up @@ -71,12 +71,13 @@
,body)))))))

(define (numerator x)
(cond ((ratio? x) (car x))
(cond ((ratio? x) (ratio-numerator x))
((exact? x) x)
(else (inexact (numerator (exact x))))))

(define (denominator x)
(cond ((exact? x) (if (ratio? x) (cdr x) 1))
(cond ((ratio? x) (ratio-denominator x))
((exact? x) 1)
((integer? x) 1.0)
(else (inexact (denominator (exact x))))))

Expand Down
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) 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
34 changes: 19 additions & 15 deletions include/meevax/kernel/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,14 @@ inline namespace kernel
}
else if constexpr (std::is_same_v<std::decay_t<decltype(z)>, ratio>)
{
return z.simple();
if (z.denominator() == 1)
{
return make(z.numerator());
}
else
{
return make(std::forward<decltype(z)>(z));
}
}
else
{
Expand Down Expand Up @@ -256,21 +263,18 @@ inline namespace kernel
struct exact
{
template <typename T>
auto operator ()(T const& x) const -> decltype(auto)
{
return ratio(x).simple();
}

auto operator ()(exact_integer const& x) const -> auto const&
{
return x;
}

auto operator ()(ratio const& x) const -> auto const&
auto operator ()(T&& x) const -> decltype(auto)
{
return x;
if constexpr (std::is_floating_point_v<std::decay_t<decltype(x)>>)
{
return ratio(std::forward<decltype(x)>(x));
}
else
{
return std::forward<decltype(x)>(x);
}
}
};
} inline constexpr exact_cast;

struct inexact
{
Expand Down Expand Up @@ -376,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
25 changes: 14 additions & 11 deletions include/meevax/kernel/ratio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,36 @@
#ifndef INCLUDED_MEEVAX_KERNEL_RATIO_HPP
#define INCLUDED_MEEVAX_KERNEL_RATIO_HPP

#include <gmp.h>
#include <meevax/kernel/pair.hpp>

namespace meevax
{
inline namespace kernel
{
struct ratio : public virtual pair
struct ratio
{
using pair::pair;
mpq_t value;

explicit ratio(double);
ratio();

ratio(ratio const&);

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

auto denominator() const -> const_reference;
~ratio();

auto denominator() -> reference;
explicit ratio(exact_integer const&);

auto invert() const -> ratio;
explicit ratio(exact_integer const&, exact_integer const&);

auto numerator() const -> const_reference;
explicit ratio(double);

auto numerator() -> reference;
explicit ratio(external_representation const&, int = 10);

auto reduce() const -> ratio;
auto denominator() const -> exact_integer;

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

explicit operator double() const;
};
Expand Down
2 changes: 1 addition & 1 deletion include/meevax/kernel/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ inline namespace kernel
{
try
{
return ratio(token, radix).simple();
return make(ratio(token, radix));
}
catch (...)
{
Expand Down
33 changes: 14 additions & 19 deletions src/kernel/exact_integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,25 @@ inline namespace kernel
mpz_init(value);
}

exact_integer::exact_integer(mpz_t given) noexcept
exact_integer::exact_integer(exact_integer const& other) noexcept
{
mpz_init_set(value, given);
mpz_init_set(value, other.value);
}

exact_integer::exact_integer(exact_integer const& given) noexcept
exact_integer::exact_integer(exact_integer && other) noexcept
{
mpz_init_set(value, given.value);
mpz_init(value);
mpz_swap(value, other.value);
}

exact_integer::exact_integer(exact_integer && rhs) noexcept
exact_integer::~exact_integer()
{
*value = *rhs.value;
mpz_init(rhs.value);
mpz_clear(value);
}

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

exact_integer::exact_integer(int 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
12 changes: 12 additions & 0 deletions src/kernel/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,16 @@ inline namespace kernel

#undef DEFINE

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

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

library.define<procedure>("floor", [](let const& xs)
{
return apply<floor>(car(xs));
Expand Down Expand Up @@ -648,6 +658,8 @@ inline namespace kernel
library.export_("-");
library.export_("/");
library.export_("%");
library.export_("ratio-numerator");
library.export_("ratio-denominator");
library.export_("floor");
library.export_("ceiling");
library.export_("truncate");
Expand Down
Loading

0 comments on commit 2aef62b

Please sign in to comment.