Skip to content
New issue

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

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

Already on GitHub? # to your account

Arithmetic with references #50

Closed
jedbrown opened this issue Jan 3, 2024 · 3 comments
Closed

Arithmetic with references #50

jedbrown opened this issue Jan 3, 2024 · 3 comments

Comments

@jedbrown
Copy link
Contributor

jedbrown commented Jan 3, 2024

In various circumstances, including with iterators such as below, you're holding a reference and would like to perform arithmetic.

let velocity = [Velocity::meters_per_second(5.0); 3];
let energy = 0.5 * velocity.iter().map(|v| v * v).sum::<SpecificEnergy>();

This works in Diman if one writes the closure as |v| *v * *v, but the above works with nondimensional types and is less visually noisy. Should arithmetic (Add, Sub, Mul, Div) be implemented for all four combinations in which one or both of the args are references?

Yes, this case also works with |v| v.powi::<2>().

@Tehforsch
Copy link
Owner

This is a good idea. I think anything that works with floats and that we can support should be supported, so I'd like to add this.

@Tehforsch
Copy link
Owner

Done in #53. Your example:

let velocity = [Velocity::meters_per_second(5.0); 3];
let energy = 0.5 * velocity.iter().map(|v| v * v).sum::<SpecificEnergy>();

now compiles.

@jedbrown
Copy link
Contributor Author

jedbrown commented Jan 7, 2024

Wonderful, thanks!

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

No branches or pull requests

2 participants