ogc is the Lazy Man’s command line calculator, crunching the maximum number of numbers with the minimum number of keystrokes. The Lazy Man is called OG.
If you you’re lazy too;
If you’re fond of binary, octal and hex formats;
If you hate typing too much;
If you hate mouse-clicking even more;
If you hate calculators going floating-point after the first division;
If you still need floating-point occasionally;
If you typo a lot and prefer to undo them all;
If you like to comment your calculations (well, you don’t, but it’s possible):
Accept no substitute! Use ogc.
Features
- 32-bit integer or floating-point arithmetic
- decimal, binary, octal or hexadecimal format (commands d, b, o, x)
- input of expressions, variable assignments and commands on stdin
- output of calculations on stdout
- error messages (syntax error, division by zero) or stderr
- unlimited undo
- a fair amount of memory registers (G .. Z)
- operators ()~+- */% &| =
- symbol _ means last result, like Perl/Python
- math functions @s @c @a @l @e @r = sin cos atan log exp sqrt
- missing identifier means _ (+5 means last result + 5) like Perl
- optional unsigned arithmetic (commands n, s)
- floating-point mode never upon division, only upon user request, like Python
- combination if expressions/commands on same line
- comments starting with #
- interpreter mode when input file specified
- manual page
Installation
It can be compiled and installed on Linux or Windows/cygwin.
On Windows, it runs both in cygwin and Windows command line windows.
Download and extract in a new directory (why not ogc?);
Open a cygwin/terminal window there;
Call “make”;
Call “make install” or “sudo make install” (Linux)
Since 4.1, the tarball contains native Linux and Windows binaries.
Examples
Desktop calculator workflow
d 5000 # one steak
5000
d +3*2000 # three whiskies neat
11000
d +1000 # tip
12000
d # This is an expensive restaurant...
Command/statement combination for hex/dec conversion, signed/unsigned modes. Watch the prompt!
d xFFFFd # change to hex, enter FFFF, change back to dec
65535
d x FFFFFFFF d # change to hex, enter FFFFFFFF, change back to dec
-1
d n # unsigned (natural number) mode
4294967295
D 240x # stay in dec, enter 240, change to hex
F0
X d239x # change to dec, enter 239, change to hex
EF
X
Undo, difference between subtraction and unary minus.
84
d ~42 # Let's subtract 42 ...
-42
d u-42 # Oops, I hit tilde instead of minus
42
Since version 3.0, ogc works as an interpreter as well, an input file can be specified on the command line. This also allows to create executable ogc interpreter scripts. For instance let’s create an executable file called LifeUniverseAndEverything:
#!/usr/bin/ogc
33*2 x
Since version 4.1, ogc supports floats and binary formats. You can even peek into the insides of floats.
d 1+2
3
d /2 # no implicit float-conversion upon division
1
d /2.0 # Pythonic float-conversion if float-format entered
0.5
f b # let's peek inside
00111111 00000000 00000000 00000000
f
Background
This is my first shot at a unix-style program. It’s a filter, as in unix nearly everything should be. I tried to follow the Sacred Rules of the Art from esr’s book:
Command line option conventions
-h –help for help
-V –version for version info
Data driven development
The source of the usage and version screens are text files which look exactly like those screens. During make, they are sedded into header files with lists of string initializers. The headers are then included in the middle of const table definitions, to make the text available as a list of strings, line by line.
Write programs to write programs
I used Lex and Yacc to parse the input and to write the syntax. I can still hardly believe what these nearly 40 year old tools can do.