|
| 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 |
0 commit comments