#undef NDEBUG #include "Utils.hpp" #include #include #include using namespace std; static double const TEST_BASE = 0.9; double exponentiation(double base, int powerArg) noexcept { precondition(powerArg >= 0, "exponentiation() power must be non-negative, was " + to_string(powerArg)); int power = powerArg; double result = 1; while (power > 0) { result *= base; --power; } // 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. postcondition((power & 1) == 1 && (base < 0) ? result < 0 : result >= 0, "Postcondition Failed: Result is wrong sign"); return result; } int main(int argc, char **argv) { for (int p = 10; p >= -1; --p) { cout << fmt::format("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)) << endl; } return 0; }