/****************************************************************************** * This program demonstrates fast exponentiation * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ #undef NDEBUG #include "Utils.hpp" #include #include #include using namespace Utils; using namespace std; double fast_exponentiation(double base, int power) noexcept { double result = 1; double factor = base; int p = power; while (p > 0) { if (((p & 1) == 1)) { result *= factor; } factor *= factor; p >>= 1; } return result; } int main(int argc, char **argv) { for (double base = -0.1; base < 0.9; base += 0.1) { for (int p = 0; p <= 16; ++p) { cout << fmt::format("{0:.1f}^{1:d} = {2:.16f}", base, p, fast_exponentiation(base, p)) << endl; } } return 0; }