dnsl48 / fraction

[Rust] Lossless fractions and decimals; drop-in float replacement
Apache License 2.0
83 stars 25 forks source link

Do not simplify fractions #94

Closed AngeloFrangione closed 10 months ago

AngeloFrangione commented 1 year ago

Hi, I am encountering a problem with the Fraction crate where the simplification of the fraction causes a loss in data.

Here is the explanation: I need to keep track of a percentage over time. The percentage is for example Bad apples out of the total apples. So i can keep track of the percentage of bad apples. Let's say we iterate over the apple for each bad apple i increment the denominator AND the nominator. if we start with a bad apple it is 1/1 so 100%, next round a good apple, so i increment only the denominator: 1/2 so 50%. Now we start with a good apple. We have 0/1 so 0%. And if we have a second good apple after that. I still increment only the denominator. This makes 0/2. but it is simplified with 0/1 and i just lost track of how much apples are bad.

I hope you understand what i mean and that it is relevant.

Thank you

dnsl48 commented 1 year ago

Hi @AngeloFrangione, Sorry to hear you got a tough challenge. I would consider using a different way of preserving the data about the numbers of bad and good apples. I don't think you could use numerator/denominator for that. I would recommend you keeping the original numbers separately and only convert into a fraction when you need to find the ratio.