Comments (6)
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.
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.
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.
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.
Hi @onkoe,
Sure, PRs are welcome. Otherwise, we may include that into the next version.
from fraction.
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)
- FromStr trait support + Parse fractions HOT 4
- How to convert to `f32` and `f64` numeric types HOT 1
- .pow() and .root() functions for fraction types HOT 3
- Serde support? HOT 2
- Fraction::from_str can fail with overflow if fractional part ends with too many zeroes HOT 3
- Weird huge values when getting the denominator of a fraction HOT 9
- Serialization support for DynaInt HOT 1
- Error: trait bounds other than `Sized` on const fn parameters are unstable HOT 2
- What does it mean that a value cannot be represented by `Ratio<T>`? HOT 2
- Fraction ceil and floor round negative fraction "the wrong way" HOT 1
- how do you conver a GenericFraction into a float? HOT 5
- Is there a list of methods that floats have that aren't impled for fractions HOT 2
- Do not simplify fractions HOT 1
- How should I handle consts? HOT 1
- Trait bounds were not satisfied with nalgebra HOT 2
- Add better unicode support (Fraction slash/division slash) HOT 8
- eligible for funding via thanks.dev HOT 2
- Completing implemention of `identities` module from `num_traits` HOT 3
- Add support for ruint? 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 fraction.