bc? basic calculator.
I've mentioned bc before relative to calculating π. It's a great little basic calculator for *nix systems. Programmable. Quite literally all the precision you could ever want, if you want it.
If you remember using the old BASIC language interpreters as a substitute for a desktop calculator, and don't like having to open up a spreadsheet document just to check your math on something, bc may be what you're looking for.
bc is one of the packages available for cygwin distribution, so the following is not by any means the only way to get it onto your MSWindows PC. But you can use MinGW to install it, here is how.
[JMR201704271237:
I should note that, unless you really want to practice compiling someone else's source code, I recommend getting Cygwin and just installing bc from the packages with a few button clicks. You can still compile from source using Cygwin, if you want to do that sometime. In the meantime, you can use bc and dc without jumping through extra hoops.
]
bc is not an official package of MinGW, so this is a good way to practice compiling and installing free software by hand.
First, find the source code. As I type this, the wikipedia pages have a link to the source code (alpha, too) on the gnu servers. (The pages also have a link to the bc package from the separate GnuWin32 project on SourceForge, but then you wouldn't get the practice.) (Surprisingly, searching at google for "bc basic calculator source code" did not yield the gnu repositories on the first pages. It did bring us back to the wikipedia pages, so it got close.)
So, download the source code from the gnu servers. (Not the alpha version, I haven't yet figured out how to get that to compile and run on MinGW. Unless you're good at debugging these, in which case, please leave some suggestions in the comments.)
You get a tarball, which stock MSWindows does not know how to deal with. But that's okay, because MinGW should have the tar utility installed with it. (If you type "tar" at the msys shell command line and it says it doesn't understand, check the packages and install the appropriate package with "mingw-get install". But mingw-get isn't going to work without tar, so I guess that should never happen.)
Save or copy the tarball into your user's home directory:
c:/MinGW/msys/1.0/home/username
If you don't already have an msys shell running, go to the MinGW folder in the Start menu and get an msys shell running. (Not the uninstall icon, of course.) Enter the following commands at the prompt:
tar -zxvf bc-1.06.tar.gz
ls bc*
(tar didn't used to accept leading hyphen on the options, now it does.) The directory list should show that the tarball has been unpacked. Move into the bc-1.06 directory that tar just made and tell the configuration script to tell you what it can do:
cd bc-1.06
./configure --helpYou probably want to add the readline option or the bsd version of it. (Don't need both.) If you plan, for some reason, on cloning your installation and distributing it to others, use the bsd version to keep yourself in conformance with the license. (I assume you are not going to cloning your installation tree, but I'll remind you just in case.) Here's how to configure it with the bsd version of line editing:
./configure --with-libedit
This will take a few minutes and tell you lots of things you may not really understand. Scan the output but don't worry about the stuff you don't understand unless it says "ERROR!" and stops. Warnings, you don't need to worry about just yet, either. Sort of.
When the prompt returns to you, unless there have been errors, you can make the executable:
make
Make handles the compiler commands for you. Are you let down? Don't be. Look back at the compiler output. ("make clean; make" again if you've lost the output.) You'll see some key warnings. The ones about memset and related functions are not that important, but you can edit lib/number.c (the file number.c in the directory lib) and add the line
#include <string.h>
at the end of the other include lines to get rid of that.
Make it again. (You may want to do a "make clean" just to be sure.) Test the results by typing the command
bc/bc
Once you get the copyright message so you know you are in the bc shell, type
scale = 100
17/11
You should see 1.5454 ... stretching out for 100 places. Now type
exit
to get back the the msys shell and type
bc/bc -l
to start bc with the math library. This is where things get good. Except you have to fix something first. In bc, type
4 * a(1)
That's 4 times the arctangent of 1. It should type the value of pi out to twenty places. (The last digit will be incorrect.)
3.14159265358979323844
But it probably won't.
Until somebody at gnu.org decides to update the package, it will probably complain about segmentation faults and the like. I looked on the web for "bc segmentation fault string.h save_adr" or something like that and found an archived mailing list thread and a patch from the linux-from-scratch project.
If you wanted to practice applying patches you could have applied it before you added the line to #include string.h
But it's easy enough to do by hand. Open bc/load.c (the file "load.c" in the directory "bc") in your text editor, and look around line 159. You'll find the line
program_counter save_adr;
On that line, save_adr needs to be declared static. Change it to
static program_counter save_adr;
and save it. Run make again, and repeat the tests. Just for fun, try this:
scale = 1000
4 * a(1)
That should give you pi to a thousand places (the last digit being incorrect again).
Way cool, huh? Now you need to make bc more generally accessible, so you don't have to type the whole path all the time.
make install
and bc is now available as just "bc". (dc is also fun to play with.) After you install it, the manual pages should be available, so you can type
man bc
to get more information about the cool things you can do with it. You can also look in the Examples directory for ideas.
I'll be using bc in some of my posts to the math-and-English blog, too.
[JMR20190316: math and English blog post on calculating π.
]
I've just got this working:
ReplyDelete1) Installed mingw via the installer, I added the gcc and c++ compilers (see http://www.mingw.org/wiki/getting_started)
2) Install wget - I used command line here "mingw-get install mysys-wget"
3) wget bc - "wget http://ftp.gnu.org/gnu/bc/bc-1.07.tar.gz"
4) unpack - "tar xfz bc-1.07.tar.gz"
5) Change to the new directory - "cd bc-1.07"
6) ./configure
7) make will fail here with an error about random, the fix is detailed here - https://stackoverflow.com/questions/27186706/spin-verification-undefined-reference-to-random-and-srandom
8) With this change made, build and install - "make" then "make install"
Done, a working bc :-D
Glenn
I can't take a look at this today, but, just as a matter of principle, I would look for something besides rand() and srand() for the defines.
DeleteThere are of course questions of how you intend to use bc that need to be considered. For some uses, rand() and srand() would actually be indicated.