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

Failure to simplify units in very simple case #303

Open
mattfbacon opened this issue Jul 1, 2024 · 3 comments
Open

Failure to simplify units in very simple case #303

mattfbacon opened this issue Jul 1, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@mattfbacon
Copy link

> s = 34 gallons to L
128.704000656 L
> k = 10.7 kwh / L
10.7 kWh / L
> s * k
1377.1328070192 L kWh / L
@printfn
Copy link
Owner

printfn commented Jul 1, 2024

Thanks for the report. The reason this fails to simplify is that because you're first converting s explicitly to a specific unit, fend tracks that s should not be auto-simplified further. You can see this by running @debug s, which shows not simplifiable as one of the flags. This flag is set whenever you use to to explicitly convert to a specific unit.

Multiplying s with another value carries over this not simplifiable flag, causing fend to not simplify s * k.

If you instead calculate e.g.:

> s = 34 gallons
34 gallons
> k = 10.7 kwh / L
10.7 kWh / L
> s * k
1377.1328070192 kWh

fend manages to correctly simplify the units.

I'm not sure how to best fix this. One option would be to automatically drop the flag when the units change due to multiplication or such, but I'll need to carefully test to make sure this doesn't cause unintentional regressions.

Also note that flags are only carried over from the value on the left-hand side, so k * s does simplify correctly.

@printfn printfn added the bug Something isn't working label Jul 1, 2024
@mattfbacon
Copy link
Author

I think it would be appropriate to simplify it anyway if the unit is exactly the same, i.e., not not just the same quantity but also the same actual unit. Also, what is the intended value of this "not simplifiable" flag and is it possible to disable with config?

@printfn
Copy link
Owner

printfn commented Jul 2, 2024

The flag is important because it prevents fend from auto-simplifying any calculations that are explicitly converted to a specific unit with to.

For example:

> 4 watts * 3 seconds
12 joules
> 4 watts * 3 seconds to N m
12 N m

It makes it possible to use units like Newton-metres without having them be auto-converted to joules.

But anyway, reenabling auto-conversions after multiplying values (or doing other maths operations) does make sense I think, to make sure that calculations like (34 gallons to L) * (10.7 kwh / L) work properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants