Reference · GNU bc
BC Calculator Function Reference
Complete reference for GNU bc mathematical functions — useful alongside the Unix Calculator math tools that mirror bc-style precision.
Math functions
GNU bc provides standard numeric functions. sqrt(x) returns the square root; length(x) returns the number of significant digits in x; scale(x) reports the scale of x (fractional digits in expressions using that value).
scale=10 sqrt(2) length(3.14159) scale(1/3)
String operations
bc treats strings in print statements and can mix lengths with arrays of digits for formatting. For scripted pipelines, combine bc with printf for aligned columns.
# Format bc output to fixed width printf "%.4f\n" "$(echo "scale=4; 355/113" | bc -l)"
Control flow: if / while / for
Use C-like blocks: if (cond) { } else { }, while (cond) { }, and for (init; cond; step) { }. Conditions are numeric — zero is false, any non-zero is true.
sum = 0
for (i = 1; i <= 10; i++) {
sum += i
}
sumUser-defined functions
Define reusable functions with define name(params) { ... }. Use auto for locals. Recursion is supported; watch scale to avoid runaway precision cost.
define factorial(n) {
if (n <= 1) return 1
return n * factorial(n - 1)
}
factorial(6)