Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <algorithm>
- #include <cmath>
- #if defined(__cpp_lib_math_constants)
- # include <numbers>
- #endif
- // π(n) = n . sin(180 / n)
- #define FUNCTIONAL_
- #if defined(__cpp_lib_math_constants)
- constexpr
- double pi = std::numbers::pi_v<double>;
- #elif defined(M_PI)
- constexpr
- double pi = M_PI;
- #else
- constexpr
- double pi = 2.0 * std::acos(0.0);
- #endif
- auto to_radian = [](double angle) { return angle * (pi / 180.0); };
- auto poly_pi_l = [](long sides) {
- return (1.0 * sides) * std::sin(to_radian(180.0) / (1.0 * sides));
- };
- auto poly_pi = [](long sides) { return (1.0 * sides) * std::sin(pi / (1.0 * sides)); };
- int main() {
- std::cout << "C++ Version: " << __cplusplus << '\n';
- #if defined(__cpp_lib_math_constants)
- std::cout << "C++ Math Constants Version: " << __cpp_lib_math_constants << '\n';
- #endif
- std::cout << std::fixed << std::setprecision(12);
- std::cout << "π: " << pi << '\n';
- auto polygons = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 50, 100, 200, 500,
- 1'000, 5'000,
- 10'000, 20'000,
- 100'000, 200'000,
- 200'000, 400'000,
- 1'000'000, 2'000'000, 3'000'000,
- 4'000'000, 4'100'000, 4'200'000,
- 10'000'000, 20'000'000, 25'000'000, };
- #ifndef FUNCTIONAL_
- std::cout << std::string(60, '=') << std::endl;
- for (auto side : polygons) {
- std::cout << std::setw(12) << side << " : "
- << std::setw(15) << poly_pi(side)
- << " - "
- << std::setw(15) << poly_pi_l(side)
- << '\n';
- }
- #else
- std::cout << std::string(60, '.') << std::endl;
- std::for_each(polygons.begin(), polygons.end(), [](auto const side) {
- std::cout << std::setw(12) << side << " : "
- << std::setw(15) << poly_pi(side)
- << " - "
- << std::setw(15) << poly_pi_l(side)
- << '\n';
- });
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement