Posts Tagged ‘calculator’

szg The Calculator

December 2nd, 2011 subogero 8 comments

Get szg now!

Get szg now!

szg is the Lazy Man’s command line calculator, crunching the maximum number of numbers with the minimum number of keystrokes.

szg stands for SZámolóGép, which is Hungarian for calculator.
It also happens to be the initials of the author, a particularly Lazy Man.

If you you’re lazy too;

If you like 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 szg.


  • 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
  • input of expressions directly from the command line
  • output of calculations on stdout
  • error messages (syntax error, division by zero) or stderr
  • unlimited undo
  • user defined variables like $foo
  • operators ()~ ^ */%& +-| =
  • symbol _ means last result, like Perl/Python
  • missing identifier means _ (+5 means last result + 5) like Perl (sorry Python)
  • math functions @s @c @a @l @e @r = sin cos atan log exp sqrt
  • optional unsigned arithmetic (commands N, S)
  • floating-point not upon division, only by user request, like Python (but not Python3)
  • combination if expressions/commands on same line
  • comments starting with #
  • interpreter mode when input file specified
  • manual page


Debian Packages

The easiest way to install szg is using apt-get on Debian-based systems. Add my Debian repo to /etc/apt/sources.list:

deb /
deb-src /

And then

# apt-get update
# apt-get install szg

Source Tarball

From get szg_X.Y.orig.tar.gz.
szg 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 the newest one, then

$ cd szg-X.Y
$ ./configure
$ make
# make install # as root


From get
Download and extract the newest one, then copy szg.exe to C:\WINDOWS or another folder which is on PATH.

Git for the Brave

$ git clone || \
  git clone
$ cd szg
$ ./configure
$ make
$ make install # as root


Desktop calculator workflow with a twist

d 5000    # one steak
d +3*2000 # three whiskies neat
d +1000   # tip
d 20000-  # Let's see how much money I've got left from my 20000 note

Command/statement combination for hex/dec conversion, signed/unsigned modes. Watch the prompt!

d XffffD       # change to hex, enter FFFF, change back to dec
d X ffffffff D # change to hex, enter FFFFFFFF, change back to dec
d N            # unsigned (natural number) mode
D 240X         # stay in dec, enter 240, change to hex
X D239X        # change to dec, enter 239, change to hex

Undo, difference between subtraction and unary minus.

d ~42        # Let's subtract 42 ...
d U-42       # Oops, I hit tilde instead of minus

szg works as an interpreter as well, an input file can be specified on the command line. This also allows to create executable szg interpreter scripts. For instance let’s create an executable file called LifeUniverseAndEverything:

33*2 X

szg supports floats and binary formats. You can even peek into the insides of floats.

D 1+2
d /2    # no implicit float-conversion upon division
d /2.0  # Pythonic float-conversion if float-format entered
f B     # let's peek inside
00111111 00000000 00000000 00000000

You can specify an expression on the command line, to maximize laziness:

$ szg -e XffD


This is the first remake of my first shot at a unix-style program. In the first shot you had to enter too many capital letters for hex numbers.
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.

Undo is possible thanks to a stack data-structure and the user-defined variables’ heart and soul is a hash-table, with buckets and all that.

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.

ogc 6.0rc2

July 18th, 2011 subogero Comments off

ogc 6.0 rc2 is out for the general public to enjoy. It now supports any number of user defined variables in the form of $FOO or $SPAM. Dollar sign plus all-caps. I know, it’s somewhat LarryWallesque. The dollar-sign, not the all-caps, you understand.

Just to show the severity of my mental state, I’ve implemented “hash-buckets” in C. With vi.

ogc 5.0

May 7th, 2011 subogero Comments off

The new version of the Lazy Man’s calculator maximizes laziness by taking your expressions directly from the command line. No pipes, no interactivity, no interpreter-scripts. Just what the customer wants.

$ ogc -e 2*21

The clever customer can actually take ogc’s laziness to even greater extremes. Just add the following line to ~/.bashrc

alias og='ogc -e'

And voilá:

$ og xFFd

You can download sources and Linux/Win32 binaries from the ogc page.

Geeks can clone the git repo as well:

git clone
git clone

ogc 4.2 – Math Functions

August 22nd, 2010 subogero Comments off

The introduction of the lex (flex) tokenizer into the ogc development allows an incredible amount of new bloatware to be implemented. The first menacing omen was floating-point support.

And now it’s math functions. Our friends from <math.h> sin, cos, atan, log, exp, sqrt are available as @s @c @a @l @e @r. The little bastards, besides looking very ugly, also perform a sneaky implicit floating-point conversion.

Check it out. But where will this all end?

ogc 4.1 – Floating But Not Sinking

August 3rd, 2010 subogero Comments off

Erm… ogc 4.0 had some certain erm… bugs in its syntax.

But ogc 4.1 is now downloadable from the ogc page. Including native Linux and Windows binaries.

ogc – The OG Calculator

July 29th, 2009 subogero Comments off

So my yacc studies led me to a series of thoughts about writing my own command-line calculator with integer arithmetics. Which the Windows Calculator is not.

I set the following goals:

  • 32-bit integer arithmetics (don’t ask me why, I just need it at work, OK?)
  • decimal, octal and hexadecimal formats
  • unlimited undo
  • a fair amount of memory registers
  • ability to continue from the last result (+5 should add 5 to the last result)
  • no mouse clicking
  • following Unix/GNU conventions

The program’s called ogc. I wrote it in yacc surprisingly quickly. The related info and the sources are available on my dedicated ogc page.

Categories: posts Tags: , , ,