Tcl Library Source Code

View Ticket
Login
Ticket UUID: a316389e99e6721673115c655f16c553fccda04
Title: math::decimal doesn't handle Tip 551 underscores in numbers fully
Type: Bug Version: 1.0.4
Submitter: juliannoble2 Created on: 2023-08-25 10:14:25
Subsystem: math Assigned To: aku
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2024-01-09 10:10:44
Resolution: None Closed By: nobody
    Closed on:
Description:
Assuming that Tip 551 permit underscore is here to stay...

math::decimal will quietly give the wrong answer on certain input. 
A sample:
% math::decimal::compare [math::decimal::fromstr 1.000001] [math::decimal::fromstr 1.000001]
0
% math::decimal::compare [math::decimal::fromstr 1.000001] [math::decimal::fromstr 1.000_001]
1
User Comments: aku added on 2024-01-09 10:10:44:

I suspect that the parse by itself was done to handle strings containing numbers outside of the range of regular integers and doubles.

If that is not a consideration then Tcl builtin's like expr and scan might be useful in processing the string. Maybe even if large numbers are a consideration, i.e, quick path vs. slow parse ourselves.


aku added on 2024-01-09 10:07:13:

As far as I can tell from reading the sources fromstr assumes decimal integer/double input, and would fail on the old 0x... forms.

I believe that it would also treat 09 as decimal, and not octal as the Tcl builtins do (before Tcl 9).

Mainly because the code seems to parse the number fully by itself.

As a first action we should get more test cases for lots of possible inputs.


juliannoble2 added on 2023-08-28 05:20:14:
see also https://core.tcl-lang.org/tcl/tktview/b204c13b2b

I think a decision needs to be made on whether math::decimal is intended to work with numbers using Tcl's radix specifiers 0x, 0b etc and also the underscores. If not updated to work with all possible formats, then an update to documentation is indicated - as well as some protective measures to always fail noisily.