How to check value of variable is between MAX_INT and MIN_INT of 32 bit OS

1

I understand that all variables are of type strings.

How can we check a variable value between MAX_INT(0x7FFFFFFF) and MIN_INT (-0x80000000) of 32 bit OS

set var "12334"
...How to check var variable datatype and value range ..
tcl
asked on Stack Overflow Jun 29, 2019 by Programmer • edited Jun 29, 2019 by Programmer

1 Answer

3

The string is int command does most of the work. You can use tcl::mathop::<= for the rest:

set MIN_INT -0x80000000
set MAX_INT 0x7FFFFFFF
if {[string is int -strict $value] && [tcl::mathop::<= $MIN_INT $value $MAX_INT]} {
    puts "$value is a proper 32-bit signed integer"
}

You can use this if you prefer (the parentheses are just for clarity):

if {[string is int -strict $value] && ($MIN_INT <= $value) && ($value <= $MAX_INT)} {
answered on Stack Overflow Jun 29, 2019 by Donal Fellows

User contributions licensed under CC BY-SA 3.0