Giter Site home page Giter Site logo

Add From for f64 and f32 about fraction HOT 6 OPEN

feefladder avatar feefladder commented on July 29, 2024
Add From for f64 and f32

from fraction.

Comments (6)

dnsl48 avatar dnsl48 commented on July 29, 2024

Hi @feefladder,

Thanks for raising this.
Unfortunately, straight conversion into floats cannot be guaranteed without precision loss.
E.g. 1/3 cannot be represented as a decimal without data loss.
The simplest lossy way to convert would be to manually divide f64::from(.numer()) / f64::from(.denom()).
A more controlled way could be via f64::from_str(format!("...", fraction));.

from fraction.

feefladder avatar feefladder commented on July 29, 2024

The conversion would also need a match statement for ±infty and Nan.

I would argue that floats are lossy by definition and converting the conceptual value of a fraction to a float means acceptig lossy conversions/operations; From the perspective of the fraction it is lossy, but from the perspective of the float it is lossless: f64::from(1/3) gives the best possible float representation. Aka 1f64/3f64 === f64::from(1/3). Compare that to f32::from(f64) not being implemented because f64 could overflow f32 (and precision loss).

Maybe as a feature called lossy-float-from? The main difference with floats and other types is that conversion cannot fail.

Okok. Just realized that bigint frac could overflow f64, but thats not the type I'm asking here :)

from fraction.

dnsl48 avatar dnsl48 commented on July 29, 2024

I agree, there could be specific lossy features that make a a lot of sense and would help :)
We already have the approx module. Probably could add more approximate conversions in there.

from fraction.

onkoe avatar onkoe commented on July 29, 2024

Hey there! I know beggars can't be choosers, but I find this crate to be way less useful without this kind of abstraction. It doesn't feel good to spin this yourself - reminds me of C... 😭

In my view, there should at least be a feature-flag'd submodule with as_f32_lossy() and as_f64_lossy() methods for these types.

Are there any implementation barriers you see for this kind of thing? If someone were to implement it, what would you want to see? And would you accept a PR?

Thanks for the useful crate! I hope to see it continue to improve. 😄

from fraction.

dnsl48 avatar dnsl48 commented on July 29, 2024

Hi @onkoe,
Sure, PRs are welcome. Otherwise, we may include that into the next version.

from fraction.

feefladder avatar feefladder commented on July 29, 2024

Yeah @onkoe should be not that much of an effort (for you :P)! There's already macros in the crate for try_into and @dnsl48 is quite supportive for getting stuff merged :)

from fraction.

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.