Jason Stelzer on 16 Sep 2009 17:12:36 -0700 |
Well, to be fair.... First, thanks, that's a good idea. I'll just do that. Second, I really just need to always round up the the nearest whole number. On Wed, Sep 16, 2009 at 7:33 PM, Paul L. Snyder <plsnyder@drexel.edu> wrote: > On Wed, September 16, 2009, Jason Stelzer" wrote: > >> The details are boring and irritating, but I'm looking to get the ceil >> for something in bash. Here's how I'm brute forcing it. > [...] >> Is there an easier way? And yeah, I know that using _MY_CEIL as a >> bucket is kind of weird. Either way the code looks odd to me. > > Bash does not support floating-point math, unless they've done something > sneaky in recent releases that I don't know about. Trying to make it > do so on your own will only lead to woe. I haven't traced through your > script, but I'd be pretty suspicious of it. > > If you don't want to use a shell that supports floating-point math > (such as zsh), I'd recommend turning to bc at a time like this. > > First define a bash function that calls bc. In bc, "scale" is the number of > positions after the decimal place that are retained after some arithmetic > operations. By default, scale is 0, so dividing by 1 returns the integer > portion of a number. The $1 is the first argument to the bash ceil function > being defined. > > ------ > function ceil () { > echo "define ceil (x) {if (x<0) {return x/1} \ > else {if (scale(x)==0) {return x} \ > else {return x/1 + 1 }}} ; ceil($1)" | bc; > } > ------ > > To stuff a value into an envar, call with command substitution: > > $ X=5.5 > $ CEIL_X=$(ceil x) > $ echo $CEIL_X > 6 > > HTH, > 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 > -- J. ___________________________________________________________________________ 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
|
|