Skip to content
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

Support for specifying max number of decimals #124

Open
rellfy opened this issue Feb 23, 2024 · 3 comments
Open

Support for specifying max number of decimals #124

rellfy opened this issue Feb 23, 2024 · 3 comments

Comments

@rellfy
Copy link

rellfy commented Feb 23, 2024

My use case requires a maximum number of decimals for all numbers.

I am not aware of a setting to enforce that globally at the moment, so the only way to achieve that is to always call .with_scale(x) after every operation which is very verbose and error-prone.

@akubera
Copy link
Owner

akubera commented Mar 6, 2024

There's a bunch of work-in-progress branches for adding "Context support" to the arithmetic operations, but they're taking a while.

// keep operation results to 25 digits 
let ctx = Context::new().with_precision(25).unwrap();

let c = ctx.add(a, b);
let z = ctx.mul(x, y);

Is that what you're looking for?

Until those are done you'll have to use the with_scale() methods to trim them yourself.

@rellfy
Copy link
Author

rellfy commented Mar 6, 2024

@akubera thanks for the response.

I actually specifically need to set the scale, not the precision.
But that would work yes (assuming I can set the scale on the context), but even better would be a global max_scale setting which would apply to all BigDecimals by default.

I have a fork where I did some work on that, and it works for divisions but it's failing for multiplications so I need to finish the implementation. Happy to open a PR if it's something that you would be open to merging eventually.

@akubera
Copy link
Owner

akubera commented Mar 6, 2024

Well in that case I wonder what you think about adding the ability to use precision or a fixed (max) scale in the Context.

Right now it's just the total precision

pub struct Context {
    /// total number of digits
    precision: NonZeroU64,
    /// how to round
    rounding: RoundingMode,
}

But we could do something like:

enum DigitLimiter {
  /// total number of digits
  Precision(NonZeroU64),
  /// Max number of digits after the decimal point
  MaxScale(i64),
}

pub struct Context {
    /// total number of digits
    digit_limit: DigitLimiter,
    /// how to round
    rounding: RoundingMode,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants