Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 583fadfcf9eda0b2edea8fdb6fade0a4d02782e7defb5edb7b47fed6a28a5b9d
Ticket: b204c13b2b2335a6baf8fa1a3c6a627432380012
Incompatibility between 8.6 and 8.7 with underscores in numerics
User & Date: juliannoble2 on 2023-08-27 13:28:25
Changes

  1. icomment:
    If this is to be a documentation issue - then I think some clarification of the intended usage and best practice is required.
    
    TIP 551 is very light on details about what the intention is regarding 'in source code' when it comes to a language like Tcl which is fundamentally different to others.
    There is notably no 'consequences' or 'compatibility' section in the TIP.
    
    While the issues might at first seem the same as per 0x 0b 0d 0f specifiers there are a couple of differences.
    
    1)On a possibly minor note - the underscore cross-cuts all these as it extends the syntax of them all.
    This can presumably be addressed in compatibility documentation but already previous code looking at leading 0x and then doing a [string is xdigit] on the remainder would need updating.. as presumably would some regexs.
    
    2) The distinction between what is source code vs data is less clear in Tcl. As it seems there is no intention to restrain this syntax to expr statements where it is normalized - the string rep is free to propagate to other libraries/areas of the codebase unless explicit steps are taken.
    
    It's clear from some of the test files and from interactive use that the extended numeric syntaxes work in other commands such as the list and string commands whether as literals in the specific line of source or as results of command or variable substitution.
    
    There is an example in socket.test of:      set port 0x7fffffff
    (It's tolerated just fine by socket - and similarly other channel commands such as read accept a size with various formats)
    
    This leads one to assume that similarly 'set port 6_000'  etc are likely to be looked on favourably by coders and a this point - with serialization and configuration saving - the syntax is inevitably going to up in config files or at the very least initialisation defaults. More likely so I suspect than things like 0xFF or 0b1010, though ultimately the issues are similar.
    
    My point is not so much whether this is a bad thing as such - but that the policy of what is intended is unclear - and the consequences of this 'leakage' are apparent from a somewhat haphazard survey I've done of the behaviour of various libraries (mainly in tcllib). See below.
    
    If it's intended to be generally limited in some way - then perhaps documentation should express a preference for:
    set port [expr {6_000}]   
    
    But this of course, whilst still providing clarity for reading larger numbers, removes some of the convenience factor the facility was intended to introduce.
    (It also doesn't work in all situations where for example normalising 6_000.00  to 6000.0 may be an undesirable loss of precision)
    
    
    Is it intended that usage such as:
    proc myfunc {{maxsize 6_000_000}} {...}
    set myval 3_000.000_001
    
    where the values are later potentially passed to other libraries that document they accept integers or doubles - be normal usage? 
    
    If so - then all these libraries and existing code bases need to properly normalize or otherwise do further checks than just 'string is integer|double -strict' if they are not using them purely as numbers.
    
    The issue with things like port 6_000 being loaded from configs or defaults - is not that there is a problem passing it on to the socket code - but that configuration settings such as these are quite likely to be fed to other parts of code where it's treated as a string.
    e.g hypothetically:  set connectioninfo ${ip}_${serverport}_${clientport} 
    
    Now imagine another part of the code splitting that on _ to work out the ports. All fixable with documentation and guidelines I'm sure, but I think the notes in the proc below give an idea of the potentially hidden/latent nature of this. It gives the potential for existing codebases to become unreliable as wider use of the syntax occurs. 
    
    
    proc handle_input {input} {
        if {[string is double -strict $input]} {
    
    
            ##1) - a generic example/comment
            lassign [split $input .] a b
            if {[regexp e $b]} {
                #return [handle_input_sci $input]
            }
            ##custom code related to sigfigs,currency(treating input as string to be converted to some non-float) or some other purpose that uses $a and $b
            ## any code using string length to analyze the components will need to be aware.
            ##note that a and b can no longer be assumed to pass the string is double -strict test independently
            ##e.g for input 1000_._01  which passes the initial test  we get 1000_  and _01  which don't.
            ## This may be unlikely input but the 1_._0  appears to be valid double syntax as far as Tcl is concerned.
            
            ##2) a sample taken from the wiki
            ##https://wiki.tcl-lang.org/page/significant+digits+rounding
            #puts stdout [signif $input 6 .]
            ##error: number to round "1_000.1234" is not numeric
            ## in fairness - this code doesn't handle 0x 0b 0o 0d  either - 
    
            ##3)
            ##Breaks silently!    
            ##from tcllib math::decimal - (ticket created 2023-08)
            ## when input has underscore on rhs e.g 1.000_001
            #package require math::decimal
            #math::decimal::compare [math::decimal::fromstr 1.000001] [math::decimal::fromstr $input]
            ## returns 1 when should be 0
            ## (rescale uses string length)
            ## compare 1 vs 0d1 , 0x0_2 vs 0b1_0 etc work ok.
            ##However - from postings on wiki.tcl.tk by the author Mark Alston - it appears the intended input format is as specified in the IEEE 754 standard given at http://speleotrove.com/decimal/ i.e numeric strings
            ## Consequently it may be worthwhile modifying the tcllib version to explicitly check the input conforms rather than allowing Tcl's alternate number forms to leak into it - sometimes working, or sometimes failing silently or noisily.
            
            
            ##4)
            ##tcllib 
            #package require math::bigfloat
            ##Failure input example 1_000.25
            #puts stdout [math::bigfloat::fromstr $input]
            ##error: 1_000.25 is not a number
            ##Nor does bigfloat's fromstr accept 0x, 0b  etc 
            ##
            ##- this is at odds with math::decimal::fromstr which does, but perhaps by happenstance more than by design.
            ##- documentation of the fromstr methods should specify explicitly if alternate formats allowed and lib should sanitize/reject if not rather than accept some and not others.
            
            ##5)
            ##tcllib math::bignum
            #package require math::bignum
            ##Failure input example 1_000
            #puts stdout [math::bignum::fromstr $input]
            ##error: Illegal char '_' for base 10
            ##- as above - bignum's fromstr acceps 0d 0x but not 1_000 or 0d1_000 - inconsistent with math::bigfloat and math::bigdecimal
            
            ##6)
            ##tcllib - The units module takes strings - so it can't be expected to accept the new format,
            ## however - this may be called from user code that does take numbers as parameters
            ## and has only checked 'string is integer' or 'string is double' and built a string to pass on to units::convert
            package require units
            ##works for 2100, 2.1e3 
            units::convert "${input}m" km 
            ## Failure input example 2_100 
            ##error: invalid characters in unit string '2_100m'
            ##Units quite reasonably doesn't accept 0x 0b 0d etc either - The difference being the question of whether numbers like 1_000 are more likely to leak outside of plain expr usage to variables throughout the code/serialized data.
            
            ##7)
            ##tcllib 
            #package require ascii85
            #ascii85::encode -maxlen $input "somestring.... " 
            ## Failure input example 1_00
            ##error: Couldn't compile regular expression pattern: invalid repetition count(s)
            
            ##8)
            ##tcllib
            #package require struct::pool
            ## failure input example 1_000
            #struct::pool mypool $input
            ##error: The second argument must be a positive integer value
            ## (regexp on maxsize)
            
            ##9)
            ## tcllib struct::prioqueue
            ## works fine with various underscored priorities - but they pop out the other side in queuename peekpriority
            ## - This may be more of a feature than a bug - but an example of where the effects may lie distant to where a default was set,
            ## e.g in logging/debugging code.
            
        } else {
            puts other-branch
            #return [handle_input_nondouble $input]
        }
    }
    
    All this is to say, I think releasing this is a bold choice for a non-major version bump - and I'm hoping that someone with experience can provide some more clarity/code-review guidance in both the TIP documentation and any release-notes or general Tcl guidelines.
    
  2. login: "juliannoble2"
  3. mimetype: "text/plain"