Skip to content

Commit 159fa68

Browse files
committed
Add log (#34)
1 parent 3ae75bd commit 159fa68

File tree

8 files changed

+288
-1
lines changed

8 files changed

+288
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ SET(COMPONENTS
100100
division
101101
root
102102
factorial
103-
trig hyp)
103+
trig hyp log)
104104
# NEW_COMPONENT: PATCH Do NOT remove the previous comment.
105105

106106
SET(TARGETS ${COMPONENTS} util)

include/fn/basicArithm.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,49 @@ namespace steppable::__internals::arithmetic
430430
*/
431431
std::string coth(const std::string& x, int decimals);
432432

433+
/**
434+
* @brief Calculates the logarithm with a given base.
435+
*
436+
* @param _number The number to calculate the logarithm of.
437+
* @param _base The base of the logarithm.
438+
* @param _decimals The number of decimal places to round off to.
439+
* @return The result of the logarithm operation.
440+
*/
441+
std::string logb(const std::string& _number, const std::string& _base, size_t _decimals);
442+
443+
/**
444+
* @brief Calculates the common logarithm of a number.
445+
*
446+
* @note The common logarithm is the logarithm with base 10.
447+
*
448+
* @param _number The number to calculate the common logarithm of.
449+
* @param _decimals The number of decimal places to round off to.
450+
* @return The result of the common logarithm operation.
451+
*/
452+
std::string log10(const std::string& _number, size_t _decimals);
453+
454+
/**
455+
* @brief Calculates the binary logarithm of a number.
456+
*
457+
* @note The binary logarithm is the logarithm with base 2.
458+
*
459+
* @param _number The number to calculate the binary logarithm of.
460+
* @param _decimals The number of decimal places to round off to.
461+
* @return The result of the binary logarithm operation.
462+
*/
463+
std::string log2(const std::string& _number, size_t _decimals);
464+
465+
/**
466+
* @brief Calculates the natural logarithm of a number.
467+
*
468+
* @note The natural logarithm is the logarithm with base e.
469+
*
470+
* @param _number The number to calculate the natural logarithm of.
471+
* @param _decimals The number of decimal places to round off to.
472+
* @return The result of the natural logarithm operation.
473+
*/
474+
std::string ln(const std::string& _number, size_t _decimals);
475+
433476
/**
434477
* @brief Executes a given predicate function a specified number of times.
435478
*

res/approximation_scripts/Ln.mlx

60 Bytes
Binary file not shown.

res/approximation_scripts/Trig.mlx

-9 Bytes
Binary file not shown.

src/log/log.cpp

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2024 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+
23+
/**
24+
* @file log.cpp
25+
* @brief Contains the implementation of the logarithmic functions.
26+
*
27+
* @author Andy Zhang
28+
* @date 23rd June 2024
29+
*/
30+
31+
#include "argParse.hpp"
32+
#include "fn/basicArithm.hpp"
33+
#include "logReport.hpp"
34+
#include "output.hpp"
35+
#include "rounding.hpp"
36+
#include "util.hpp"
37+
38+
#include <cstdlib>
39+
#include <iostream>
40+
#include <string>
41+
42+
using namespace steppable::__internals::arithmetic;
43+
using namespace steppable::__internals::utils;
44+
using namespace std::literals;
45+
46+
namespace steppable::__internals::arithmetic
47+
{
48+
std::string _log(const std::string& _number, const size_t _decimals)
49+
{
50+
// /-\ +--------------------------------------------+
51+
// / ! \ | WARNING: DO NOT CALL THIS METHOD DIRECTLY! |
52+
// /-----\ +--------------------------------------------+
53+
// 4 3 2
54+
// (x - 1)(137x + 1762x + 3762x + 1762x + 137)
55+
// ln(x) = ------------------------------------------------
56+
// 5 4 3 2
57+
// 30(x + 25x + 100x + 100x + 25x + 1)
58+
59+
auto x2 = power(_number, "2", 0);
60+
auto x3 = power(_number, "3", 0);
61+
auto x4 = multiply(x2, x2, 0);
62+
auto x5 = multiply(x2, x3, 0);
63+
64+
auto xMinus1 = subtract(_number, "1", 0);
65+
auto numerator = multiply(x4, "137", 0);
66+
numerator = add(numerator, multiply(x3, "1762", 0), 0);
67+
numerator = add(numerator, multiply(x2, "3762", 0), 0);
68+
numerator = add(numerator, multiply(_number, "1762", 0), 0);
69+
numerator = add(numerator, "137", 0);
70+
numerator = multiply(xMinus1, numerator, 0);
71+
72+
auto denominator = add(x5, multiply(x4, "25", 0), 0);
73+
denominator = add(denominator, multiply(x3, "100", 0), 0);
74+
denominator = add(denominator, multiply(x2, "100", 0), 0);
75+
denominator = add(denominator, multiply(_number, "25", 0), 0);
76+
denominator = add(denominator, "1", 0);
77+
denominator = multiply("30", denominator, 0);
78+
79+
auto result = divide(numerator, denominator, 0, static_cast<int>(_decimals));
80+
return result;
81+
}
82+
83+
// Common logarithms
84+
std::string logb(const std::string& _number, const std::string& _base, const size_t _decimals)
85+
{
86+
// ln(a)
87+
// log (x) = -------
88+
// b ln(b)
89+
auto lnX = _log(_number, _decimals + 2);
90+
auto lnB = _log(_base, _decimals + 2);
91+
auto result = divide(lnX, lnB, 0, static_cast<int>(_decimals));
92+
93+
return numUtils::roundOff(result, _decimals);
94+
}
95+
96+
std::string log10(const std::string& _number, const size_t _decimals) { return logb(_number, "10", _decimals); }
97+
98+
std::string log2(const std::string& _number, const size_t _decimals) { return logb(_number, "2", _decimals); }
99+
100+
std::string ln(const std::string& _number, const size_t _decimals) { return _log(_number, _decimals); }
101+
} // namespace steppable::__internals::arithmetic
102+
103+
#ifndef NO_MAIN
104+
int main(int _argc, const char* _argv[])
105+
{
106+
Utf8CodePage _;
107+
ProgramArgs program(_argc, _argv);
108+
program.addPosArg('c', "Command", false);
109+
program.addPosArg('n', "Number", true);
110+
program.addPosArg('b', "Base", true);
111+
program.addKeywordArg("mode", 0, "The mode to calculate in. 0 = radians (default), 1 = degrees, 2 = gradians.");
112+
program.addKeywordArg("decimals", 5, "Amount of decimals while calculating.");
113+
program.addSwitch("profile", false, "profiling the program");
114+
program.parseArgs();
115+
116+
const int mode = program.getKeywordArgument("mode");
117+
const bool profile = program.getSwitch("profile");
118+
const int decimals = program.getKeywordArgument("decimals");
119+
const auto& command = static_cast<std::string>(program.getPosArg(0));
120+
const auto& arg = static_cast<std::string>(program.getPosArg(1));
121+
const auto& base = static_cast<std::string>(program.getPosArg(2));
122+
123+
using namespace steppable::__internals;
124+
using namespace steppable::output;
125+
126+
if (command == "logb")
127+
std::cout << arithmetic::logb(arg, base, decimals) << "\n";
128+
else if (command == "log10")
129+
std::cout << arithmetic::log10(arg, decimals) << "\n";
130+
else if (command == "log2")
131+
std::cout << arithmetic::log2(arg, decimals) << "\n";
132+
else if (command == "ln")
133+
std::cout << arithmetic::ln(arg, decimals) << "\n";
134+
else
135+
{
136+
error("log"s, "Unknown command: "s + command);
137+
return EXIT_FAILURE;
138+
}
139+
}
140+
#endif

src/log/logReport.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2024 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+
23+
/**
24+
* @file logReport.cpp
25+
* @brief Not implemented.
26+
*
27+
* @author Andy Zhang
28+
* @date 23rd June 2024
29+
*/
30+
31+
#include "logReport.hpp"
32+
33+
#include <string>
34+
35+
std::string reportLog()
36+
{
37+
// Intentionally not implemented.
38+
return "";
39+
}

src/log/logReport.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2024 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+
23+
/**
24+
* @file logReport.hpp
25+
* @brief Not implemented.
26+
*
27+
* @author Andy Zhang
28+
* @date 23rd June 2024
29+
*/
30+
31+
#include <string>
32+
33+
std::string reportLog();

tests/testLog.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2024 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+
23+
#include "colors.hpp"
24+
#include "output.hpp"
25+
#include "testing.hpp"
26+
#include "util.hpp"
27+
28+
#include <iomanip>
29+
#include <iostream>
30+
31+
TEST_START()
32+
TEST_END()

0 commit comments

Comments
 (0)