Relational expressions return 0 if the relation is false and 1 if the relation is true
Any non-zero result is considered true.
but beware of precedence. a = 3 < 2 does not assign 1 to a; instead
it sets a = 3 and then compares 3 to 2 so to get the same result
as in c, use parenthesis a = ( 3 < 2 )
bc must be called with the parameter -l in order to use the math library.
This also sets the default scale to 20.
$ echo '4*a(1)' | bc
Runtime error (func=(main), adr=3): Function a not defined.
$ echo '4*a(1)' | bc -l
3.14159265358979323844
echo 'scale=50;4*a(1)'|bc -l
3.14159265358979323846264338327950288419716939937508
Note that the precision for pi suffers in the last 1-2 decimal places so for a
work around, ask for two more than you want and truncate.
#!/usr/bin/bc -ql
# Two comment styles:
/* also this */
"How old are you ? "
age = read()
print age
" isn't very old"
print "\n"
halt
Only numbers can be input using read
Functions return 0 (false) by default.
Assume this script is named factorial
#!/usr/bin/bc -q
# Recursion is possible
define f (x) {
if (x <= 1) return (1);
return (f(x-1) * x);
}
f(8)
halt
$ factorial
40320
![]() |
This site best viewed with a browser |
| Warning: This is a Debian centric site | |
| Many thanks to Debra and Ian Murdock for making Debian possible | |
| First created Feb 5, 2010 ~ Last revised February 05, 2010 |