From 40d362f4a33b59bd8bf26efd4910205123bee949 Mon Sep 17 00:00:00 2001 From: Andy Zhang Date: Sun, 16 Jun 2024 08:50:27 +0800 Subject: [PATCH] Added other inverse functions. --- .github/workflows/cmake-multi-platform.yml | 6 +- src/trig/trig.cpp | 66 +++++++++++++++++++++- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 50f8c8ec1..d9c093b75 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -25,12 +25,12 @@ jobs: # # To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list. matrix: - os: [ubuntu-latest, windows-latest, macos-12, macos-13, macos-14] + os: [ubuntu-latest, windows-2019, macos-12, macos-13, macos-14] build_type: [Release] c_compiler: [clang, cl] python_version: ['3.10', '3.11', '3.12'] include: - - os: windows-latest + - os: windows-2019 c_compiler: cl cpp_compiler: cl cmake_extra_options: '' @@ -54,7 +54,7 @@ jobs: cpp_compiler: clang++ cmake_extra_options: '-GNinja' exclude: - - os: windows-latest + - os: windows-2019 c_compiler: clang - os: ubuntu-latest c_compiler: cl diff --git a/src/trig/trig.cpp b/src/trig/trig.cpp index 035b3acde..c3cbfd7d6 100644 --- a/src/trig/trig.cpp +++ b/src/trig/trig.cpp @@ -36,6 +36,7 @@ #include "util.hpp" #include +#include #include using namespace std::literals; @@ -380,7 +381,59 @@ namespace steppable::__internals::arithmetic return result; } - // std::string acos(const std::string& x, const int decimals, const int mode) {} + std::string acos(const std::string& x, const int decimals, const int mode) + { + std::string circleAngle; + switch (mode) + { + case 1: + circleAngle = "180"; + break; + case 2: + circleAngle = "200"; + break; + default: + circleAngle = static_cast(constants::PI); + } + + // pi + // acos(x) = ----- - asin(x) + // 2 + return subtract(circleAngle, asin(x, decimals, mode), 0); + } + + std::string asec(const std::string& x, const int decimals, const int mode) + { + if (compare(abs(x, 0), "1", 0) != "0") + error("trig::asec"s, "Arc secant is not defined here."s); + + // 1 + // asec(x) = acos(---) + // x + return acos(divide("1", x, 0, decimals), decimals, mode); + } + + std::string acsc(const std::string& x, const int decimals, const int mode) + { + if (compare(abs(x, 0), "1", 0) != "0") + error("trig::acsc"s, "Arc cosecant is not defined here."s); + + // 1 + // acsc(x) = asin(---) + // x + return asin(divide("1", x, 0, decimals), decimals, mode); + } + + std::string acot(const std::string& x, const int decimals, const int mode) + { + if (compare(abs(x, 0), "1", 0) != "0") + error("trig::acot"s, "Arc cotangent is not defined here."s); + + // 1 + // acot(x) = atan(---) + // x + return atan(divide("1", x, 0, decimals), decimals, mode); + } } // namespace steppable::__internals::arithmetic #ifndef NO_MAIN @@ -405,28 +458,39 @@ int main(int _argc, const char* _argv[]) std::function function; + // Basic trigonometric functions if (command == "sin") function = arithmetic::sin; else if (command == "cos") function = arithmetic::cos; else if (command == "tan") function = arithmetic::tan; + // Reciprocal trigonometric functions else if (command == "csc") function = arithmetic::csc; else if (command == "sec") function = arithmetic::sec; else if (command == "cot") function = arithmetic::cot; + // Inverse trigonometric functions else if (command == "atan") function = arithmetic::atan; else if (command == "asin") function = arithmetic::asin; else if (command == "acos") function = arithmetic::acos; + else if (command == "asec") + function = arithmetic::asec; + else if (command == "acsc") + function = arithmetic::acsc; + else if (command == "acot") + function = arithmetic::acot; + // Invalid command else { error("trig::main", "Invalid command."s); return EXIT_FAILURE; } + std::cout << function(arg, decimals, mode) << '\n'; } #endif