Coder Social home page Coder Social logo

Comments (5)

paupino avatar paupino commented on May 27, 2024

I've taken a further look and am going to change approach slightly. Returning the overflowing bit is relatively easy however there is not much that can be done with it. I think a much more gracious solution is to round the number upon rescaling - namely because it can't be represented in greater detail. In the example above, 0.6386554621848739495798319328 should be scaled down to 0.638655462184873949579831933 since the alternative can't be scaled up without overflowing giving a final result of 12.453781512605042016806722690. This seems consistent with other libraries.

from rust-decimal.

rkusa avatar rkusa commented on May 27, 2024

I think a much more gracious solution is to round the number upon rescaling - namely because it can't be represented in greater detail. In the example above, 0.6386554621848739495798319328 should be scaled down to 0.638655462184873949579831933 since the alternative can't be scaled up without overflowing giving a final result of 12.453781512605042016806722690.

Sounds good! Thanks @paupino for having a look and also for providing details and reasoning 👍

from rust-decimal.

rkusa avatar rkusa commented on May 27, 2024

Out of curiosity, for a consistent behavior, couldn't you round to the max possible precision in the multiplication case as well, instead of giving a panic?

rust-decimal/src/decimal.rs

Lines 1811 to 1817 in 7a3728a

if final_scale > MAX_PRECISION {
panic!(
"Multiplication underflowed: {} > {}",
final_scale,
MAX_PRECISION
);
}

(I've already encountered this panic multiple times)

from rust-decimal.

paupino avatar paupino commented on May 27, 2024

Yes; good catch. Let’s open a new ticket for this case and I’ll see if I can get to it today - I believe the fix should be relatively straight forward. If possible, can you provide some failure cases for this?

from rust-decimal.

paupino avatar paupino commented on May 27, 2024

Issue created for the multiplication case at #96.

from rust-decimal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.