Click to See Complete Forum and Search --> : "bc" and "dc" can't see to divide right.


Craig McPherson
09-16-2001, 10:17 AM
bc and dc are described as "arbitrary precision", but I can't seem to get much out of them besides integers. I'll use bc as an example instead of dc because some people aren't familiar with RPN.

2 + 2
4 good so far...

2 * 10
20 still looking good...

10 / 2
5 okay...

1 / 2
0 DOH!

10 / 3
3 Nope!

3.1 / 1
3 Not quite!

2.1 + 4.5
6.6 It works with addition...

1.1 - 0.1
1.0 It works with subtration...

1.4 * 3.7
5.1 It works with multipication...

3 / 2.5
1 Nope!

Whenever the result of a division is not an integer, it drops the fractional bit.

What's going on here? I can't find anything about this in the manpages. I assume there's some floating-point mode I have to stick the program in, but I'm not finding anything.

nuisance
09-16-2001, 10:45 AM
With bc you can give the command 'scale=x' (where x is an integer or was assigned some value previously) before doing the division to get it to print 'x' numbers after the decimal. Apparently, division is the only mathematical function that uses the 'scale' variable to determine the accuracy of its answer.