pub fn py_round(x: f64) -> i64 {
let f = x.floor();
let diff = x - f;
let r = if diff < 0.5 {
f
} else if diff > 0.5 {
f + 1.0
} else {
if (f as i64) % 2 == 0 {
f
} else {
f + 1.0
}
};
r as i64
}
#[cfg(test)]
mod tests {
use super::py_round;
#[test]
fn matches_python_round_half_to_even() {
assert_eq!(py_round(0.5), 0);
assert_eq!(py_round(1.5), 2);
assert_eq!(py_round(2.5), 2);
assert_eq!(py_round(3.5), 4);
assert_eq!(py_round(-0.5), 0);
assert_eq!(py_round(-1.5), -2);
assert_eq!(py_round(-2.5), -2);
assert_eq!(py_round(0.49), 0);
assert_eq!(py_round(0.51), 1);
assert_eq!(py_round(2.4), 2);
assert_eq!(py_round(2.6), 3);
assert_eq!(py_round(-2.6), -3);
assert_eq!(py_round(10.0), 10);
}
}