Skip to content

Commit

Permalink
code(numbers): Created the 'Number' concept to determine whenever a t…
Browse files Browse the repository at this point in the history
…ype satisfies the criteria to be a Number
  • Loading branch information
TheRustifyer committed Nov 12, 2023
1 parent 557a7fe commit 9f4923a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
30 changes: 23 additions & 7 deletions zero/ifc/math/numbers.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export module math.numbers;

import std;
import math.symbols;

export {
// Forward declarations
Expand All @@ -13,13 +14,31 @@ export {
class Real;
class Complex;

// TODO concept Number
// TODO adding the set symbols for every type

/// Concept to act as an interface for the abstract concept of 'number' in mathematics.
/// In particular, this interface represents a kind of number that belongs to a concrete set of numbers,
/// for example, the naturals, the integers, the reals, the complex numbers...
template <typename T>
concept Number = (
std::is_same_v<T, Natural> ||
std::is_same_v<T, Integer> ||
std::is_same_v<T, Rational> ||
std::is_same_v<T, Irrational> ||
std::is_same_v<T, Real> ||
std::is_same_v<T, Complex>
) && requires {
T::symbol; /* Check if 'T' has a static member named 'symbol' */
{ T::symbol } -> std::same_as<const MathSymbol&>; // Check if 'T::symbol' has the type MathSymbol
};

/// A positive integer number
class Natural {
private:
unsigned int _number;
public:
static constexpr MathSymbol symbol { MathSymbol::Naturals };

[[nodiscard]] constexpr explicit Natural(unsigned int value) noexcept : _number(value) {}

/// @return an {@link unsigned int}, which is the value stored in the type, being only a positive integer number
Expand All @@ -34,8 +53,11 @@ export {

/// A whole real number
class Integer {
private:
signed int _number;
public:
static constexpr MathSymbol symbol { MathSymbol::Integers };

[[nodiscard]] constexpr explicit Integer(signed int value) noexcept : _number(value) {}
[[nodiscard]] constexpr explicit Integer(const Natural value) noexcept
: _number(static_cast<signed int>(value.number())) {}
Expand Down Expand Up @@ -106,12 +128,6 @@ export {
// Real imaginary;
// };


// /**
// * Concept to act as an interface for the abstract concept of 'number' in mathematics.
// * In particular, this interface represents a kind of number that belongs to a concrete set of numbers,
// * for example, the naturals, the integers, the reals, the complexes...
// *
}

// TODO move this ones to an internal module partition?? or to a module implementation better?
Expand Down
4 changes: 2 additions & 2 deletions zero/ifc/math/symbols.cppm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export module math.symbols;

enum class MathSymbol {
export enum class MathSymbol {
// Basic Math Operators
Plus = 0x002B, // +
Minus = 0x2212, // -
Expand Down Expand Up @@ -49,7 +49,7 @@ enum class MathSymbol {
SineWave = 0x223F, //

// Number Sets
NaturalNumbers = 0x2115, //
Naturals = 0x2115, //
Integers = 0x2124, //
Rationals = 0x211A, //
Reals = 0x211D, //
Expand Down
5 changes: 5 additions & 0 deletions zero/tests/math/numbers_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

TestSuite numbers_suite {"Numbers TS"};

/// Compile time tests for numbers
static_assert(Number<Natural>);
static_assert(Number<Integer>);
static_assert(!Number<std::string>);

void numbers_tests() {
TEST_CASE(numbers_suite, "Testing the construction of a Number", [] {
auto natural = Natural(1);
Expand Down

0 comments on commit 9f4923a

Please sign in to comment.