Skip to content
Tcl Source Code View Ticket
Login

View Ticket

expr round(12345678901234567890)

Closed / Invalid809784ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 23 years ago
Descriptionplain2003-09-20 15:31:42
This fails on 8.4.2 and 8.4.4, Windows version at least:

% expr round(1234567890123456789)
1234567890123456789

% expr round(12345678901234567890)
-6101065172474983726

% expr round(123456789012345678900)
integer value too large to represent

-- gary

garyfeng at duke dot edu
dkfplain2003-09-20 22:50:56
Logged In: YES 
user_id=79902

Tcl integers are signed 64-bit values (modulo some
complexity which doesn't matter here.)  1234567890123456789
is a value that is precisely representable as a signed
64-bit value, and so is OK.  12345678901234567890 is not,
but can be represented as an *unsigned* 64-bit value so we
can still parse it (and get the same bit representation) but
it is interpreted as a signed 64-bit value and so comes out
looking quite different (you can use the [scan] command to
print it as an unsigned value if necessary.)
123456789012345678900 is not representable at all in a
64-bit quantity.

If you have to work with integers of this size, check out
the mpexpr extension.  And rest assured that before 8.4 you
would have hit the limit with much smaller values (i.e.
32-bits) on your platform!  :^/  ("Fixing" this in the core
really requires access to a high-quality unencumbered
arbitrary-precision arithmetic library, but those are
kind-of rare on the ground.)
nobodyplain2003-09-22 21:57:31
Logged In: NO 

Thank, dkf. I was working on a microsecond timer/counter 
sort of thing with an arbitrary starting value. I was surprised 
it hits the upper limit of 64-bit system. I just thought round() 
should give a consistent answer. But I can easily add a check 
in my code. thanks for your reply!

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