Tcl Library Source Code

Ticket Change Details
Login
Overview

Artifact ID: a53575c70764f8d83045e653b43ceba444cb140a
Ticket: 3309165fffffffffffffffffffffffffffffffff
math::bigfloat::isInt returns 1 on non-integers
User & Date: arjenmarkus 2014-09-21 13:17:17
Changes

  1. assignee changed to: ""
  2. comment changed to:
    Tcl 8.5
    math::bigfloat version 2.0.1
    
    The procedure math::bigfloat::isInt performs a very naive check to distinguish BigFloats from integers, but the check isn't nearly comprehensive enough to cover all or even most cases.  The doc states that the proc should be able to distinguish standard Tcl 8.5 integers from other values.  But note for example:
    
    % isInt 2.0
    1
    % isInt 0.5
    1
    % isInt 1/2
    1
    % isInt [expr exp(1)]
    1
    % isInt i
    1
    % isInt HAM_SANDWICH
    1
    
    A better implementation might be something like:
    
    proc isInt {n} {
    if {[catch {set n [expr abs($n)]}]} {return 0}
    if {[string first . $n] > -1} {return 0}
    return 1
    }
    
  3. icomment:
    Fixed this bug - the original implementation simply checked if it was a single string, nothing more.
    
    To avoid the overhead of converting a number to a string, I have chosen the following implementation:
    
    proc ::math::bigfloat::isInt {n} {
        set rc [catch {
            expr {$n%2}
        }]
        return [expr {$rc == 0}]
    }
    
  4. login: "arjenmarkus"
  5. mimetype: "text/plain"
  6. resolution changed to: "Fixed"
  7. severity changed to: "Minor"
  8. status changed to: "Closed"