TIP 514: Platform differences in handling int/wide

Login
Author:         Jan Nijtmans <[email protected]>
State:          Final
Type:           Project
Vote:           Done
Created:        20-Aug-2018
Post-History:   
Keywords:       Tcl
Tcl-Version:	8.7
Tcl-Branch:     tip-514
Vote-Results:   3/0/2 accepted
Votes-For:      DKF, JN, KBK
Votes-Against:  none
Votes-Present:  JD, SL

Abstract

This TIP proposes to resolve the platform differences between int/wide/entier math functions and commands like "string is integer"/"string is wide"/"string is entier". At the script level it should not be relevant whether the platform is 32-bit or 64-bit any more.

Most Tcl commands already accept unlimited integers, so there is hardly any command left which need to be checked for correct range.

Rationale

Some examples:

% string is int -4294967296
0
% string is int -4294967295
1
% string is int 4294967295
1
% string is int 4294967296
0

So valid integers appear to range from -(2^32-1) to +2^32-1. Most people learn in school that 32-bit integers range from -2^31 to 2^31-1. Are Tcl's integers 33-bit, but then excluding -4294967296?

% string is wide -18446744073709551616
0
% string is wide -18446744073709551615
1
% string is wide 18446744073709551615
1
% string is wide 18446744073709551616
0

So valid wide integers appear to range from -(2^64-1) to +2^64-1. Most people learn in school that 64-bit integers range from -2^63 to 2^63-1. Are Tcl's wide integers 65-bit, but then excluding -18446744073709551616?

% expr int(2147483648)          ; #on LP64/ILP64 platforms
2147483648
% expr int(2147483648)          ; #on other platforms
-2147483648
% expr int(9223372036854775808) ; #on LP64/ILP64 platforms
-9223372036854775808
% expr int(9223372036854775808) ; #on other platforms
0
% expr wide(9223372036854775808); #all platforms
-9223372036854775808

So, int() does either 32-bit or 64-bit truncation, but the highest left-over bit becomes the sign-bit. wide() does 64-bit truncation.

Specification

Implications

Implementation

Currently, the proposed implementation is available in the tip-514 branch.

Copyright

This document has been placed in the public domain.