#!/usr/bin/env python3; import Utils TEST_BASE = 0.9 def exponentiation(base, powerArg) : assert powerArg >= 0, "exponentiation() power must be non-negative, was " + str(powerArg) power = powerArg result = 1 while (power > 0) : result *= base power -= 1 # This line will make the result incorrect. result = -result # If power is odd and base is negative, then result should be negative. # Otherwise result should be positive. assert result < 0 if (Utils.bitwiseAnd32(power, 1)) == 1 and (base < 0) else result >= 0, "Postcondition Failed: Result is wrong sign" return result # Begin Main for p in range(10, -1 + -1, -1) : print("{0:f} ^ {1:d} = {2:f}".format(TEST_BASE, p, exponentiation(TEST_BASE, p)))