Version 20 of ceil

Updated 2014-05-13 13:22:59 by MS

This expr function returns the least floating point number with a zero fractional part that is no less than its argument (i.e., it rounds up).


The ceil command is a part of the TclX extension. TclX created command interfaces to a number of Tcl mathematical functions. So just read the expr documentation for ceil, and you will know what kind of arguments to pass to it.

RS: Of course you can also write, if you wish

   proc ceil x {expr {ceil($x)}}

so you don't have to require TclX...

LV Certainly. However, the point was to document what the TclX ceil command does...

DKF: Also available in Tcl 8.5 as tcl::mathfunc::ceil.


MS - 2014-05-13 07:04:24 (Miguel Sofer is not the author of this comment, must be some other MS)

The ceil function fails if the difference to the number is smaller than 1e-15. expr ceil(34.000000000000001) = 34 by the interpreter. In most uses of ceil this will probably not be an issue but one has to keep it in mind so as to not lose information. I'm currently working on an elegant way to extend the ceil function and will post it once I've found something.


MS - 2014-05-13 12:31:02 (Miguel Sofer is not the author of this comment, must be some other MS)

an elegant solution to the ceil problem (floor has the same bug and may be treated similar) is the following:

# the number is stored in arg, e.g. arg = 345.678, arg 345
if {[string length [expr int($arg)]] != [string length $arg]} {;#checks whether it is already an integer or not
set i 0; set empt ""; 
while {[string index $arg $i] != "." && $i<[string length $arg]} {set empt "$empt[string index $arg $i]";incr i};#ignore all the digits after the period
incr empt
puts $empt
} else {puts $arg}# empt = 346, empt = 345

One can easily adapt this code fragment to fix ceil and floor.


I didn't know that there is already someone with the initials MS sorry about that. I'll look into changing that.