fix neg slope calculated value

This commit is contained in:
Pascal Seitz
2022-08-23 13:42:09 +02:00
parent 050fc5dde9
commit 193a3c21f4

View File

@@ -121,8 +121,12 @@ fn diff(val1: u64, val2: u64) -> f64 {
}
#[inline]
pub(crate) fn get_calculated_value(first_val: u64, pos: u64, slope: f32) -> u64 {
first_val + (pos as f32 * slope) as u64
fn get_calculated_value(first_val: u64, pos: u64, slope: f32) -> u64 {
if slope < 0.0 {
first_val - (pos as f32 * -slope) as u64
} else {
first_val + (pos as f32 * slope) as u64
}
}
impl FastFieldCodecSerializer for LinearInterpolFastFieldSerializer {
@@ -261,6 +265,26 @@ mod tests {
>(data, name)
}
#[test]
fn get_calculated_value_test() {
// pos slope
assert_eq!(get_calculated_value(100, 10, 5.0), 150);
// neg slope
assert_eq!(get_calculated_value(100, 10, -5.0), 50);
// pos slope, very high values
assert_eq!(
get_calculated_value(i64::MAX as u64, 10, 5.0),
i64::MAX as u64 + 50
);
// neg slope, very high values
assert_eq!(
get_calculated_value(i64::MAX as u64, 10, -5.0),
i64::MAX as u64 - 50
);
}
#[test]
fn test_compression() {
let data = (10..=6_000_u64).collect::<Vec<_>>();