Showing results for 
Search instead for 
Did you mean: 

bc and rounding ...

Go to solution
A. Daniel King_1
Super Advisor

bc and rounding ...

Hi, folks.

I'd like to generate some general discussion about mathematics in HP-UX from the shell, starting with:

- How does one do rounding in "bc"?
- Does anyone know where I could find the "bc tutorial in Number Processing Users Guide" referenced in the bc man page?
- Is there a good beginners' reference for dc?
- Do either of the above calculate standard deviation, solving matrices, or other interesting math/statistics? (Deliberately left wide open!)

(I know the perl and mathomatic guys are going to have a field day with this, and the input is certainly welcome, though the question is focused on HP-UX default-OS utilities.)
Command-Line Junkie
Bill Hassell
Honored Contributor

Re: bc and rounding ...

Since bc performs real number math (fractions included), normal math steps are used for rounding. To round to 2 decimal positions, multiply the number by 100, add 0.5, convert the result to an integer and then divide by 100.

Unfortunately, the bc man page is badly out of date as far as the manual "Number Processing Users Guide". The last time I saw that book was HP-UX 8.0, maybe 9.0, but apparently it went out of print before it could be converted to a PDF and sent to hppt://

It is even tricky to locate on the are some references:

There are some GNU bc manuals...much of it will apply, but as with most GNU tools, there are enhancements...

and for dc

Bill Hassell, sysadmin
A. Clay Stephenson
Acclaimed Contributor

Re: bc and rounding ...

Here's my function for rounding in bc:

define r (a,s) {
auto z,f,n

n = 0;

if (a < 0) { n = 1; a = -(a) }
f=a + (5 / 10^(s + 1));
f=(f * (10^s)) / (10^s);
if (n > 0) f = -(f);


You send in the value and the number of desired decimal places. Note that the rounding assumes a radix of 10.

While one could use bc to do more complex math, it's much like using a file to cut threads on a bolt. Yes it can be done but a die is a much better tool. Nowadays, Perl can be considered "Standard Equipment" on virtually any UNIX box and would be a far better choice for complex math. Bc and dc really only make sense with numbers with extremely high-precision and needed and even there Perl's Math::BigFloat and/or Math::BigInt modules make much more sense although that would require the installation of non-standard modules.
If it ain't broke, I can fix that.
A. Daniel King_1
Super Advisor

Re: bc and rounding ...

From the bc.pdf link ...

"BC is a language and a compiler for doing arbitrary precision arithmetic on the PDP-11
under the UNIXâ   time-sharing system."

I'm loving it. Handed down with the monolith from 2001 ...

I was hoping there would be some built-in functions; I guess there are not so many as I had hoped.

Anyone else? No mathomatic folks?
Command-Line Junkie
James R. Ferguson
Acclaimed Contributor

Re: bc and rounding ...

Hi Daniel:

Perl's POSIX module (part of its standard distribution) offers the 'floor' and 'ceil' functions in addition ot 'int' for rounding.

While the standard 'int' returns the integer part of a floating point number, truncates the fractional part. Thus, it rounds down for positive numbers and up for negative ones. If you use the POSIX module you have 'floor' and 'ceil' available. 'floor' always rounds down regardless of the number's sign, while 'ceil' always rounds up, regardless of the sign.

use POSIX qw(floor, ceil);