dnsl48 / fraction

[Rust] Lossless fractions and decimals; drop-in float replacement
Apache License 2.0
83 stars 25 forks source link

panic: attempt to multiply with overflow #15

Closed lovebug356 closed 5 years ago

lovebug356 commented 5 years ago

I get a panic when I run the following code (with fraction=0.6.2):

use fraction::Decimal;

fn main() {
    let d1 = Decimal::from("0.0002") * Decimal::from("9876.54321");
    let d2 = Decimal::from("51.53023471399515");
    let d3 = d1 + d2;

    println!("{:?} + {:?} = {:?}", d1, d2, d3);
}
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/fraction-test`
thread 'main' panicked at 'attempt to multiply with overflow', /rustc/eae3437dfe991621e8afdc82734f4a172d7ddf9b/src/libcore/ops/arith.rs:318:45
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
dnsl48 commented 5 years ago

Hi @lovebug356. Yes, by default there are no overflow checks performed so that there are no performance penalties for when it's not required. If you expect there may be huge numbers, you can try either of these

dnsl48 commented 5 years ago

If it doesn't solve your problem or you have any other questions, please feel free to reopen this issue or create a new on.

lovebug356 commented 5 years ago

Thanks for the feedback! I will try the other types.