Updated 2018-09-28 13:04:13 by PeterLewerin

expr function available since Tcl 8.5: tcl::matchfunc::entier

Converts a number to some integral type (int, wide or bignum). This function was introduced in TIP#237.

The name of this function is derived from French, via ALGOL [1]. It also shows up in Tcl as a subcommand of string is

This is different from [tcl::mathfunc::int] in that it won't overflow:
% expr int(256**$::tcl_platform(wordSize))
0
% expr entier(256**$::tcl_platform(wordSize))
18446744073709551616

Rounding behaviour

Note that entier will truncate the decimal part of a number, effectively 'rounding towards 0':
% expr entier(-1.6)
-1
% expr entier(1.6)
1

Lars H: As a rounding function, this is unfortunately not particularly good. There a four common ways to round doubles to integers, three of which are useful:
To nearest integer
This is round.
To smallest integer >= given number
This has to be coded as round(ceil($x)).
To greatest integer <= given number
This has to be coded as round(floor($x)).

and one which is not:
Round towards zero
A.k.a. truncating decimals. This is entier. Specified by the hideous inequality 1 > abs($x) - abs(entier($x)) >= 0.

At least one specification of Algol (now linked above) was careful to not allow such behaviour for that entier.

2014-12-29: then again, entier isn't a rounding function. Like int or wide, it's a coercion function. If this coercion were combined with rounding, the implementation would in effect be making choices the programmer would like to have some control over.