最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In Rust fastnum, make division yield Inf - Stack Overflow

programmeradmin3浏览0评论

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 0
Add a comment  | 

1 Answer 1

Reset to default 3

Potentially 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)
发布评论

评论列表(0)

  1. 暂无评论