Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-07-24
09:21 • Ticket [62144fdc02] "scan NaN %f" returns a list with an empty element... artifact: 698a7f9327 user: oehhar
08:56 • Ticket [62144fdc02]: 3 changes artifact: 7660028867 user: kuehro
07:58 • Ticket [62144fdc02]: 4 changes artifact: fcf8a11521 user: oehhar
07:45 • Ticket [62144fdc02]: 3 changes artifact: e10b763a5d user: kuehro
2026-07-21
11:00 • New ticket [62144fdc02]. artifact: 84fd588d06 user: oehhar

"scan NaN %f" returns a list with an empty element

Open62144fdc02ef37b0fcbdbbcd05ffb8375446f536Created 134 days agoUpdated 132 days ago
Descriptionwiki2026-07-21 11:00:57
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

kuehroplain2026-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
oehharwiki2026-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

kuehroplain2026-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
oehharwiki2026-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

Log in (anonymous is fine) to add a comment.