In the Rust fastnum high precision decimal floating-point package, dividing by zero panics by default. Fair enough.
But fastnum does specifically list among its features, support for full floating-point semantics including +/- Inf: /
How do you get division to yield Inf instead of panic? The documentation doesn't say.
In the Rust fastnum high precision decimal floating-point package, dividing by zero panics by default. Fair enough.
But fastnum does specifically list among its features, support for full floating-point semantics including +/- Inf: https://www.reddit/r/rust/comments/1hk61om/announcing_a_new_fast_exact_precision_decimal/
How do you get division to yield Inf instead of panic? The documentation doesn't say.
Share Improve this question asked Mar 9 at 16:30 rwallacerwallace 33.7k45 gold badges134 silver badges281 bronze badges 01 Answer
Reset to default 3Potentially erroneous cases trigger Signals
which are handled by the Context
that is bound to each decimal value. The default context doesn't allow div-by-zero. And it appears both division operands need to allow div-by-zero to avoid a panic.
Here is a non-panicking sample:
use fastnum::decimal::Context;
use fastnum::udec256;
fn main() {
let allow_divzero = Context::default().without_traps();
let a = udec256!(1.5).with_ctx(allow_divzero);
let b = udec256!(0.0).with_ctx(allow_divzero);
let c = a / b;
dbg!(c);
}
[src\main.rs:12:5] c = UD256(Inf)