Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2025-05-20
18:36 • Ticket [e3dcab1d14] Tcl aborts with wrong digits status still Closed with 4 ... artifact: adeacf8de7 user: dgp
2025-05-16
19:12 • Ticket [e3dcab1d14]: 5 changes artifact: a2b8c5d2ca user: dgp
2023-01-22
02:27 • Closed ticket [e3dcab1d14]. artifact: cb9f600deb user: kbk
02:02
Merge 8.7 (Bug [e3dcab1d14]) check-in: eb8a3ac636 user: kbk tags: trunk, main
01:55
Bug [e3dcab1d14] - merge fix from 8.6 check-in: 1e89a154c2 user: kbk tags: core-8-branch
01:33
Bug [e3dcab1d14] fix check-in: 783bacf316 user: kbk tags: core-8-6-branch
2022-12-23
21:02 • Ticket [e3dcab1d14] Tcl aborts with wrong digits status still Open with 3 ot... artifact: f21f041dc3 user: kbk
06:05 • Ticket [e3dcab1d14]: 3 changes artifact: 092915b279 user: kbk
2022-12-22
15:36 • Ticket [e3dcab1d14]: 4 changes artifact: dbfd97e5d1 user: jan.nijtmans
15:25 • Ticket [e3dcab1d14]: 4 changes artifact: 3b177611f3 user: jan.nijtmans
2022-10-26
09:43 • New ticket [e3dcab1d14]. artifact: 5523d8e030 user: anonymous

Tcl aborts with wrong digits

Closed / Fixede3dcab1d14c0bcc5938ee570b2e0da208bff9239Created 4 years agoUpdated 2 years ago
Descriptionplain2022-10-26 09:43:58
On Ubuntu 22.04 when running the following code, the application crashes with:
wrong digit!
Aborted (core dumped)


#include <float.h>
#include <limits.h>
#include <stdlib.h>
#include "tcl.h"

Tcl_Interp *interp_ = NULL;

const char *GetGlobalArray( const char *varname, const char *idx )
{
  const char *actval = Tcl_GetVar2( interp_, varname, idx,
				    TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG );
  if ( actval == NULL ) {
    fprintf( stderr, "Error in get %s (%s)\n", varname, idx );
    exit( 1 );
  }
  
  return actval;
}

void SetGlobalArray( const char *varname, const char *idx,
		     double varval )
{
  fprintf( stderr, "Set %s, %s  : %.*e\n", varname, idx, DBL_DIG, varval );

  Tcl_Obj *ns1 = Tcl_NewStringObj( varname, -1 );
  Tcl_IncrRefCount( ns1 );
  Tcl_Obj *ns2 = Tcl_NewStringObj( idx, -1 );
  Tcl_IncrRefCount( ns2 );
  Tcl_Obj *nd = Tcl_NewDoubleObj( varval );
  Tcl_IncrRefCount( nd );
  double foo;
  Tcl_GetDoubleFromObj( interp_, nd, &foo );
  fprintf( stderr, "Double value of object: %.*e\n", DBL_DIG, foo );
  
  Tcl_Obj *actval = Tcl_ObjSetVar2( interp_, ns1, ns2, nd,
				    TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG );

  fprintf( stderr, "Content of array      : %s\n", 
	   GetGlobalArray( varname, idx ) );
  Tcl_DecrRefCount( ns1 );
  Tcl_DecrRefCount( ns2 );
  Tcl_DecrRefCount( nd );
}

int main( int argc, const char *argv[] ) 
{
  Tcl_FindExecutable( argv[0] );
  interp_ = Tcl_CreateInterp();
  
  Tcl_ObjSetVar2( interp_, Tcl_NewStringObj( "tcl_precision", -1 ), NULL,
  		  Tcl_NewIntObj( 17 ), TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG );

  double val = 6.4623485355705287e-27;
  SetGlobalArray( "some_array", "idx1", val );
  return 0;
}
Referenced by3
jan.nijtmanswiki2022-12-22 15:25:19

This is even reproducable (kind of) at script level:

% set tcl_precision 14
14
% expr 6.4623485355705287e-27
6.4623485355705e-27
% set tcl_precision 15
15
% expr 6.4623485355705287e-27
%
So "expr" gives the empty string when tcl_precision>=15.

Since, tcl_precision is deprecated (it's removed completely in Tcl 9.0), not high prio to fix this.

jan.nijtmanswiki2022-12-22 15:36:34

Most likely, this is the TclDoubleDigits function, which was rewritten [here].

Assigning to Kevin

kbkplain2022-12-23 06:05:00
It's going to be a little while before I can get to this; I've got a bit of a case of "CoViD brain fog" and can't quite focus enough to debug something complicated at the moment. It's been less than a week since I came down with CoViD, so I'm hoping things will have cleared up after the holidays.
kbkplain2022-12-23 21:02:40
It's going to be a little while before I can get to this; I've got a bit of a case of "CoViD brain fog" and can't quite focus enough to debug something complicated at the moment. It's been less than a week since I came down with CoViD, so I'm hoping things will have cleared up after the holidays.
kbkplain2023-01-22 02:27:07
There was an off-by-one in three (closely related) places in tclStrToD.c that affected 15<=tcl_precision<=18 for certain double values that were very close to overflowing intermediate results. It would take more work to explain exactly which values are affected, but 2**-87 is definitely one of them, and there's a suitable test case for 8.x.

Tcl 9.x has the bug fixed, but the behaviour appears to be untestable at script level at the moment. I'll look into tidying that at some point.  If we ever get around to using Tcl_DoubleDigits in Tcl_AppendFormatToObj (which would probably be a good idea), then we'll need the same code.
dgpplain2025-05-16 19:12:43
Three tests introduced by this patch fail on a tcl/unix build
on a macOS Sonoma 14.7.5 system, built under tcl/unix with clang 16:

==== expr-63.15.87 convert 2**-87 to decimal at precision 15 FAILED
==== Contents of test case:

                do-one-test-expr-63 $e $p $float $athreshold
            
---- Result was:
Result 0.00000018626451e-28 is more than 5.7174648137343064e-42 away from 6.462348535570529e-27
---- Result should have been (exact matching):

==== expr-63.15.87 FAILED



==== expr-63.16.87 convert 2**-87 to decimal at precision 16 FAILED
==== Contents of test case:

                do-one-test-expr-63 $e $p $float $athreshold
            
---- Result was:
Result 0.000000186264515e-28 is more than 1.2174648137343063e-42 away from 6.462348535570529e-27
---- Result should have been (exact matching):

==== expr-63.16.87 FAILED



==== expr-63.17.87 convert 2**-87 to decimal at precision 17 FAILED
==== Contents of test case:

                do-one-test-expr-63 $e $p $float $athreshold
            
---- Result was:
Result 0.0000001862645149e-28 is more than 7.674648137343063e-43 away from 6.462348535570529e-27
---- Result should have been (exact matching):

==== expr-63.17.87 FAILED

These failures continue through the current tip of the 8.6 branch
dgpplain2025-05-20 18:36:44
It looks like the tcl/unix builds make it far too easy
for a `make test` to test the installed library that is
still buggy instead of testing the built library in the
workspace which includes the fix.

I think this was a false alarm.
Log in (anonymous is fine) to add a comment.