FlexUnits |> Simplify: extended capability suggestion

A bit of background, I first started using MathCad around 1993. This was the way I became familiar with units aware calculation software. A big step up from spreadsheets! Let me walk you through an example. Here := is set equal to and = is show me the value of the variable.

  • In the first line I am setting torque to be 100 ft lbs.
  • When I display it, the default SI units are Joules as the units for energy and torque are the same.
  • Since I don’t like that, I edit the box to the right of the units (third line) and type in the desired unit.
  • In the 4th line I changed the units to the desired N*m
  • Similarly in the 5th line they are in*lbf
  • in the 6th line I mixed up lb(m) and lbf so it automatically inserted the SI units to give a dimensionally consistent unit output.
  • it also corrected the units for the 7th line, where I had typed in W

I am wondering if the simplify function in FlexUnits can be extended to give a similar behaviour. Something like the behaviour of the first line remains the same, but if I add units like the last line below that its behaviour mirrors the MathCad behaviour?

julia> torque = 100u"ft lbf" |> simplify
135.5817456 (N*m)

julia> torque = 100u"ft lbf" |> simplify(u"J")

Perhaps there are even better ideas out there?

The intent is to be able to display the output in my desired units, rather than base SI units or the default simplification rules. The example above shows that there are two valid ways to simplify some sets of base units.

I think this is what you want, and it already works. Just drop the simplify.

julia> using FlexUnits, .UnitRegistry

julia> torque = 100u"ft lbf" |> u"J"
135.5817456 J

Thanks, that gets a long way there. I missed that in the documentation.

I would find it useful if it did not error on incorrect units but rather displayed the requested units and added the required units to give correct results as shown below

julia> torque = 100u"ft lbf" |> u"W"
135.5817456 W s  # This would be my desired result, instead of an error

Throwing errors on incompatible unit conversions (i.e. with mismatching dimensions) is a core feature in pretty much every units package as it enforces correctness. If you’re running a long calculation, and the result cannot be converted to the desired units, it’s likely that there’s a mistake in the calculation.

In fact, when I originally made the falling object example in the documentation here, I originally forgot to divide the force by mass to get acceleration. The unitless baseline results looked weird but I didn’t want to go through everything line by line because I was going to add units anyway. When I added units, the error made the mistake obvious. Errors thrown by units packages have literally saved weeks of my life from tedious debugging. This is a major reason why I’m such a heavy user of units packages and developed my own.

The error message in your example is pretty good. It tells you that s is what’s missing from your target unit. I consider it a feature that Julia keeps you in the loop and forces an explicit correction instead of silently adjusting itself to the closest valid solution it can find.

ERROR: ConversionError: Cannot convert unit '(m² kg)/s²' to target unit 'W' due to a dimension mismatch of 's'