Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/dec128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,18 @@ impl d128 {
d128::with_context(|ctx| unsafe { *decQuadQuantize(&mut self, &self, other.as_ref(), ctx) })
}

/// Same as the `quantize` function but allows a rounding mode to be specified.
pub fn quantize_with_mode<O: AsRef<d128>>(mut self, other: O, mut rounding: Rounding) -> d128 {
use std::mem;

d128::with_context(|ctx| {
mem::swap(&mut ctx.rounding, &mut rounding);
let result = unsafe { *decQuadQuantize(&mut self, &self, other.as_ref(), ctx) };
ctx.rounding = rounding;
result
})
}

/// Returns a copy of `self` with its coefficient reduced to its shortest possible form without
/// changing the value of the result. This removes all possible trailing zeros from the
/// coefficient (some may remain when the number is very close to the most positive or most
Expand Down Expand Up @@ -1093,4 +1105,13 @@ mod tests {
assert_eq!(d128::from_str(&(::std::u64::MIN).to_string()).unwrap(),
d128::from(::std::u64::MIN));
}

#[test]
fn quantize_rounding() {
assert_eq!(d128!(2.0), d128!(1.5).quantize_with_mode(d128!(1), Rounding::HalfUp));
assert_eq!(d128!(3.0), d128!(2.5).quantize_with_mode(d128!(1), Rounding::HalfUp));

assert_eq!(d128!(2.0), d128!(2.9).quantize_with_mode(d128!(1), Rounding::Floor));
assert_eq!(d128!(3.0), d128!(2.1).quantize_with_mode(d128!(1), Rounding::Ceiling));
}
}