BC(1plan9)

๐Ÿ’– bc โ€” arbitrary-precision arithmetic language ๐Ÿ’–

okay so hi bestie, welcome to the bc page from the Plan 9 manual, section 1 (that's the 1plan9 situation up top in BC(1plan9) โ€” yes it's the header, we're not copying it, we're vibing with it ๐Ÿซถ). it's giving calculator but make it a whole programming language. no cap.


โœจ NAME โœจ

bc โ€” arbitrary-precision arithmetic language. that's it. short. iconic. ate.

๐ŸŒธ SYNOPSIS ๐ŸŒธ

bc [ -c ] [ -l ] [ -s ] [ file ...  ]

๐Ÿซถ DESCRIPTION ๐Ÿซถ

okay so Bc is an interactive processor for a language that resembles C, but the plot twist is that it provides arithmetic on numbers of arbitrary length with up to 100 digits right of the decimal point. ๐Ÿ’… it takes input from any files given, then reads the standard input.

here's the flag situation, bestie:

  • -l โ€” stands for the name of an arbitrary precision math library.
  • -s โ€” suppresses the automatic display of calculation results; all output is via the print command.
  • -c โ€” compile only, and we'll get to that in a sec (spoiler: it's the preprocessor tea โ˜•).

the following syntax for bc programs is like that of C; L means letter a-z, E means expression, S means statement. literally the alphabet soup of math, and you're the main character.

๐Ÿ“ Lexical

  • comments are enclosed in /* */
  • newlines end statements

๐Ÿท๏ธ Names

  • simple variables: L
  • array elements: L[E]
  • The words ibase, obase, and scale

๐Ÿงฎ Other operands

  • arbitrarily long numbers with optional sign and decimal point.
  • (E)
  • sqrt(E)
  • length(E) โ€” number of significant decimal digits
  • scale(E) โ€” number of digits right of decimal point
  • L(E,...,E) โ€” function call

โž• Operators

  • + - * / % ^ (that's % is remainder; ^ is power, no cap)
  • ++ --
  • == <= >= != < >
  • = += -= *= /= %= ^=

๐Ÿ“œ Statements

  • E
  • { S ; ... ; S }
  • print E
  • if ( E ) S
  • while ( E ) S
  • for ( E ; E ; E ) S
  • null statement
  • break
  • quit
  • "text"

๐ŸŽ€ Function definitions

define L ( L , ...  , L ){
auto L , ...  , L
S ; ...  ; S
return E
}

๐Ÿงš Functions in -l math library

  • s(x) โ€” sine
  • c(x) โ€” cosine
  • e(x) โ€” exponential
  • l(x) โ€” log
  • a(x) โ€” arctangent
  • j(n, x) โ€” Bessel function

All function arguments are passed by value. periodt. ๐Ÿ’–


The value of an expression at the top level is printed unless the main operator is an assignment or the -s command line argument is given. Text in quotes, which may include newlines, is always printed. Either semicolons or newlines may separate statements. Assignment to scale influences the number of digits to be retained on arithmetic operations in the manner of dc(1). Assignments to ibase or obase set the input and output number radix respectively. ๐Ÿฆ‹

The same letter may be used as an array, a function, and a simple variable simultaneously. All variables are global to the program. Automatic variables are pushed down during function calls. In a declaration of an array as a function argument or automatic variable empty square brackets must follow the array name.

ngl, that "same letter for everything at once" thing is highkey unhinged and I respect it so much.

Bc is actually a preprocessor for dc(1), which it invokes automatically, unless the -c (compile only) option is present. In this case the dc input is sent to the standard output instead.

๐Ÿ“ EXAMPLE ๐Ÿ“

Define a function to compute an approximate value of the exponential. Use it to print 10 values. (The exponential function in the library gives better answers.)

scale = 20
define e(x) {
     auto a, b, c, i, s
     a = 1
     b = 1
     s = 1
     for(i=1; 1; i++) {
          a *= x
          b *= i
          c = a/b
          if(c == 0) return s
          s += c
     }
}
for(i=1; i<=10; i++) print e(i)

๐Ÿ“ FILES ๐Ÿ“

  • /lib/bclib โ€” mathematical library

๐Ÿ” SOURCE ๐Ÿ”

  • /src/cmd/bc.y

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

๐Ÿ’€ BUGS ๐Ÿ’€

bestie. sit down. ๐Ÿ™ˆ

  • No or operators.
  • A statement must have all three
  • A is interpreted when read, not when executed.
yeah. "A statement must have all three" and then it just... stops. the manpage said "all three" and left us on read. no further details, we simply do not know what the three are. ๐Ÿ’€ iconic behavior honestly.

okay and that's the whole page, fr fr ๐Ÿ’œ the footer at the bottom of this manual is just BC(1plan9) all over again โ€” same source, Plan 9 manual, section 1 โ€” with no date in sight, because who needs dates when you have arbitrary precision. go forth and compute, you gorgeous genius, you. ๐ŸŒˆ๐Ÿ‘‘โœจ

๐Ÿ“ฆ where this page comes from