Tcl Source Code

View Ticket
Login
Ticket UUID: 62144fdc02ef37b0fcbdbbcd05ffb8375446f536
Title: "scan NaN %f" returns a list with an empty element
Type: Bug Created on: 2026-07-21 11:00:57
Submitter: oehhar Assigned to: nobody
Subsystem: 18. Commands M-Z Severity: Minor
Priority: 5 Medium Last modified: 2026-07-24 09:21:25
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6-9.1
Description:

I tried a bit with doubles, scan and format with NaN and stumbled over the following:

% scan NaN %f
{}
% scan NaN %f fl
0
% set fl
can't read "fl": no such variable

For sure it is a bug:

  • that "scan NaN %f" returns a list with one element with the empty string

As NaN is a valid value for a double, it would be beneficial for me, that "scan NaN %f" really scans a double with a NaN value.

Apparently, TCL does not see "NaN" as a double value. "INF" works for scan, format, expr.

Even if there is no way to generate a NaN within TCL, it may come from an external source like a data base.

Thanks for all, Harald

User Comments:
kuehro added on 2026-07-24 07:45:26:
There are two ways to fix the deficient math of our beloved Tcl:

1) Build your own Tcl with line 26 of generic/tclInt.h changed to 

#define ACCEPT_NAN


2) Download mymath.tcl from 

https://gist.github.com/yyamasak/b1ce55c40631b1c1ea6e8200dba3f7e0

and change line 109 from

		if {$res eq "domain error: argument not in valid range"} {


to


		if {$res eq "domain error: argument not in valid range" || \
		   [string first "non-numeric" $res] > 0}  {


Now you can use myexpr instead of expr, for example with 8.6.18 and 9.1b1 we get:

% set $n -nan
-nan
% puts [myexpr {2.0+$n}]
NaN
% puts [myexpr {1.0/0.0}]
Inf
% puts [myexpr {0.0/0.0}]
NaN


Searching for nan at https://core.tcl-lang.org/tips/doc/trunk/index.md
shows several TIPs related to proper math handling (520,521, 731, ...).
Method 1) still has inconsequent handling of the sign, i.e. -NaN vs NaN 
and method 2) can be used without recompiling but needs myexpr instead 
of expr.
Working in a scientific workplace I had to abandon the use of Tcl for this
reason but for many use cases method 1) or 2) might be viable.

Good luck,

Kai-Uwe

oehhar added on 2026-07-24 07:58:45:

Thanks, Kai-Uwe, for the great hints and TIP pointers. I have apparently hit a difficult point. That explains why nobody adds comments.

But the reason of this ticket is the return of an list with an emty string element by

scan NaN %f

So, NaN is represented here by the empty string.

But

scan NaN %f fl

does not scan NaN as the empty string.

This is inconsistent.

Sorry to hear, that you had to abandon TCL due to missing nan support. Yes, there is now a "isnan()" command which might be the first step.

And the documentation of scan/format do not mention +/-Inf neither, which works.

Thanks for all, Harald


kuehro added on 2026-07-24 08:56:42:
Using a fossil checkout from today and compilation with #define ACCEPT_NAN leads to:


(tcltest) 1 % scan NaN %f
NaN
(tcltest) 2 % scan NaN %f fl
1
(tcltest) 3 % set fl
NaN
(tcltest) 4 % scan -nan %f fl
-NaN
(tcltest) 15 % expr {isnan($fl)}
1
(tcltest) 6 % info patchlevel
9.1b1

Looks Good.

Kai-Uwe

oehhar added on 2026-07-24 09:21:25:

I added a forum to TIP 520 and wrote a message on tcl-core:

https://core.tcl-lang.org/tips/forumpost/990033f3

Please use the forum to post any information outside this bug. We risk to loose the information if the bug is closed.

Thanks for all, Harald