Bc (programming language): Difference between revisions

Content deleted Content added
History: RPN is a weird combination of both more and less convenient; bc was supplementary rather than an outright replacement
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 55:
=====Similar to C=====
The [[Modulus operator|modulus]] operators, <code>%</code> and <code>%=</code> behave exactly like their C counterparts only when the global '''<code>scale</code>''' variable is set to 0, i.e. all calculations are integer-only. Otherwise the computation is done with the appropriate scale. <code>a%b</code> is defined as <code>a-(a/b)*b</code>. Examples:
<sourcesyntaxhighlight lang="console" highlight="6,8,10">
$ bc
bc 1.06
Line 67:
scale=20; 5%3
.00000000000000000002
</syntaxhighlight>
</source>
 
=====Conflicting with C=====
Line 216:
==Using bc in shell scripts==
bc can be used non-interactively, with input through a [[Pipeline (Unix)|pipe]]. This is useful inside [[shell script]]s. For example:
<sourcesyntaxhighlight lang="console">
$ result=$(echo "scale=2; 5 * 7 /3;" | bc)
$ echo $result
11.66
</syntaxhighlight>
</source>
In contrast, note that the [[Bash (Unix shell)|bash shell]] only performs integer arithmetic, e.g.:
<sourcesyntaxhighlight lang="console">
$ result=$((5 * 7 /3))
$ echo $result
11
</syntaxhighlight>
</source>
One can also use the [[Here document|here-string]] idiom (in bash, ksh, csh):
<sourcesyntaxhighlight lang="console">
$ bc -l <<< "5*7/3"
11.66666666666666666666
</syntaxhighlight>
</source>
 
==See also==