TABLE OF CONTENTS


::pwtk::type::number

SYNOPSIS

proc ::pwtk::type::number {args} {

USAGE

   ::pwtk::type::number type ?-strict? value

PURPOSE

Query if a value (or number) is of correct numeric type. The following types are supported:

       binary
       fboolean
       boolean
       int | integer
       posint      
       nonposint   
       negint      
       nonnegint   
       real | double | float
       posreal | posdouble | posfloat
       nonposreal | nonposdouble | nonposfloat  
       negreal | negdouble | negfloat            
       nonnegreal | nonnegdouble | nonnegfloat
       real01

OPTION

ARGUMENTS

RETURN VALUE

1 or 0

SOURCE

    args_

    # perhaps number is specified in Fortran syntax, e.g., 1.0d0
    set number [::pwtk::f2c_number $number]
    
    if { $strict eq {} && $number eq {} } {
        # for non-strict, empty string matches all types
        return 1
    }
    
    switch -glob -- $type {
        fboolean {
            # boolean + Fortran logic (.t. .f. .true. .false.)
            return [string is boolean -strict [string trim $number .]]
        }
        boolean {
            return [string is boolean -strict $number]
        }
        *int* - binary {
            if { ! [string is integer -strict $number] } {
                return 0
            }
        }
        *real - *double - *float - real01 {
            if { ! [string is double -strict $number] } {
                return 0
            }
        }
    }
    switch -glob -- $type {
        binary {
            # 0 or 1
            if { $number == 0 || $number == 1 } {
                return 1
            } else {
                return 0
            }
        }
        nonpos* {
            return [expr $number <= 0]
        }
        nonneg* {
            return [expr $number >=0]
        }
        pos* {
            return [expr $number > 0]
        }
        neg* {
            return [expr $number < 0]
        }
        int* {
            return [string is integer -strict $number]
        }
        real01 {
            # a real number within [0:1]
            return [expr $number >= 0.0 && $number <= 1.0]
        }
        real - double - float {
            return [string is double -strict $number]
        }
    }
    
    # if we came here something is wrong:
    ::error "unsupported type $type" 1
}