Skip to content

Commit d94ea5f

Browse files
committed
Add decimal check.
1 parent 84d2551 commit d94ea5f

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

include/util.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ namespace steppable::__internals::utils
208208
if (*decimal > MAX_DECIMALS)
209209
{
210210
output::error("checkDecimalArg"s,
211-
"The number of decimals ({}) is more than the accepted {} digits."s,
211+
"The number of decimals ({0}) is more than the accepted {1} digits."s,
212212
{ std::to_string(*decimal), std::to_string(MAX_DECIMALS) });
213213
programSafeExit(1);
214214
}

src/calc/hyp/hyp.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ namespace steppable::__internals::arithmetic
5151
{
5252
std::string sinh(const std::string& x, const int decimals)
5353
{
54+
checkDecimalArg(&decimals);
55+
5456
const auto& twoX = multiply(x, "2", 0);
5557
const auto& eTwoX = roundOff(power(static_cast<std::string>(constants::E), twoX, 0), decimals + 2);
5658
const auto& eX = roundOff(power(static_cast<std::string>(constants::E), x, 0), decimals + 2);
@@ -63,6 +65,8 @@ namespace steppable::__internals::arithmetic
6365

6466
std::string cosh(const std::string& x, const int decimals)
6567
{
68+
checkDecimalArg(&decimals);
69+
6670
const auto& twoX = multiply(x, "2", 0);
6771
const auto& eTwoX = roundOff(power(static_cast<std::string>(constants::E), twoX, 0), decimals + 2);
6872
const auto& eX = roundOff(power(static_cast<std::string>(constants::E), x, 0), decimals + 2);
@@ -75,6 +79,8 @@ namespace steppable::__internals::arithmetic
7579

7680
std::string tanh(const std::string& x, const int decimals)
7781
{
82+
checkDecimalArg(&decimals);
83+
7884
const auto& numerator = sinh(x, decimals);
7985
const auto& denominator = cosh(x, decimals);
8086

@@ -83,6 +89,8 @@ namespace steppable::__internals::arithmetic
8389

8490
std::string coth(const std::string& x, const int decimals)
8591
{
92+
checkDecimalArg(&decimals);
93+
8694
const auto& denominator = tanh(x, decimals);
8795
if (isZeroString(denominator))
8896
{
@@ -95,6 +103,8 @@ namespace steppable::__internals::arithmetic
95103

96104
std::string csch(const std::string& x, const int decimals)
97105
{
106+
checkDecimalArg(&decimals);
107+
98108
const auto& denominator = sinh(x, decimals);
99109
if (isZeroString(denominator))
100110
{
@@ -107,6 +117,8 @@ namespace steppable::__internals::arithmetic
107117

108118
std::string sech(const std::string& x, const int decimals)
109119
{
120+
checkDecimalArg(&decimals);
121+
110122
const auto& denominator = cosh(x, decimals);
111123
if (isZeroString(denominator))
112124
{
@@ -119,6 +131,8 @@ namespace steppable::__internals::arithmetic
119131

120132
std::string asinh(const std::string& x, const int decimals)
121133
{
134+
checkDecimalArg(&decimals);
135+
122136
// /------|
123137
// / 2
124138
// asinh(x) = ln(x + \/ x + 1 )
@@ -132,6 +146,8 @@ namespace steppable::__internals::arithmetic
132146

133147
std::string acosh(const std::string& x, const int decimals)
134148
{
149+
checkDecimalArg(&decimals);
150+
135151
// /------|
136152
// / 2
137153
// acosh(x) = ln(x + \/ x - 1 )
@@ -145,6 +161,8 @@ namespace steppable::__internals::arithmetic
145161

146162
std::string atanh(const std::string& x, const int decimals)
147163
{
164+
checkDecimalArg(&decimals);
165+
148166
// 1 1 + x
149167
// atanh(x) = --- * ln( ------- )
150168
// 2 1 - x
@@ -158,6 +176,8 @@ namespace steppable::__internals::arithmetic
158176

159177
std::string acoth(const std::string& x, const int decimals)
160178
{
179+
checkDecimalArg(&decimals);
180+
161181
// 1 1 + x
162182
// acoth(x) = --- * ln( ------- )
163183
// 2 x - 1
@@ -171,6 +191,8 @@ namespace steppable::__internals::arithmetic
171191

172192
std::string acsch(const std::string& x, const int decimals)
173193
{
194+
checkDecimalArg(&decimals);
195+
174196
// /-------|
175197
// 1 / 1
176198
// acsch(x) = ln(--- + / ---- + 1 )
@@ -189,6 +211,8 @@ namespace steppable::__internals::arithmetic
189211

190212
std::string asech(const std::string& x, const int decimals)
191213
{
214+
checkDecimalArg(&decimals);
215+
192216
// /-------|
193217
// 1 / 1
194218
// asech(x) = ln(--- + / ---- - 1 )

src/calc/log/log.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ namespace steppable::__internals::arithmetic
4747
{
4848
std::string _log(const std::string& x, const size_t _decimals)
4949
{
50+
checkDecimalArg(&_decimals);
51+
5052
const auto& decimals = _decimals + 2;
5153
// Zero check
5254
if (numUtils::isZeroString(x))
@@ -104,6 +106,8 @@ namespace steppable::__internals::arithmetic
104106
// Common logarithms
105107
std::string logb(const std::string& _number, const std::string& _base, const size_t _decimals)
106108
{
109+
checkDecimalArg(&_decimals);
110+
107111
// -ln(1/a)
108112
// log (x) = ----------
109113
// b -ln(1/b)
@@ -118,11 +122,23 @@ namespace steppable::__internals::arithmetic
118122
return numUtils::roundOff(result, _decimals);
119123
}
120124

121-
std::string log10(const std::string& _number, const size_t _decimals) { return logb(_number, "10", _decimals); }
125+
std::string log10(const std::string& _number, const size_t _decimals)
126+
{
127+
checkDecimalArg(&_decimals);
128+
return logb(_number, "10", _decimals);
129+
}
122130

123-
std::string log2(const std::string& _number, const size_t _decimals) { return logb(_number, "2", _decimals); }
131+
std::string log2(const std::string& _number, const size_t _decimals)
132+
{
133+
checkDecimalArg(&_decimals);
134+
return logb(_number, "2", _decimals);
135+
}
124136

125-
std::string ln(const std::string& _number, const size_t _decimals) { return _log(_number, _decimals); }
137+
std::string ln(const std::string& _number, const size_t _decimals)
138+
{
139+
checkDecimalArg(&_decimals);
140+
return _log(_number, _decimals);
141+
}
126142
} // namespace steppable::__internals::arithmetic
127143

128144
#ifndef NO_MAIN

src/calc/root/root.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ namespace steppable::__internals::arithmetic
127127

128128
std::string _root(const std::string& _number, const std::string& base, const size_t _decimals, const int steps)
129129
{
130+
checkDecimalArg(&_decimals);
131+
130132
if (isDecimal(base))
131133
{
132134
const auto& fraction = Fraction(base);
@@ -186,6 +188,8 @@ namespace steppable::__internals::arithmetic
186188

187189
std::string root(const std::string& _number, const std::string& base, const size_t _decimals, const int steps)
188190
{
191+
checkDecimalArg(&_decimals);
192+
189193
if (isZeroString(_number))
190194
return "0";
191195
if (isInteger(_number))

src/calc/trig/trig.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ namespace steppable::__internals::arithmetic
9797

9898
std::string _cos(const std::string& x, const int decimals)
9999
{
100+
checkDecimalArg(&decimals);
101+
100102
// .,,. ., /=====================================================================\
101103
// //%, .( | NOTE: DO NOT CALL THIS METHOD DIRECTLY IN PRODUCTION CODE! |
102104
// /%( ., | /-\ THIS METHOD ONLY ACCEPTS RADIANS AS INPUT, AND DOES |
@@ -135,6 +137,8 @@ namespace steppable::__internals::arithmetic
135137

136138
std::string cos(const std::string& x, const int decimals, const int mode)
137139
{
140+
checkDecimalArg(&decimals);
141+
138142
// Mode Options:
139143
// 0: Radians (default)
140144
// 1: Degrees (converts to radians)
@@ -169,6 +173,8 @@ namespace steppable::__internals::arithmetic
169173

170174
std::string sin(const std::string& x, const int decimals, const int mode)
171175
{
176+
checkDecimalArg(&decimals);
177+
172178
// Mode Options:
173179
// 0: Radians (default)
174180
// 1: Degrees (converts to radians)
@@ -206,6 +212,8 @@ namespace steppable::__internals::arithmetic
206212

207213
std::string tan(const std::string& x, const int decimals, const int mode)
208214
{
215+
checkDecimalArg(&decimals);
216+
209217
// Mode Options:
210218
// 0: Radians (default)
211219
// 1: Degrees (converts to radians)
@@ -260,6 +268,8 @@ namespace steppable::__internals::arithmetic
260268

261269
std::string csc(const std::string& x, const int decimals, const int mode)
262270
{
271+
checkDecimalArg(&decimals);
272+
263273
auto sinX = sin(x, decimals + 1, mode);
264274
if (isZeroString(sinX))
265275
{
@@ -271,6 +281,8 @@ namespace steppable::__internals::arithmetic
271281

272282
std::string sec(const std::string& x, const int decimals, const int mode)
273283
{
284+
checkDecimalArg(&decimals);
285+
274286
auto cosX = cos(x, decimals + 1, mode);
275287
if (isZeroString(cosX))
276288
{
@@ -282,6 +294,8 @@ namespace steppable::__internals::arithmetic
282294

283295
std::string cot(const std::string& x, const int decimals, const int mode)
284296
{
297+
checkDecimalArg(&decimals);
298+
285299
auto tanX = tan(x, decimals + 1, mode);
286300
if (isZeroString(tanX))
287301
{
@@ -293,6 +307,8 @@ namespace steppable::__internals::arithmetic
293307

294308
std::string atan(const std::string& _x, const int decimals, const int mode)
295309
{
310+
checkDecimalArg(&decimals);
311+
296312
auto x = _x;
297313
// Zero check
298314
if (isZeroString(x))
@@ -346,6 +362,8 @@ namespace steppable::__internals::arithmetic
346362

347363
std::string asin(const std::string& x, const int decimals, const int mode)
348364
{
365+
checkDecimalArg(&decimals);
366+
349367
if (compare(abs(x, 0), "1", 0) == "1")
350368
{
351369
error("trig::asin", $("trig", "b06650e0-7101-4734-9647-5abb56beb492"));
@@ -386,6 +404,8 @@ namespace steppable::__internals::arithmetic
386404

387405
std::string acos(const std::string& x, const int decimals, const int mode)
388406
{
407+
checkDecimalArg(&decimals);
408+
389409
if (compare(x, "1", 0) == "2")
390410
return "0";
391411
std::string circleAngle;
@@ -410,6 +430,8 @@ namespace steppable::__internals::arithmetic
410430

411431
std::string asec(const std::string& x, const int decimals, const int mode)
412432
{
433+
checkDecimalArg(&decimals);
434+
413435
if (compare(abs(x, 0), "1", 0) == "1")
414436
{
415437
error("trig::asec", $("trig", "fffb4742-3712-4c9a-a7ff-65cd51508a0a"));
@@ -424,6 +446,8 @@ namespace steppable::__internals::arithmetic
424446

425447
std::string acsc(const std::string& x, const int decimals, const int mode)
426448
{
449+
checkDecimalArg(&decimals);
450+
427451
if (compare(abs(x, 0), "1", 0) != "0")
428452
{
429453
error("trig::acsc"s, $("trig", "c021dfde-300c-4d74-a6a1-87a514c1bbe0"));
@@ -438,6 +462,8 @@ namespace steppable::__internals::arithmetic
438462

439463
std::string acot(const std::string& x, const int decimals, const int mode)
440464
{
465+
checkDecimalArg(&decimals);
466+
441467
if (compare(abs(x, 0), "1", 0) != "0")
442468
{
443469
error("trig::acot"s, $("trig", "c0c6a29f-abda-4676-9662-1d00f94f10a4"));

src/calculus/nInt/nInt.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "fn/calculus.hpp"
2525
#include "output.hpp"
2626
#include "rounding.hpp"
27+
#include "util.hpp"
2728

2829
#include <cmath>
2930
#include <functional>
@@ -32,6 +33,7 @@
3233

3334
using namespace steppable::__internals::arithmetic;
3435
using namespace steppable::__internals::numUtils;
36+
using namespace steppable::__internals::utils;
3537
using namespace std::literals;
3638

3739
namespace steppable::__internals::calculus
@@ -42,6 +44,8 @@ namespace steppable::__internals::calculus
4244
const int max_steps,
4345
const int decimals)
4446
{
47+
checkDecimalArg(&decimals);
48+
4549
auto acc = "0." + std::string(decimals - 1, '0') + "1";
4650
auto previous = std::vector(max_steps, "0"s);
4751
auto current = std::vector(max_steps, "0"s);

0 commit comments

Comments
 (0)