-
-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overflow with small number of multiplication #33
Comments
Hi @ChristopherRabotin, thanks for reporting that! To me this looks correct as we are relying on the num::Ratio implementation underneath, and it does not perform checked math by default (for performance reasons). In the case you reported the precision of To work with those numbers without overflows you may switch to BigDecimal or DynaDecimal. |
Thanks for your quick response. How come that number can't be represented without memory allocation? It fits as an f64, and if PI and TAU can be represented (although they are irrational), I would assume that this other number would be representable. What am I misunderstanding? I'm using fraction for high precision time computations, but the structure needs to be copy-able so I can't use BigDecimal yet. |
I think that comes to the internal representation of our Decimal implementation. Our Decimal is just a wrapper over Fraction type, which in turn is a wrapper around num::Ratio. As such, Decimals use rationals internally for the computations. I didn't manually debug that, but from what I can see, the denominators are different, so to figure out the result of the multiplication we will have to get an intermediate value of these two denoms and numerators multiplied, and those values don't fit on stack.
Well, that feels like a limitation of this library, but it needs the intermediate values of the computations to be bigger than the computation results. One potential way to work it around might be to use BigDecimal for the computation and then downcast it to the simple Decimal if the final result fits. But obviously that requires some manual juggling in the code. Sorry it's not that simple for your use-case. Also, there might be other rust libraries out there which are more optimized for decimal calculus (not using Rationals internally). |
I like the idea of using BigDecimal only for this calculation and then downcasting it. I'm using fraction for hifitime and the bug is extremely limited: if you try to convert a time to this odd time system (called Dynamic Barycentric Time, or TDB) which is within +/- 1.5 seconds from |
This is what |
I ended up implementing it this way, and it works like a charm since this function returns an f64:
|
Oh, true that's much simpler if you need f64 as the result :) |
Thanks for all your help and for being super responsive. |
cf. dnsl48/fraction#33 Signed-off-by: Christopher Rabotin <christopher.rabotin@gmail.com>
In the following example, the computation of
g_rad
will cause an overflow in either of the functions. This overflow error does not occur when compiled in release mode because rust does not do bound checking in release mode.Output in release mode:
Output in debug mode:
The text was updated successfully, but these errors were encountered: