- 1975
- "Basic/Bench Calculator"
- started as a frontend for dc
- Authors:
- Robert H. Morris Sr.
- Lorinda Cherry
cli
bc [OPTIONS] [FILES]
-q | –quiet | suppreses welcome message |
-l | –mathlib | enables trigonometric functions, sets scale=20 |
language
- Runs interactively (REPL) if you just run it bare "bc"
- default
scale=0
, aka integer division - in POSIX mode, functions and variables can only be 1 character long
- undefined variables return 0
- if/while/for
- "#" single line comment, "**" multi line comment
- "auto" for declare local variables
define foo(x) { auto pi=3.14 print "this is 2 to the power of "; x*1; 2^x; pi*2; print "\n" }
functions
length | n | length in digits of n, including decimals |
s | n | sine |
c | n | cosine |
a | n | arctangent |
l | n | natural logarithm |
e | n | exponential of e to n |
j | n,m | Bessel function of integer order n of x |
random | - | returns a random positive? integer |
read | - | reads value from stdin, returns value read |
s | ala Python 2 |
variables
last | last numer output |
scale | number of decimals (default 0) |
obase | "output base" (default 10) |
ibase | "input base" |
articles
- 23 https://org.coloradomesa.edu/~mapierce2/bc/
- https://web.archive.org/web/20140112080500/http://mylinuxbook.com/linux-command-line-calculator-bc-examples/
- https://unixetc.co.uk/2014/01/19/bc-rounding-errors/
- https://www.johndcook.com/blog/2010/07/14/bc-math-library/
- https://www.johndcook.com/blog/2019/10/29/computing-pi-with-bc/
- http://www.physics.smu.edu/coan/linux/bc.html
videos
- bc - geeksforgeeks https://www.youtube.com/watch?v=_UwhS0IvwQk
- Command Line Calculator - gotbletu https://www.youtube.com/watch?v=JkyodHenTuc
- Bc: Deep Dive Into The POSIX Calculator Language | Broodie Robertson https://www.youtube.com/watch?v=JascI_29sks
- Linux in the Shell Ep 25 - bc https://vimeo.com/101977655
- 23 video Is Python a souped up Basic Calculator? https://www.youtube.com/watch?v=HcRMo0wGq44
codebases
- math and bitcoin
- gcd, fibo https://literateprograms.org/category_programming_language_bc.html
- official examples https://github.com/fivepiece/gnu-bc/tree/master/Examples
- resources http://www.phodd.net/gnu-bc/
- functions https://github.com/mikepierce/GNU-bc-Functions
- functions https://web.archive.org/web/20160304092132/http://x-bc.sourceforge.net/extensions_bc.html
- functions https://github.com/idealvin/bc
- constants https://web.archive.org/web/20160304081309/http://x-bc.sourceforge.net/scientific_constants_bc.html
snippets
pi
scale=10
4*a(1)
solve
$ solve "4*49+732" $ cat /usr/local/bin/solve #!/bin/sh bc << EOF scale=4 $@ quit EOF
sergiosgc/AdventOfCode2021
day6 part 1
for (; i>0; i-=1) { n = timer[0] for (t=0; t<8; t+=1) timer[t] = timer[t+1] timer[8] = n timer[6] += n } for (i=0; i<9; i+=1) result += timer[i] print result print "\n"
implementations
- alternative qalc
- https://github.com/gavinhoward/bc