Skip to content
Tcl Source Code View Ticket
Login

View Ticket

expr, literal numeric values, and string comparisons

Closed / Invalidef5373e6fa0617ee02f201c6c8f41746678c5afbCreated 10 years agoUpdated 2 years ago
Descriptionwiki2016-10-03 22:17:53
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.

dgpplain2016-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.
dkf Ticket [ef5373e6fa]: 3 changes [History diff]2016-10-08 16:57:57
dkf Ticket [ef5373e6fa]: 3 changes [History diff]2016-10-30 17:40:11
oehharwiki2016-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

pooryorickwiki2024-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.

Log in (anonymous is fine) to add a comment.