Code Monkey home page Code Monkey logo

Comments (4)

cuviper avatar cuviper commented on August 27, 2024 1

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.

cuviper avatar cuviper commented on August 27, 2024

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.

JulianKnodt avatar JulianKnodt commented on August 27, 2024

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.

JulianKnodt avatar JulianKnodt commented on August 27, 2024

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)

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.