Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing implementation of product for Ratio #404

Closed
JuliusHerrmann opened this issue Feb 1, 2023 · 1 comment
Closed

Missing implementation of product for Ratio #404

JuliusHerrmann opened this issue Feb 1, 2023 · 1 comment

Comments

@JuliusHerrmann
Copy link

Hi everyone,
this is my first contribution/issue so please correct me on anything :).
During a university course I came across the problem of calculating the product of an iterator of Ratios.
This is an example that would not work but I think should be possible:

extern crate uom;

use uom::si::ratio::percent;
use uom::si::rational64::Ratio;

fn test_uom() -> Ratio {
    let ratios = vec![Ratio::new::<percent>(100.into()), Ratio::new::<percent>(200.into())];
    ratios.into_iter().product()
}

I searched around a bit and came across the "num" crate. This implements Ratio and product of ratios, so I think it should be possible for "uom" to do the same.
This here works in the "num" crate:

use num::rational::Ratio;

fn test_num() -> num::rational::Ratio<usize> {
    let ratios = vec![Ratio::from_integer(1), Ratio::from_integer(2)];
    ratios.into_iter().product()
}

In the end I used

ratios.into_iter().fold(
    Ratio::new::<percent>(100.into()),
    |acc, x| acc * x
)

which worked. Which leads me to the conclusion that there is no fundamental problem hindering the support of product on ratios.

To sum it up I think product over ratios should/could be supported.
Thanks :)

@iliekturtles
Copy link
Owner

Thanks for the issue, however this functionality was removed in #15 as it doesn't work in the general case! While the product of ratios is still a ratio the product of other quantities is not that same quantity. e.g. Length * length = area. Length * length * length = volume. Product requires that all items are of the same type and the result is also that same type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants