/****************************************************************************** * This program demonstrates fast exponentiation * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ import org.pureprogrammer.Utils; public class FastExponentiation { static double fastExponentiation(double base, int power) { double result = 1; double factor = base; int p = power; while (p > 0) { if (((p & 1) == 1)) { result *= factor; } factor *= factor; p >>= 1; } return result; } public static void main(String[] args) { for (double base = -0.1; base < 0.9; base += 0.1) { for (int p = 0; p <= 16; ++p) { System.out.println(Utils.format("{0:.1f}^{1:d} = {2:.16f}", base, p, fastExponentiation(base, p))); } } } }