-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Unexpected behavior of unit conversions inside functions #534
Comments
Thank you for reporting this. I think you ran into #336. The bottom line is this: Functions like The problem is that let q1 = 123.4 cm
let q2 = q1 -> m # the same as physical quantity as q1, just a different unit
trunc(q1) # 123 cm
trunc(q2) # 1 m = 100 cm So we rather want something like your
This has sane behavior: trunc_in(m, q1) # 1 m
trunc_in(m, q2) # 1 m
trunc_in(cm, q1) # 123 cm
trunc_in(cm, q2) # 123 cm This also works for angles: let alpha = 123.4 deg
trunc_in(deg, alpha) # 123 deg
trunc_in(rad, alpha) # 2 rad
trunc_in(mrad, alpha) # 2153 mrad What you discovered here is an interesting case though. The proposed trunc_in(1 deg, alpha) # 2 <-- WRONG You discovered this in your implementation, because you used The behavior is not really different in functions. It's rather the fact that you use an identifier on the right-hand side of
The thing here is that angles in degree "decay" to scalars. I know that this is usually not the desired behavior and maybe that should be changed. When an explicit conversion is requested (12° -> deg), we keep the unit though. But that seems to rely on whether or not the conversion target is a plain value or an identifier. I currently don't know why. The "decaying" comes from a unit-simplification feature of Numbat. We have a very simple heuristic that basically says: if the unit of a quantity can be converted to a scalar, then do so. This is very useful for more complex units, but not desirable for angle units. So what can we do here to improve the situation?
|
See #537 (comment) for an updated proposal that fixes two of the issues above. |
closed via #537 |
Description
When converting units inside a function it doesn't always behave as if done outside one. It seems that inside functions some units are simply not converted.
I have experienced this with the
Angle
dimension, specifically with thedegree
unit. It seems to me that it might have something to do with the Angle dimension being an alias forScalar
, but I have not yet tested it extensively enough to confirm this.Example
In this example is the normal behavior outside a function:
When the exact same code is moved into a function however, this is the result:
The returned value is simply a scalar as if no conversion happened.
More importantly function calls inside the function work with the unconverted value as well giving results that conflict with those it would give outside the function for the same input.
The text was updated successfully, but these errors were encountered: