Version 24 of ceil

Updated 2014-05-13 14:26:37 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 req, e.g. arg = 345.678, arg 345
if {[string length [expr int($arg)]] != [string length $arg]} {
set i 0
set empt ""
while {[string index $arg $i] != "." } {set empt "$empt[string index $arg $i]";incr i}
incr empt
puts $empt
} else {puts $arg};#empt = 346, arg = 345

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