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 "rounding.hpp"
34#include "testing.hpp"
35#include "types/rounding.hpp"
36#include "util.hpp"
37
38#include <functional>
39#include <string>
40#include <utility>
41
46namespace steppable
47{
52 class Number
53 {
55 std::string value;
56
58 size_t prec;
59
62
63 template<__internals::utils::StringLiteral fnName>
64 [[nodiscard]] size_t determinePrec(const Number& rhs) const
65 {
66 size_t usePrec = 0;
68 usePrec = std::max(prec, rhs.prec);
70 usePrec = std::min(prec, rhs.prec);
72 usePrec = prec;
74 usePrec = rhs.prec;
76 usePrec = 0;
77 else
78 {
79 usePrec = 0;
80 output::warning(std::string(fnName.value), "Invalid precision specified"s);
81 }
82
83 return usePrec;
84 }
85
86 public:
91 Number(std::string value = "0", size_t prec = 10, RoundingMode mode = RoundingMode::USE_CURRENT_PREC);
92
97 template<concepts::Numeric ValueT>
99 value(std::to_string(value)), prec(prec), mode(mode)
100 {
101 }
102
103 void set(std::string newVal) { value = std::move(newVal); }
104
106 {
107 this->mode = mode;
108 prec = newPrec;
110 }
111
117 Number operator+(const Number& rhs) const;
118
124 Number operator-(const Number& rhs) const;
125
131 Number operator*(const Number& rhs) const;
132
138 Number operator/(const Number& rhs) const;
139
147 [[nodiscard]] Number mod(const Number& rhs) const;
148
154 Number operator%(const Number& rhs) const;
155
161 Number operator^(const Number& rhs);
162
168 Number& operator+=(const Number& rhs);
169
175 Number& operator-=(const Number& rhs);
176
182 Number& operator*=(const Number& rhs);
183
189 Number& operator/=(const Number& rhs);
190
196 Number& operator%=(const Number& rhs);
197
203 Number& operator^=(const Number& rhs);
204
210 bool operator==(const Number& rhs) const;
211
217 bool operator!=(const Number& rhs) const;
218
224 bool operator<(const Number& rhs) const;
225
231 bool operator>(const Number& rhs) const;
232
238 bool operator<=(const Number& rhs) const;
239
245 bool operator>=(const Number& rhs) const;
246
252
258
264 Number operator-() const;
265
271 Number operator+() const;
272
277 [[nodiscard]] std::string present() const;
278
283 [[nodiscard]] Number abs() const
284 {
285 if (*this > 0)
286 return *this;
287 return -(*this);
288 }
289 };
290
295 namespace literals
296 {
297 inline Number operator""_n(long double value) { return Number(value); }
298
299 inline Number operator""_n(unsigned long long value) { return Number(value); }
300 } // namespace literals
301} // namespace steppable
302
303template<>
304struct std::hash<steppable::Number>
305{
306 size_t operator()(const steppable::Number& n) const { return hash<std::string>()(n.present()); }
307};
308
309std::ostream& operator<<(std::ostream& os, const steppable::Number& number);
Represents a number with arbitrary precision. It basically stores the value as a string.
Definition number.hpp:53
bool operator>=(const Number &rhs) const
Compares two numbers for greater than or equal to.
Definition number.cpp:138
Number operator*(const Number &rhs) const
Multiplies two numbers.
Definition number.cpp:59
Number & operator*=(const Number &rhs)
Multiplies the number by another number and assigns the result to the current number.
Definition number.cpp:102
bool operator<=(const Number &rhs) const
Compares two numbers for less than or equal to.
Definition number.cpp:136
Number & operator%=(const Number &rhs)
Calculates the remainder of two numbers and assigns the result to the current number.
Definition number.cpp:116
size_t prec
The precision of the number.
Definition number.hpp:58
bool operator<(const Number &rhs) const
Compares two numbers for less than.
Definition number.cpp:132
bool operator!=(const Number &rhs) const
Compares two numbers for inequality.
Definition number.cpp:130
Number(std::string value="0", size_t prec=10, RoundingMode mode=RoundingMode::USE_CURRENT_PREC)
Initializes a number with a specified value.
Definition number.cpp:50
bool operator>(const Number &rhs) const
Compares two numbers for greater than.
Definition number.cpp:134
Number operator--()
Decrements the number by one.
Definition number.cpp:156
Number & operator^=(const Number &rhs)
Raises the number to a power and assigns the result to the current number.
Definition number.cpp:122
Number & operator-=(const Number &rhs)
Subtracts the number from another number and assigns the result to the current number.
Definition number.cpp:95
Number mod(const Number &rhs) const
Takes a modulus operation.
Definition number.cpp:78
Number operator++()
Increments the number by one.
Definition number.cpp:150
Number operator%(const Number &rhs) const
Calculates the remainder of two numbers. (Modulus)
Definition number.cpp:73
Number & operator/=(const Number &rhs)
Divides the number by another number and assigns the result to the current number.
Definition number.cpp:109
std::string value
The value of the number.
Definition number.hpp:55
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:98
RoundingMode mode
The rounding mode of the number.
Definition number.hpp:61
Number operator+() const
Unary plus operator.
Definition number.cpp:148
std::string present() const
Presents the number in a human-readable format.
Definition number.cpp:162
size_t determinePrec(const Number &rhs) const
Definition number.hpp:64
Number & operator+=(const Number &rhs)
Adds the number to another number and assigns the result to the current number.
Definition number.cpp:89
bool operator==(const Number &rhs) const
Compares two numbers for equality.
Definition number.cpp:128
Number abs() const
Gets the absolute value of the number.
Definition number.hpp:283
void set(std::string newVal)
Definition number.hpp:103
void setPrec(size_t newPrec, RoundingMode mode=RoundingMode::USE_CURRENT_PREC)
Definition number.hpp:105
Number operator-() const
Unary minus operator.
Definition number.cpp:140
Number operator/(const Number &rhs) const
Divides two numbers.
Definition number.cpp:66
Number operator^(const Number &rhs)
Raises the number to a power.
Definition number.cpp:83
std::string roundOff(const std::string &_number, const size_t digits, Rounding mode)
Round off a number to the nearest integer.
Definition rounding.cpp:60
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
RoundingMode
Specifies how Steppable should round the number in operations.
Definition rounding.hpp:34
@ DISCARD_ALL_DECIMALS
Do not append any decimal places.
Definition rounding.hpp:48
@ USE_MINIMUM_PREC
Use the lower precision whenever possible.
Definition rounding.hpp:39
@ USE_OTHER_PREC
Use the other number's precision.
Definition rounding.hpp:45
@ USE_MAXIMUM_PREC
Use the higher precision whenever possible.
Definition rounding.hpp:36
@ USE_CURRENT_PREC
Use the current precision.
Definition rounding.hpp:42
size_t operator()(const steppable::Number &n) const
Definition number.hpp:306
Untitled