Skip to content

Bug in gmtmath when using values with units #5348

Closed

Description

@Esteban82

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)

System information

  • Operating system: Win64
  • GMT 6.2

Activity

  1. added
    bug
    on Jun 17, 2021
  2. PaulWessel commented on Jun 19, 2021

    @PaulWessel
    Contributor

    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.

  3. PaulWessel commented on Jun 19, 2021

    @PaulWessel
    Contributor

    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 ?

  4. added a commit that references this issue on Jun 19, 2021
  5. added a commit that references this issue on Jun 21, 2021
  6. PaulWessel commented on Jul 5, 2021

    @PaulWessel
    Contributor

    Think we can close this issue now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions