mod utils; static TEST_BASE:f64 = 0.9f64; fn exponentiation(base:f64, powerArg:isize) -> f64 { assert!(powerArg >= 0, String::from("exponentiation() power must be non-negative, was ") + &powerArg.to_string()); let mut power:isize = powerArg; let mut result:f64 = 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!(if (power & 1) == 1 && (base < 0) { result < 0 } else { result >= 0 }, "Postcondition Failed: Result is wrong sign"); return result; } fn main() { { let mut p:isize = 10; while p >= -1 { println!("{}", format!("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p))); p += -1; } } }