#undef NDEBUG #include "Utils.hpp" #include #include #include using namespace Utils; using namespace std; static double const TEST_BASE = 0.9; double exponentiation(double base, int power_arg) noexcept { precondition(power_arg >= 0, string("exponentiation() power must be non-negative, was ") + to_string(power_arg)); int power = power_arg; double result = 1.0; 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.0) ? result < 0.0 : result >= 0.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; }