mamrhein / fpdec.rs

Fast implementation of Decimal fixed-point arithmetics
BSD 3-Clause "New" or "Revised" License
17 stars 4 forks source link

Unexpected subtract with overflow panic in `<Decimal as TryInto<f64>>::try_into()` impl #12

Closed MathisWellmann closed 9 months ago

MathisWellmann commented 10 months ago

Came across a panic:

---- into_float::tests_into_f32::test_subtract_with_overflow_issue stdout ----
thread 'into_float::tests_into_f32::test_subtract_with_overflow_issue' panicked at src/into_float.rs:16:5:
attempt to subtract with overflow

I've added a test that highlights the panic. It originates in the n_signif_bits function and the overflow should be bubbled up to the caller to allow him to properly handle it. I have a feeling that it has to do with the value being so tiny (or having so many digits after the dot). Not sure what the best way to address this is, but just wanted to share it. Thanks for all the good work done so far, one of my favorite workhorse crates :100:

mamrhein commented 9 months ago

The panic happens when you try to convert a Decimal with absolute value less than 1 and significant bits larger than the number of mantissa bits of the target type. This is clearly a bug. Bubbling the panic up to the caller is not an option in this case. Resolving this issue is not trivial. I have to find a way to avoid rounding twice. I've created issue #13 for this. I will take over your test case, slightly modified to have the same naming and structure as the other test cases.

mamrhein commented 9 months ago

Underlying bug to be handled via issue #13.