Steppable 0.0.1
A CAS project written from scratch in C++
Loading...
Searching...
No Matches
number.hpp
1/**************************************************************************************************
2 * Copyright (c) 2023-2025 NWSOFT *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy *
5 * of this software and associated documentation files (the "Software"), to deal *
6 * in the Software without restriction, including without limitation the rights *
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8 * copies of the Software, and to permit persons to whom the Software is *
9 * furnished to do so, subject to the following conditions: *
10 * *
11 * The above copyright notice and this permission notice shall be included in all *
12 * copies or substantial portions of the Software. *
13 * *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20 * SOFTWARE. *
21 **************************************************************************************************/
22
30
31#pragma once
32
33#include "testing.hpp"
34#include "util.hpp"
35
36#include <cstdint>
37#include <string>
38#include <utility>
39
44namespace steppable
45{
50 enum class RoundingMode : std::uint8_t
51 {
54
57
60
63
66 };
67
68 enum class Rounding : std::uint8_t
69 {
70 ROUND_DOWN = 0x00,
71 ROUND_UP = 0x01,
72 ROUND_OFF = 0x02,
73 };
74
79 class Number
80 {
82 std::string value;
83
85 size_t prec;
86
89
90 template<__internals::utils::StringLiteral fnName>
91 [[nodiscard]] size_t determinePrec(const Number& rhs) const
92 {
93 size_t usePrec = 0;
95 usePrec = std::max(prec, rhs.prec);
97 usePrec = std::min(prec, rhs.prec);
99 usePrec = prec;
101 usePrec = rhs.prec;
103 usePrec = 0;
104 else
105 {
106 usePrec = 0;
107 output::warning(std::string(fnName.value), "Invalid precision specified"s);
108 }
109
110 return usePrec;
111 }
112
113 public:
115 Number();
116
117 Number(const Number& rhs);
118
123 Number(std::string value = "0", size_t prec = 10, RoundingMode mode = RoundingMode::USE_CURRENT_PREC);
124
129 template<concepts::Numeric ValueT>
131 value(std::to_string(value)), prec(prec), mode(mode)
132 {
133 }
134
135 void set(std::string newVal) { value = std::move(newVal); }
136
138 {
139 this->mode = mode;
140 prec = newPrec;
141 }
142
148 Number operator+(const Number& rhs) const;
149
155 Number operator-(const Number& rhs) const;
156
162 Number operator*(const Number& rhs) const;
163
169 Number operator/(const Number& rhs) const;
170
178 [[nodiscard]] Number mod(const Number& rhs) const;
179
185 Number operator%(const Number& rhs) const;
186
192 Number operator^(const Number& rhs);
193
199 Number& operator+=(const Number& rhs);
200
206 Number& operator-=(const Number& rhs);
207
213 Number& operator*=(const Number& rhs);
214
220 Number& operator/=(const Number& rhs);
221
227 Number& operator%=(const Number& rhs);
228
234 Number& operator^=(const Number& rhs);
235
241 bool operator==(const Number& rhs) const;
242
248 bool operator!=(const Number& rhs) const;
249
255 bool operator<(const Number& rhs) const;
256
262 bool operator>(const Number& rhs) const;
263
269 bool operator<=(const Number& rhs) const;
270
276 bool operator>=(const Number& rhs) const;
277
283
289
295 Number operator-() const;
296
302 Number operator+() const;
303
308 [[nodiscard]] std::string present() const;
309 };
310
315 namespace literals
316 {
317 inline Number operator""_n(long double value) { return Number(value); }
318
319 inline Number operator""_n(unsigned long long value) { return Number(value); }
320 } // namespace literals
321} // namespace steppable
Represents a number with arbitrary precision. It basically stores the value as a string.
Definition number.hpp:80
bool operator>=(const Number &rhs) const
Compares two numbers for greater than or equal to.
Definition number.cpp:142
Number operator*(const Number &rhs) const
Multiplies two numbers.
Definition number.cpp:63
Number & operator*=(const Number &rhs)
Multiplies the number by another number and assigns the result to the current number.
Definition number.cpp:106
bool operator<=(const Number &rhs) const
Compares two numbers for less than or equal to.
Definition number.cpp:140
Number & operator%=(const Number &rhs)
Calculates the remainder of two numbers and assigns the result to the current number.
Definition number.cpp:120
size_t prec
The precision of the number.
Definition number.hpp:85
bool operator<(const Number &rhs) const
Compares two numbers for less than.
Definition number.cpp:136
bool operator!=(const Number &rhs) const
Compares two numbers for inequality.
Definition number.cpp:134
bool operator>(const Number &rhs) const
Compares two numbers for greater than.
Definition number.cpp:138
Number operator--()
Decrements the number by one.
Definition number.cpp:160
Number & operator^=(const Number &rhs)
Raises the number to a power and assigns the result to the current number.
Definition number.cpp:126
Number & operator-=(const Number &rhs)
Subtracts the number from another number and assigns the result to the current number.
Definition number.cpp:99
Number mod(const Number &rhs) const
Takes a modulus operation.
Definition number.cpp:82
Number operator++()
Increments the number by one.
Definition number.cpp:154
Number operator%(const Number &rhs) const
Calculates the remainder of two numbers. (Modulus)
Definition number.cpp:77
Number & operator/=(const Number &rhs)
Divides the number by another number and assigns the result to the current number.
Definition number.cpp:113
std::string value
The value of the number.
Definition number.hpp:82
Number(ValueT value, size_t prec=10, RoundingMode mode=RoundingMode::USE_CURRENT_PREC)
Initializes a number with a C/C++ long double value.
Definition number.hpp:130
RoundingMode mode
The rounding mode of the number.
Definition number.hpp:88
Number()
The default constructor. Initializes the number with a value of 0.
Definition number.cpp:50
Number operator+() const
Unary plus operator.
Definition number.cpp:152
std::string present() const
Presents the number in a human-readable format.
Definition number.cpp:166
size_t determinePrec(const Number &rhs) const
Definition number.hpp:91
Number & operator+=(const Number &rhs)
Adds the number to another number and assigns the result to the current number.
Definition number.cpp:93
bool operator==(const Number &rhs) const
Compares two numbers for equality.
Definition number.cpp:132
void set(std::string newVal)
Definition number.hpp:135
void setPrec(size_t newPrec, RoundingMode mode=RoundingMode::USE_CURRENT_PREC)
Definition number.hpp:137
Number operator-() const
Unary minus operator.
Definition number.cpp:144
Number operator/(const Number &rhs) const
Divides two numbers.
Definition number.cpp:70
Number operator^(const Number &rhs)
Raises the number to a power.
Definition number.cpp:87
Literal suffixes for literals to be converted to Steppable objects.
void warning(const std::basic_string< T > &name, const std::basic_string< T > &msg, const std::vector< std::string > &args={})
Prints a warning message.
Definition output.hpp:107
The public namespace for the Steppable library.
Definition argParse.cpp:40
Rounding
Definition number.hpp:69
@ ROUND_DOWN
Rounds the number down.
Definition number.hpp:70
@ ROUND_UP
Rounds the number up.
Definition number.hpp:71
@ ROUND_OFF
Rounds the number off.
Definition number.hpp:72
RoundingMode
Specifies how Steppable should round the number in operations.
Definition number.hpp:51
@ DISCARD_ALL_DECIMALS
Do not append any decimal places.
Definition number.hpp:65
@ USE_MINIMUM_PREC
Use the lower precision whenever possible.
Definition number.hpp:56
@ USE_OTHER_PREC
Use the other number's precision.
Definition number.hpp:62
@ USE_MAXIMUM_PREC
Use the higher precision whenever possible.
Definition number.hpp:53
@ USE_CURRENT_PREC
Use the current precision.
Definition number.hpp:59
Untitled