Assertions

This page is under construction. Please come back later.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
mod utils;
const TEST_BASE:f64 = 0.9f64;
fn exponentiation(base:f64, power_arg:isize) -> f64 {
assert!(power_arg >= 0, "{}", "power_arg >= 0");
let mut power:isize = power_arg;
let mut result:f64 = 1.0f64;
while power > 0 {
result *= base;
power -= 1;
}
return result;
}
fn main() {
{
let mut p:isize = 10;
while p >= -1 {
println!("{0} ^ {1} = {2}", TEST_BASE, p, exponentiation(TEST_BASE, p));
p += -1;
}
}
}
Output
$ rustc Assertions1.rs
error: unknown format trait `f`
--> Assertions1.rs:20:31
|
20 | println!("{}", format!("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error: unknown format trait `d`
--> Assertions1.rs:20:39
|
20 | println!("{}", format!("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error: unknown format trait `f`
--> Assertions1.rs:20:47
|
20 | println!("{}", format!("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error[E0308]: mismatched types
--> Assertions1.rs:8:23
|
8 | let mut result:f64 = 1;
| --- ^
| | |
| | expected `f64`, found integer
| | help: use a float literal: `1.0`
| expected due to this
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0308`.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
mod utils;
const TEST_BASE:f64 = 0.9f64;
fn exponentiation(base:f64, power_arg:isize) -> f64 {
assert!(power_arg >= 0, "{}", String::from("exponentiation() power must be non-negative, was ") + &power_arg.to_string());
let mut power:isize = power_arg;
let mut result:f64 = 1.0f64;
while power > 0 {
result *= base;
power -= 1;
}
return result;
}
fn main() {
{
let mut p:isize = 10;
while p >= -1 {
println!("{0} ^ {1} = {2}", TEST_BASE, p, exponentiation(TEST_BASE, p));
p += -1;
}
}
}
Output
$ rustc Assertions2.rs
error: unknown format trait `f`
--> Assertions2.rs:20:31
|
20 | println!("{}", format!("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error: unknown format trait `d`
--> Assertions2.rs:20:39
|
20 | println!("{}", format!("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error: unknown format trait `f`
--> Assertions2.rs:20:47
|
20 | println!("{}", format!("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error[E0308]: mismatched types
--> Assertions2.rs:8:23
|
8 | let mut result:f64 = 1;
| --- ^
| | |
| | expected `f64`, found integer
| | help: use a float literal: `1.0`
| expected due to this
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0308`.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
mod utils;
const TEST_BASE:f64 = 0.9f64;
fn exponentiation(base:f64, power_arg:isize) -> f64 {
assert!(power_arg >= 0, "{}", String::from("exponentiation() power must be non-negative, was ") + &power_arg.to_string());
let mut power:isize = power_arg;
let mut result:f64 = 1.0f64;
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.0f64) { result < 0.0f64 } else { result >= 0.0f64 }, "{}", "Postcondition Failed: Result is wrong sign");
return result;
}
fn main() {
{
let mut p:isize = 10;
while p >= -1 {
println!("{0} ^ {1} = {2}", TEST_BASE, p, exponentiation(TEST_BASE, p));
p += -1;
}
}
}
Output
$ rustc Assertions3.rs
error: unknown format trait `f`
--> Assertions3.rs:25:31
|
25 | println!("{}", format!("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error: unknown format trait `d`
--> Assertions3.rs:25:39
|
25 | println!("{}", format!("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error: unknown format trait `f`
--> Assertions3.rs:25:47
|
25 | println!("{}", format!("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p)));
| ^
|
= note: the only appropriate formatting traits are:
- ``, which uses the `Display` trait
- `?`, which uses the `Debug` trait
- `e`, which uses the `LowerExp` trait
- `E`, which uses the `UpperExp` trait
- `o`, which uses the `Octal` trait
- `p`, which uses the `Pointer` trait
- `b`, which uses the `Binary` trait
- `x`, which uses the `LowerHex` trait
- `X`, which uses the `UpperHex` trait
error[E0308]: mismatched types
--> Assertions3.rs:8:23
|
8 | let mut result:f64 = 1;
| --- ^
| | |
| | expected `f64`, found integer
| | help: use a float literal: `1.0`
| expected due to this
error[E0308]: mismatched types
--> Assertions3.rs:17:41
|
17 | assert!(if (power & 1) == 1 && (base < 0) { result < 0 } else { result >= 0 }, "Postcondition Failed: Result is wrong sign");
| ---- ^
| | |
| | expected `f64`, found integer
| | help: use a float literal: `0.0`
| expected because this is `f64`
error[E0308]: mismatched types
--> Assertions3.rs:17:55
|
17 | assert!(if (power & 1) == 1 && (base < 0) { result < 0 } else { result >= 0 }, "Postcondition Failed: Result is wrong sign");
| ------ ^
| | |
| | expected `f64`, found integer
| | help: use a float literal: `0.0`
| expected because this is `f64`
error[E0308]: mismatched types
--> Assertions3.rs:17:76
|
17 | assert!(if (power & 1) == 1 && (base < 0) { result < 0 } else { result >= 0 }, "Postcondition Failed: Result is wrong sign");
| ------ ^
| | |
| | expected `f64`, found integer
| | help: use a float literal: `0.0`
| expected because this is `f64`
error: aborting due to 7 previous errors
For more information about this error, try `rustc --explain E0308`.
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Base Conversion
- Fast Exponentiation
- Fibonacci Numbers with Memoization
- Football Passer Rating (with Assertions)
- Julian Day Number
- Number of Primes (Estimate)
- Ordinal Date
- Ordinal Date (Revisited)
- Shuffle Deck
- Significant Digits
References
- [[Rust Language Reference]]
- [[Rust Compiler]]
Pure Programmer


