Skip to content

Commit 755487b

Browse files
committed
chore: moved concept Number to the general 'math' partition
1 parent 04d302d commit 755487b

10 files changed

Lines changed: 47 additions & 72 deletions

File tree

zero/ifc/math/general.cppm

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,32 @@
55

66
export module math:general;
77

8-
/// Forward declarations of the 'Numbers types'
8+
import std;
9+
import math.symbols;
10+
11+
/// Forward declarations of the 'Numbers types', so they can be shared across different
12+
/// module partitions of 'math'
913
export namespace zero::math {
1014
class Natural;
1115
class Integer;
1216
class Rational;
1317
class Irrational;
1418
class Real;
1519
class Complex;
20+
21+
/// Concept to act as an interface for the abstract concept of 'number' in mathematics.
22+
/// In particular, this interface represents a kind of number that belongs to a concrete set of numbers,
23+
/// for example, the naturals, the integers, the reals, the complex numbers...
24+
template <typename T>
25+
concept Number = (
26+
std::is_same_v<T, Natural> ||
27+
std::is_same_v<T, Integer> ||
28+
std::is_same_v<T, Rational> ||
29+
std::is_same_v<T, Irrational> ||
30+
std::is_same_v<T, Real> ||
31+
std::is_same_v<T, Complex>
32+
) && requires {
33+
T::symbol; /* Check if 'T' has a static member named 'symbol' */
34+
{ T::symbol } -> std::same_as<const MathSymbol&>; // Check if 'T::symbol' has the type MathSymbol
35+
};
1636
}

zero/ifc/math/numbers/detail.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,6 @@ import :numbers.rationals;
1212
using namespace zero::math;
1313

1414
/*++++++++ Operator overloads implementations ++++++++++*/
15-
16-
/*+++++++++++++++++ Naturals +++++++++++++++++*/
17-
// Arithmetic
18-
/* [[nodiscard]] Natural Natural::operator+(const Natural rhs) const noexcept {
19-
return Natural(_number + rhs.number());
20-
}
21-
/// TODO: should we do something about the values < 1?
22-
/// Definetly yes, and now that we have a common base via CRTP,
23-
/// we can override the impl on naturals to provide custom behaviour
24-
///
25-
[[nodiscard]] Natural Natural::operator-(const Natural rhs) const noexcept {
26-
return Natural(_number - rhs.number());
27-
}
28-
29-
[[nodiscard]] Natural Natural::operator*(const Natural rhs) const noexcept {
30-
return Natural(_number * rhs.number());
31-
}
32-
[[nodiscard]] Rational Natural::operator/(const Natural rhs) const noexcept {
33-
return {static_cast<signed int>(_number), static_cast<signed int>(rhs.number())};
34-
} */
35-
// Equality
36-
/* [[nodiscard]] bool Natural::operator==(const Natural rhs) const noexcept {
37-
return _number == rhs.number();
38-
}
39-
[[nodiscard]] bool Natural::operator==(const unsigned int rhs) const noexcept {
40-
return _number == rhs;
41-
} */
42-
4315
/*+++++++++++++++++ Integers +++++++++++++++++*/
4416

4517
// Arithmetic

zero/ifc/math/numbers/general.cppm

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,51 @@
22
export module math:numbers.general;
33

44
import std;
5+
56
import :general;
7+
68
import math.ops;
79
import math.symbols;
810

911
export namespace zero::math {
10-
template<typename Derived>
12+
template<typename Derived> // can't constraint it with Number since ATM only fwd decls
1113
class NumberBase {
1214
// TODO: ensure in our concept (or create another) that all the Number(s) contains
1315
// a fixed name member (like _value or _number), normalized, so we can access it
1416
private:
1517
friend Derived;
1618
NumberBase() = default;
1719

18-
auto _self() -> Derived& {
20+
inline constexpr auto _self() -> Derived& {
1921
return static_cast<Derived&>(*this);
2022
}
2123

22-
auto _self() const -> const Derived& {
24+
inline constexpr auto _self() const -> const Derived& {
2325
return static_cast<const Derived&>(*this);
2426
}
2527
public:
2628
// Addition
27-
[[nodiscard]] Derived operator+(const Derived& rhs) const noexcept {
29+
[[nodiscard]] constexpr auto operator+(const Derived& rhs) const noexcept -> Derived {
2830
return Derived(_self().number() + rhs.number());
2931
}
3032

3133
// Subtraction
32-
[[nodiscard]] Derived operator-(const Derived& rhs) const noexcept {
34+
[[nodiscard]] constexpr auto operator-(const Derived& rhs) const noexcept -> Derived {
3335
return Derived(_self().number() - rhs.number());
3436
}
3537

3638
// Multiplication
37-
[[nodiscard]] Derived operator*(const Derived& rhs) const noexcept {
39+
[[nodiscard]] constexpr auto operator*(const Derived& rhs) const noexcept -> Derived {
3840
return Derived(_self().number() * rhs.number());
3941
}
4042

4143
// Division
42-
[[nodiscard]] Derived operator/(const Derived& rhs) const noexcept {
44+
// TODO: removed the noexcept, since division by Zero must return std::expect or just throw
45+
[[nodiscard]] constexpr auto operator/(const Derived& rhs) const -> Derived {
4346
return Derived(_self().number() / rhs.number());
4447
}
4548

46-
[[nodiscard]] constexpr bool operator==(const Derived& rhs) const noexcept {
49+
[[nodiscard]] constexpr auto operator==(const Derived& rhs) const noexcept -> Derived {
4750
return _self().number() == rhs.number();
4851
}
4952
};

zero/ifc/math/numbers/integers.cppm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import math.ops;
55
import math.symbols;
66

77
import :general;
8-
import :numbers.concepts;
8+
import :numbers.general;
99
import :numbers.naturals;
1010

1111
export namespace zero::math {
@@ -16,7 +16,9 @@ export namespace zero::math {
1616
public:
1717
constexpr static MathSymbol symbol { MathSymbol::Integers };
1818

19-
[[nodiscard]] constexpr explicit Integer(signed int value) noexcept : _number(value) {}
19+
// TODO: explicit templated constructor constrained by Numerical?
20+
// TODO: document in the public API that any decimal number will be truncated
21+
[[nodiscard]] constexpr explicit Integer(const signed int value) noexcept : _number(value) {}
2022
[[nodiscard]] explicit Integer(const Natural value) noexcept
2123
: _number(static_cast<signed int>(value.number())) {}
2224

zero/ifc/math/numbers/naturals.cppm

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ export module math:numbers.naturals;
33
import std;
44
import math.symbols;
55

6+
import :general;
67
import :numbers.general;
7-
import :numbers.concepts;
88

99
export namespace zero::math {
1010
/// A positive integer number
1111
class Natural: public NumberBase<Natural> {
1212
private:
13-
// TODO. shouldn't be unsigned. we may decide what kind of thing we do
13+
// TODO: shouldn't be unsigned. we may decide what kind of thing we do
1414
// with signedness
1515
unsigned int _number;
1616
public:
@@ -21,6 +21,13 @@ export namespace zero::math {
2121
/// @returns an {@link unsigned int}, which is the value stored in the type, being only a positive integer number
2222
[[nodiscard]] constexpr unsigned int number() const noexcept { return _number; }
2323

24+
/// TODO: should we do something about the values < 1?
25+
/// Definetly yes, and now that we have a common base via CRTP,
26+
/// we can override the impl on naturals to provide custom behaviour
27+
///
28+
/* [[nodiscard]] Natural Natural::operator-(const Natural rhs) const noexcept {
29+
return Natural(_number - rhs.number());
30+
} */
2431
/// @overload
2532
[[nodiscard]] bool operator==(unsigned int rhs) const noexcept {
2633
return _number == rhs;

zero/ifc/math/numbers/numbers.cppm

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ export module math:numbers;
55
import std;
66
import math.symbols;
77

8-
export import :numbers.concepts;
9-
import :numbers.general;
10-
import :numbers.detail;
11-
128
export import :numbers.naturals;
139
export import :numbers.integers;
1410
export import :numbers.rationals;

zero/ifc/math/numbers/numbers_concepts.cppm

Lines changed: 0 additions & 23 deletions
This file was deleted.

zero/ifc/math/numbers/rationals.cppm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import math.ops;
55
import math.symbols;
66

77
import :general;
8-
import :numbers.concepts;
9-
8+
import :numbers.general;
109
import :numbers.naturals;
1110
import :numbers.integers;
1211

zero/ifc/physics/quantities/units.cppm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ using namespace zero::math;
1919

2020
export namespace zero::physics {
2121
/* Base units */
22-
template<Ratio r, Symbol s>
22+
template<Ratio r, Symbol s>
2323
struct base_unit {
2424
using ratio = r;
2525
using symbol = s;

zork_config/zork_local_linux.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ interfaces = [
7979
{ file = 'math/linear_algebra/root.cppm', module_name = 'math.linear_algebra' },
8080
# Numbers - Core
8181
{ file = 'math/numbers/general.cppm', partition = { module = 'math', partition_name = 'numbers.general' } },
82-
{ file = 'math/numbers/numbers_concepts.cppm', partition = { module = 'math', partition_name = 'numbers.concepts' } },
8382
{ file = 'math/numbers/naturals.cppm', partition = { module = 'math', partition_name = 'numbers.naturals' } },
8483
{ file = 'math/numbers/integers.cppm', partition = { module = 'math', partition_name = 'numbers.integers' } },
8584
{ file = 'math/numbers/rationals.cppm', partition = { module = 'math', partition_name = 'numbers.rationals' } },

0 commit comments

Comments
 (0)