sulami / literate-calc-mode.el

🧮 Literate programming for M-x calc
https://melpa.org/#/literate-calc-mode
GNU General Public License v3.0
325 stars 13 forks source link

Unit simplification in results #45

Open jwiegley opened 2 months ago

jwiegley commented 2 months ago

Hello! Great mode, it could replace all my uses of the Soulver app on macOS.

One question I have: I made the alias for using uconv to do unit conversions, but it's not working as I would expect. For example, let's say I do the following in calc:

'120 gal
'794 ft^2
/                 => 1:  0.151133501259 gal / ft^2
u c in            => 1:  0.242443324937 in

This is great. But in literate-calc-mode I have:

flow = 120 gal
surface_area = 794 ft^2
loss_by_flow = flow / surface_area => loss_by_flow: 0.151133501259 gal / ft^2
result = uconv(loss_by_flow, in) => result: 1.04953820319e-3 gal / in^2

This is not the answer I would have expected, although it's correct in some sense. Why is it only applying the unit conversion to the denominator here, and not to the whole result?

jwiegley commented 2 months ago

Where uconv is defined using (defalias 'calcFunc-uconv 'math-convert-units).

sulami commented 2 months ago

From playing around with it for a bit, I don't think it's a division thing, for example

flow = 120 L => flow: 120 L
surface_area = 794 m^2 => surface_area: 794 m^2
loss_by_flow = flow / surface_area => loss_by_flow: 0.151133501259 L / m^2
result = uconv(loss_by_flow, gal) => result: 0.0399252472077 gal / m^2

converts only the numerator instead. My suspicion is that in^2/m^2 aren't actually recognized as units of volume for some reason. Similarly, usimplify doesn't seem to reduce the fraction either.

As a counterexample, this works as intended:

f = 150 g * 3 m/s^2 => f: 450 g m / s^2
= uconv(f, N) => 0.45 N