You need to understand the meaning of the scale of an expression in bc
. bc
can do arbitrary precision (which doesn't necessarily mean infinite precision) while your calculator will probably have the precision of the float
or double
data type of your processor.
In bc
. The scale is the number of decimal after the dot, so related to the precision. The scale of an expression is determined based on rules that depend on which operator is involved and the scale
variable (that variable is the one that gives the arbitrary dimension of the precision of bc
that is, that can make its precision as big as you want).
For instance, the scale of the result of a division is scale
. So 4/3
when scale
is 2 is 1.33
, so a very rough approximation of 4/3
. The scale of x * y
will be min(a+b,max(scale,a,b))
(where a
is the scale of x
and b
the scale of y
), so here 2
. so 1.33 * 3.14
will be 4.17
.
For the rules, you can check the POSIX specification for bc
.
If you want a greater precision, increase scale
. You can increase it indefinitely. With bc -l
, scale
is automatically set to 20
.
$ pi='(a(1)*4)' r=3
$ $ echo "(4 / 3) * $pi * ($r ^ 3)" | bc -l
113.09733552923255658339
$ echo "scale=1000; (4 / 3) * $pi * ($r ^ 3)" | bc -l
113.0973355292325565846551617980621038310980983775038095550980053230\
81390626303523950609253712316214447357331114478163039295378405943820\
96034211293869262532022821022769726978675980014720642616237749375071\
94371951239736040606251233364163241939497632687292433484092445725499\
76355759335682169861368969085854085132237827361174295734753154661853\
14730175311724413325296040789909975753679476982929026989441793959006\
17331673453103113187002257495740245517842677306806456786589844246678\
87098096084205774588430168674012241047863639151096770218070228090538\
86527847499397329973941181834655436308584829346483609858475202045257\
72294881898002877683392804259302509384339728638724440983234852757850\
73357828522068813321247512718420036644790591105239053753290671891767\
15857867345960859999994142720979823815034238137946746942088054039248\
86988951308030971204086612694295227741563601129621951039171511955017\
31142218396089302929537125655435196874321744263099764736353375070480\
1468800991581641650380680694035580030527317911271523
$ echo "scale=1; (4 / 3) * $pi * ($r ^ 3)" | bc -l
97.2
You can also do all your calculations with a high scale
, and reduce it in the end for display:
$ echo "scale=10; (4 / 3) * $pi * ($r ^ 3)" | bc -l
113.0973355107
$ echo "scale=100; x = (4 / 3) * $pi * ($r ^ 3); scale = 10; x / 1" | bc -l
113.0973355292
4/3*3.14*(3^3)
should be36*3.14 = 113.04
, not113.1
btw. What's thatcalculator
you're using? – Stéphane Chazelas May 02 '16 at 14:054/3*3.14159265358979324*(3^3) = 113.09733552923255664
which rounds up to113.1
– deamentiaemundi May 02 '16 at 16:51