Version 10 of entier

Updated 2007-11-11 12:12:30 by lars_h

expr function available since Tcl 8.5: Converts a number to some integral type (int, wide or bignum).

The name of this function is derived from French, via ALGOL.


LV So, this is functionally similar to a cast? What happens when the number being entiered doesn't fit into the size type? A bignum being cast into an int, for instance?

escargo 29 Nov 2006 - It's more like the round function. I think the point is that however big the number might be, it will pick the smallest integral type that will hold it. See TIP 237 [L1 ] for full details.

DKF: It's unspecified what size of value you get beyond "big enough", and a bignum is big enough to take any integer you want (assuming you've got the memory to hold it...)

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 [L2 ] specification of Algol was careful to not allow such behaviour for that entier.