View Ticket
Not logged in
Ticket UUID: ef5373e6fa0617ee02f201c6c8f41746678c5afb
Title: expr, literal numeric values, and string comparisons
Type: Bug Version: 8.5
Submitter: pooryorick Created on: 2016-10-03 22:17:53
Subsystem: 16. Commands A-H Assigned To: dgp
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2024-10-11 18:36:41
Resolution: Invalid Closed By: pooryorick
    Closed on: 2024-10-11 18:36:41
Description:

The documentation for expr provides seven rules for operand syntax. Of those, the value 0x12 matches rule 1, and only rule 1: "... a numeric value, either integer or floating-point". Therefore, 0x12 is unambiguously a numeric value. The string representation of 0x12 is 18, as illustrated by the result of [expr 0x12]. Furthermore, the documentation states that operands that are not numeric or boolean must be quoted. It's well-accepted that only numeric operands can appear unquoted in an expression. Finally, the documentation states that in order to compare a numeric operand to an operand that can't be interpreted as numeric, the string representation for the numeric operand is generated. Consider

expr {{0y} > 0x12}

. Since 0x12 is numeric, and its string representation is 18, the previous command should produce the same result as

expr {{0y} > {18}}

. However, this isn't the case. The result of the first command is 1, and the result of the second is 0.

User Comments: pooryorick added on 2024-10-11 18:36:41:

The real answer here is that if [expr] *would* interpret 0x12 as a number if the operation made sense with a numeric operand, but because the other operand is a string, not a number, the only sensible thing that remains is to treat 0x12 as a string. In fact in both of the given examples, string comparison, not numeric comparison is performed: 18 is also interpreted as a string, not a number.


oehhar added on 2016-10-31 17:34:20:

The issue of the argumentation is, that the string representation of 0x12 is 0x12 and not 18. "18" is the result of a conversion to decimal 0x 0x12.

As a consequence, a comparison with the string "0y" once results in true, once in false which is correct IMHO.

Thank you, Harald


dgp added on 2016-10-04 18:27:07:
Everything is a string.

The string value of 0x12 is 0x12.

Where the documentation appears to suggest
otherwise, it is in error and needs 
correction and clarification.