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

Adds truncate, half-up rounding functions #47

Open
wants to merge 80 commits into
base: master
Choose a base branch
from

Conversation

jonathanstrong
Copy link
Collaborator

This is an alternative approach to allowing the user to specify a mode for a closure, namely, providing two specific and commonly used rounding functions in a quantize-like interface.

This commit adds

  • A binding to the decContextSetRounding function
  • A d128 method, with_rounding to initialize a Context with a given
    Rounding
  • ROUND_DOWN and HALF_UP thread local static Contexts initialized
    with those roundings
  • Two methods on d128, with_round_down and with_half_up that mimic
    with_context but use alternative roundings
  • Two methods, truncate and round that offer quantize like
    interfaces to these rounding settings.

Jonathan Strong added 30 commits June 22, 2017 00:18
merging some tiny upstream updates to try to eliminate a compiler
warning.
- removed unsafe designation for `from_raw_bytes` because otherwise
every use of the d128! plugin macro would need an unsafe.
- macro can be used to create a `const A: d128 = d128!(1);`
- decimal-macros has basic tests but they don't run when `cargo test` is
called from the root project dir (e.g. ~/src/decimal). You need to
navigate up to the decimal-macros dir to run them.
This adds

- A binding to the `decContextSetRounding` function
- A d128 method, `with_rounding` to initialize a `Context` with a given
`Rounding`
- `ROUND_DOWN` and `HALF_UP` thread local static `Context`s initialized
with those roundings
- Two methods on d128, `with_round_down` and `with_half_up` that mimic
`with_context` but use alternative roundings
- Two methods, `truncate` and `round` that offer `quantize` like
interfaces to these rounding settings.
This allows d128 to be used as a key-value pair via the slog logging
macros, e.g.

```
let x = d128::zero();
info!(logger, "something important"; "x" => x);
```
@alkis
Copy link
Owner

alkis commented Oct 1, 2020

@jonathanstrong do you want to make this merge cleanly so that it can be merged? Then we can push a new version out.

@jonathanstrong
Copy link
Collaborator Author

you're interested in the code that fixes the uninitialized usage, correct? happy to setup a clean pull request with those changes if so.

@alkis
Copy link
Owner

alkis commented Oct 2, 2020

I think we can merge the whole thing. I also sent you an invite to commit directly if that helps.

@jonathanstrong
Copy link
Collaborator Author

Are you sure? There are quite a few unrelated differences, including a d64 type, some From and TryFrom conversion routines that should probably be reviewed, some dependencies brought in for benchmarking that are probably not a good idea to include in the upstream repo, etc.

@alkis
Copy link
Owner

alkis commented Oct 2, 2020

If you are happy to do it in smaller steps that would be even better.

@jonathanstrong
Copy link
Collaborator Author

jonathanstrong commented Oct 2, 2020

ok, I pushed a branch to upstream (i.e. this repo) as I am now a collaborator (thank you). it is only the MaybeUninit conversion changes: https://github.com/alkis/decimal/compare/uninit-fix. next week I can setup pull requests for the other things. if you have the bandwidth it'd be nice to get a second set of eyes on the changes (the stuff other than MaybeUninit). I am pretty good at rust but not experienced at C.

@alkis
Copy link
Owner

alkis commented Oct 2, 2020

Will do. Thanks @jonathanstrong!

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

Successfully merging this pull request may close these issues.

None yet

3 participants