Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

expr 1

Status
Not open for further replies.

ddrillich

Technical User
Jun 11, 2003
546
US
Good Day,

The following code fails:

set x 12
set y 09
expr {$x - $y}

The error message is:
can't use floating-point value as operand of "-"
while executing
"expr {$x - $y}"

When replacing 09 with 9 all is fine.

Any ideas?

Thanks,
Dan
 
Yes.
All numbers beginning with 0x are hexadecimal vaues and all remaining numbers beginning with 0 are octal values.
But if a number can't fall in these categories it is considered as float. And 09 is a bad octal value (octal digits are 0-7) and was considered as float.
09 being a floating-point is questionable but it was that expr did.

Octal values are no more used and remain an annoyance. You have to deal with.

From the expr page:
OPERANDS
A Tcl expression consists of a combination of operands, operators, and parentheses. White space may be used between the operands and operators and parentheses; it is ignored by the expression's instructions. Where possible, operands are interpreted as integer values. Integer values may be specified in decimal (the normal case), in octal (if the first character of the operand is 0), or in hexadecimal (if the first two characters of the operand are 0x). If an operand does not have one of the integer formats given above, then it is treated as a floating-point number if that is possible. Floating-point numbers may be specified in any of the ways accepted by an ANSI-compliant C compiler (except that the f, F, l, and L suffixes will not be permitted in most installations). For example, all of the following are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16. If no numeric interpretation is possible, then an operand is left as a string (and only a limited set of operators may be applied to it).

HTH

ulis
 
ulis basically explained what you're experiencing. For more information on this -- as well as how to work around it -- go to the Tcl'ers Wiki ( and check out the page "Tcl and octal numbers,"
There is a TIP (Tcl Improvement Proposal) to remove this treatment of octal numbers for the 9.0 release. I strongly suspect that it will pass and be implemented, as octal numbers aren't used much these days. But the Tcl Core Team is very conservative about breaking backwards compatibility, so don't expect this change to occur before 9.0.

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Thanks a lot Ulis and Ken!

Even if 09 is a float why does 12 - 09 fail?

Regards,
Dan
 
I suspect you're using an old & buggy version of Tcl.
With Tcl 8.4 I get:
Code:
  puts [expr {12 - 09}]
->
expected integer but got "09" (looks like invalid octal number)
    while compiling
"expr {12 - 09}"

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top