Paul L. Snyder on 17 Sep 2009 11:30:52 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] floor/ceil in bash?


On September 17, 2009, "Michael Cramer" <cramer@webkist.com> wrote:

> For a shorter, more obscure version, you could use the reverse-polish, dc
> calculator instead:
[...]
> function ceil { echo "[1+]sa $1 $2 ~ 0 !=a p" | dc; }

Again, this will be fine for same-sign numbers, but will fail for cases
where one of the arguments is negative.  For example:

  $ ceil _5 3  # dc needs '_' rather than '-' for negative numbers
  0

The correct result should be, of course, 1.  You'll need something a
bit longer (and, presumably, correspondingly more obscure ;) to hit all
cases.

Paul
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug