A4 as a Calculator

Type a4 new my-file to create a new a4 file.

This will open a shell allowing you to make calculations:

<A4> 1 + 1
  -> 2

You can set variables with the <- operator:

<A4> x <- 2*4
  -> 8
<A4> x + 1
  -> 9

You can press CTRL+D to save and exit.

Advanced Feature - Persistence

For your convenience, every accessible value is saved to disk by default.

To open the same file again, just type a4 resume my-file.

Then you can check that the variable is still remembered:

<A4> x
  -> 8

(Saving happens automatically with the a4 new and a4 resume commands. You can disable saving by using the a4 new-and-forget or a4 resume-and-forget commands.)

Advanced Feature - Exact Numbers

Many calculators and programming languages use limited-precision numbers by default. This means that calculations on very large and/or very small numbers will often result in crazy errors. Different types of issues arise depending on the number format used.

No Integer Overflows!

Many calculators and programming languages use a fixed-size format for storing integers (whole numbers). These formats are very efficient, but they don't behave intuitively for large numbers.

For example, in Java, which uses a 32-bit signed integer format by default, 3 * 1000000000 will result in a negative number (unless you specify a more precise number format). This means that if you're writing a Java program that helps you do taxes for billionaires, you could end up in a wee bit of trouble. (This also applies to C, C++, C#, Pascal and most other languages that default to limited-precision integer types.)

A4 manages integer formats automatically, so small numbers will still only take up 32 bits, but when the result of an operation "overflows" like it does in the other languages, it's automatically converted into a larger format. In other words, 3 * 1000000000 is 3000000000. (Some other languages, such as Python and Scheme, use a similar system for managing integer formats - and you can use large integers in Java and the other languages too, it just isn't anywhere near as easy.)

Exact Fractions!

In most programming languages, something like 1 / 3 will result in either 0 or something like 0.3333 (depending on whether an integer or floating-point format is used). The former is the desirable outcome in some cases (if you specifically need an integer result), as is the latter in some other cases, but neither is a sane default.

A4 will use the 0.3333 behaviour only if the numbers you're operating on are already in floating-point format - otherwise, it will store rational numbers exactly (so 1/3 is just stored as 1/3). You can always limit the precision manually if you want a less accurate number!

Things To Watch Out For

A4 isn't perfect, and design is all about compromise.

Order Of Operations

Because the language was designed to be extremely simple and generic, Smalltalk doesn't support special order-of-operation rules that are normally used in mathematics.

This means that 1 + 2 * 3 evaluates to 9 (or (1 + 2) * 3), as opposed to 7 (or 1 + (2 * 3)).