: : C y r e k S o f t : :

 
* Home

* FAQ

* Tools

+ Games + GNU bc + Interesting + Stuff + Links
 

GNU bc

Ah yes. GNU bc. That useful little calculator program found on most modern Linux installations that no-one seems to do anything interesting with. Well it's time for a change. On this page you'll find various bits and pieces to make life with GNU bc much, much simpler or at least more interesting.

Somewhere below his paragraph there are... (counts for a bit) ...uh, more than ten bc files and one bash script for starting bc with a special setup.

Each of the scripts/files/programs/libraries/whatevers below are packed with comments explaining exactly what the file is, what it does and what each of the functions do. It's not the best way of documenting things, but if you intend to use or learn from these files, putting the documentation in with the code was - IMHO - the best way to do things.

This page is currently linked from the bc articles in both the English and German Wikipedias. Welcome and Willkommen to readers heading in from over there!

* If you found this page through an internet search and still can't see what you're looking for, try the new bc FAQ page - you might find it there!

Functions Galore
  • funcs.bc - A large number of functions that expand on the standard GNU bc library. They can be used for doing calculations without having to rederive them every time bc is started. The first line is a shebang line just so that it can be started instead of the standard GNU bc interpreter.
  • intdiff.bc - Supplanting the old dfxdx.bc which provided a function to perform simple numerical differentiation of a single variable function, this also contains the corresponding numerical integration of a function between two values. The latter comes with a tasty little function called glai that tries to predict convergence limits.
  • logic.bc - Ever wanted to be able to do logical bitwise operations in bc? AND, OR, XOR and NOT? Now you can.
  • primes.bc - Originally part of funcs.bc, the prime number functions have now been moved into their own file. Some algorithms have been rewritten for faster prime sieving, and there's a new function for generating primes quickly. Update: added a function to determine whether a number is square-free. (Or cube-free, or tesseract-free, etc.)
  • Other interesting libraries
    • collatz.bc - Some functions for experimenting with the Collatz conjecture. It's unlikely this library will provide any breakthroughs or insights, but it's fun to play with every now and again.
    • cf.bc - Want to play with simple Continued Fractions? Start right here.
    • complex.bc - Complex numbers in bc. Yes, that's right; And it works too. OK so it's still no great shakes, and there's only the most basic of mathematical functions but I'm still patting myself on the back for this one.
    • thermometer.bc - Twenty functions to convert temperatures between the five most common temperature scales: Celcius (Centigrade), Farenheit, Kelvin, Reamur and Rankine.
Random Numbers

"What's this?" I hear you cry. Random numbers in bc? Yes and no. GNU bc, let alone POSIX bc has no access to real-world information that a user can't provide, so while it's perfectly feasible to implement a random number generator in bc, there's no way to seed it. i.e. It hasn't got a starter motor to get it going. That's never stopped me before though.

  • rand.bc - A floating point random number generator of my own devising. It passes all standard distribution and chi-square testing, and is based entirely around floating point numbers. This of course makes it much slower than, say, a mersenne twister, but it's just as good otherwise. Update: added a tweak to prevent random seed decay that can occur if scale isn't set high enough on loading.
  • guess.bc - It's the old higher-lower guess-the-number game. bc wasn't meant for this sort of thing so it's time to buck the trend.
  • randbc - A bash shell script that starts rand.bc and seeds the random number generator using it's own random numbers. i.e. Jump-leads.
Weird Functions
  • The 'sum of repeated square roots' function
    Okay, so I couldn't come up with a better name for this one. To calculate this function of/for a number 'n', first take the square root of n, remember it, add it to a running total and subtract one from the total. Do the same with the square root remembered from before, repeat until the remembered square root is reduced to 1. (And thus the running total is getting nothing added to it).

    The mathematical formula (assuming your browser will handle this) for the function is:
    ß(x) =
    Σ
    n = 1
    x2-n - 1

    Or as simply as possible: ss(x) = sum[n=1..oo] x^(2^(-n))-1
  • The Pan-Digital Halving Index
    This strange little function returns how many times a number must be divided by two before all digits, zero to nine, are within the decimal representation of the result. There's an example in the source file if that's confusing...
  • The Angular Logarithm function
    Yes, it's true, I suck at naming things. This function and it's inverse (included in the source) are related to the number sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, etc. etc. [See the OEIS's A037124]
    • Sharp eyed readers might notice that this should be an 'angular exponential' and not an angular logarithm. I'll get round to changing it eventually.

 

 
This site should work with most browsers
though there are some advanced features
that will only work with more recent software
  that mail thing | web{}phodd.net