Comments (4)
If it is a float that it is being cast to, I don't want to do any rounding.
This isn't generally possible to guarantee either. f64
to f32
definitely has to round, but even integer to float will round if the value is greater than the mantissa bits can represent.
In any case, it's not possible to control rounding with the current API. A new rounding API would need to be proposed.
from num-traits.
Doc improvements are welcome in PRs.
it may be worth letting the caller dictate the rounding behavior,
That sounds like a whole new API! Maybe you could implement your own wrapper with different floating point behavior, and I found an example of that in a different crate with az::Round
. Another option I found is numeric_cast
with multiple traits available.
from num-traits.
I'm currently working with num_traits
inside of a crate I don't own (see linked PR), and it is using ToPrimitive
in order to cast between f32 to either an integer or a float (specified only as an impl Primitive
). If it is a float that it is being cast to, I don't want to do any rounding. While the library could change to use a different trait for their numeric types, it's a bit invasive of a change, and it feels natural that conversion between primitives should be able to control their rounding mode.
For the specific issue, there's workarounds, but I think it was worth asking here since it seems to be the most natural place to change it. If you don't think it should be added though, that's fine
from num-traits.
ah yes, currently I'm doing f32 -> Generic
which would not require rounding.
Sounds good. I don't think I'll propose such an API myself, so for now I'll just close this.
from num-traits.
Related Issues (20)
- `CheckedAdd` should probably not extend `Add` HOT 3
- Why not make zero() and one() as const functions? HOT 2
- Add trigonometry functions HOT 1
- constifying traits in an nighlty branch or feature gate it? (with #[const_trait]) HOT 2
- `cargo doc` fails if they run it with nightly Rust HOT 2
- New nightly version broke `cargo-doc`, unintended use of unstable feature HOT 3
- Is there any hope of seeing a minor release anytime soon? HOT 1
- add a quotien_and_rem_euclid method to Euclid trait HOT 1
- Feature request: OverflowingShl HOT 2
- Feature request: can we have a trait for checked_add_signed uX implementations?
- Add `BitOrAssign` etc. to `PrimInt` HOT 4
- Introduce derive macros HOT 1
- Adding {ilog, ilog2, ilog10} to PrimInt HOT 1
- Add `trait UnsignedAbs` HOT 3
- feat: ToPrimitive split into ToFloatPrimitive and ToIntegerPrimitive HOT 7
- Add `round_to_digit` method to `Float` trait. HOT 2
- `struct` built on top of `pub trait Num` can't compile if `pub trait Num` doesn't impl `Debug` HOT 4
- Add `Hash` as a trait bound for `PrimInt` HOT 2
- Add is_signed HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from num-traits.