You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I add units to the values I get some weird results
gmt math -Q 1 1 DIV = 1
gmt math -Q 1c 1 DIV = 1
gmt math -Q 1c 1c DIV = 2.54
gmt math -Q 1i 1i DIV = 2.54
gmt math -Q 1 1i DIV = 2.54
gmt math -Q 1 1c DIV = 6.4156 (2.54 x 2.54)
Good one. So when units are given the numbers are internally converted to inches. Then on output they are reported according to PROJ_LENGTH_UNIT [cm]. Your first example is unrelated to this since no units. So 1c/1c is dimensionless but gmtmath does not know that, so it has the result as 1 (with internal unit inch) and on output that becomes 2.54 cm. Save for 1i/1i. 1/1i is therefore the same as 1i/1i. 1/1c becomes 1i/1c and 2.54 internally (dimensionless but seen as inches) so out comes 6.4156. If I set PROJ_LENGTH_UNIT to inch and run your examples I get
gmt math -Q 1 1 DIV = 1
gmt math -Q 1c 1 DIV = 0.3937
gmt math -Q 1c 1c DIV = 1
gmt math -Q 1i 1i DIV = 1
gmt math -Q 1 1i DIV = 1
gmt math -Q 1 1c DIV = 2.54
which is better since it still converts numbers with units to internal inches, but then does not do any conversion on the output (assuming inches).
Given how gmtmath works I do not think I can easily fix this - i.e., keep track of units. Maybe document that divisions with united numbers will cause confusion. I will think about this a bit.
It would be pretty simple to turn off any unit conversion on output. This means you would get the same as I get when setting PROJ_LENGTH_UNIT to inch. I think those results are more sensible in most but not all cases. Or, we could let -Q get a small enhancement:
-Q[c|i|p|n]
Quick mode for scalar calculation. Shorthand for -Ca -N1/0 -T0/0/1. In this mode, constants may have dimension units (i.e., c, i, p) and will be converted to internal inches before computing. If one or more constants with units are encountered then the final answer will be reported in the unit set by PROJ_LENGTH_UNIT, unless overridden by appending on of c|i|p. Alternatively, append n for a non-dimensional result, meaning there is no unit conversion during output.
I think this would be a pretty good solution and documentation, @Esteban82 ?
Activity
PaulWessel commentedon Jun 19, 2021
Good one. So when units are given the numbers are internally converted to inches. Then on output they are reported according to PROJ_LENGTH_UNIT [cm]. Your first example is unrelated to this since no units. So 1c/1c is dimensionless but gmtmath does not know that, so it has the result as 1 (with internal unit inch) and on output that becomes 2.54 cm. Save for 1i/1i. 1/1i is therefore the same as 1i/1i. 1/1c becomes 1i/1c and 2.54 internally (dimensionless but seen as inches) so out comes 6.4156. If I set PROJ_LENGTH_UNIT to inch and run your examples I get
which is better since it still converts numbers with units to internal inches, but then does not do any conversion on the output (assuming inches).
Given how gmtmath works I do not think I can easily fix this - i.e., keep track of units. Maybe document that divisions with united numbers will cause confusion. I will think about this a bit.
PaulWessel commentedon Jun 19, 2021
It would be pretty simple to turn off any unit conversion on output. This means you would get the same as I get when setting PROJ_LENGTH_UNIT to inch. I think those results are more sensible in most but not all cases. Or, we could let -Q get a small enhancement:
-Q[c|i|p|n]
Quick mode for scalar calculation. Shorthand for -Ca -N1/0 -T0/0/1. In this mode, constants may have dimension units (i.e., c, i, p) and will be converted to internal inches before computing. If one or more constants with units are encountered then the final answer will be reported in the unit set by PROJ_LENGTH_UNIT, unless overridden by appending on of c|i|p. Alternatively, append n for a non-dimensional result, meaning there is no unit conversion during output.
I think this would be a pretty good solution and documentation, @Esteban82 ?
Improve gmtmath -Q behavior
Improve gmtmath -Q behavior (#5362)
PaulWessel commentedon Jul 5, 2021
Think we can close this issue now.