# How do you program Babbage's Difference Engine?

We know that Charles Babbage wrote a book of log tables:

In doing this, Babbage was inspired to create Log Tables mechanically, leading to his design of the Difference Engine.

The Difference Engine didn't produce Log Tables, but a 'tabulation of polynomial functions'.

We can see that the operation of the Difference engine could produce a table that looked like this.

The difference engine exists today in the Science Museum in London, and was rebuilt in 1991.

Now it seems to me that you either input a polynomial to be evaluated(doesn't seem possible with the technology), or you set up a series of differences in the third column to be summed up into the first column.

My question is: How do you program Babbage's Difference Engine?

• The Analytical Engine was programmable, but the Difference Engine was not. Sep 22 '19 at 12:57

How do you program Babbage's Difference Engine?

Simply you don't, as it is not programmable. (*1)

A Difference Engine is a fixed sequence adder.

## Workings

A differential engine adds a series of values, called columns (registers), in the form `R(n) = R(n) + R(n+1)` with `n` ranging from 1 to the number of registers - in case of Babbage's machine six.

From an engineering point the machine isn't anything different from a pinwheel calculator, which, being able to solve a multiplication as a sequence of additions, is still just an adder.

Well, the Difference Engine is maybe special the way of organizing the adders in two groups to allow parallel operation. It only needs a fixed number of two calculation steps, independent of the number of columns, as a side effect the optimal number of columns is even. Calculation is done in two steps with all even columns in step 1 and all odd in step 2. A very simple and admirable way to increase performance due parallel processing.

Beside the number of columns, which defines the complexity of polynomial that can be calculated/approximated, there is the number of digits per column defining the precision. Babbage's machine was planned for 20, the prototype had 6, the 1991 #2 has 31.

Like the mentioned multiplication by a pinwheel calculator, a differential engine is not programmable at all (*2).

## Use Case

The specific use case comes not from its mechanics or any 'programming', but the fact that a wide range of mathematical descriptions can be turned into a polynomial function, which again can be expressed as a series of addition, subtractions and multiplications, which then again can be reduced into simple additions.

And that's what the Difference Engines are about: Adding up a fixed sequence of intermediate values.

## Change of Operation

The only change that could be done to a difference engine, much like to any adder, is to change the gearing to work in different number systems. Like using base 60 gears to calculate trigonometry or 1/20/12 gears to handle English currency, the later a use case for interest tables.

## 'Standard' Implementations

Since a difference engine is just a series of adders, it can as well be formed from any machine capable to add a series of values and keep track of each. Notable later examples showing this might be

*1 - As Tofro already pointed out.

*2 - Going as far as JeremyP does and call the preparation of the input programming might be quite over the edge, as it would turn any preparation of input values into programming, effectively making the term equivalent to preparation and thus meaningless.

• I hesitated to do the technical explanation of the inner workings (I consider this off-topic. In addition [pun intended] I consider myself lazy). But, still +1 for the effort. And I strongly agree there is no programming involved. Sep 23 '19 at 17:26

Not at all.

The Difference Engine is not what we would call a programmable device. (So, it would also be considered off-topic for this site). It's more a slide rule than a computer.

The polynomal approximations to mathemathical functions are "hard-coded" into the gear transmission ratio.

What your linked Wikipedia article refers to as "programming" is only setting initial values.

• @vsz you seem to have confused the Difference Engine with the Analytical Engine Sep 22 '19 at 19:42
• The difference engine is programmable. It can be programmed to calculate the values of a wide variety of polynomials. It is not Turing complete because it can't do anything else. Sep 23 '19 at 9:04
• @JeremyP Defnitely depends on your definition of programmable. Setting initial values to counters is not what I would call programmable. Sep 23 '19 at 9:17
• @JeremyP Is shifting gears in a car programming? Sep 23 '19 at 9:40
• I'm afraid that an explanation of a non-computer would be off-topic in a retrocomputing stack Sep 23 '19 at 12:55

Now it seems to me that you either input a polynomial to be evaluated(doesn't seem possible with the technology), or you set up a series of differences in the third column to be summed up into the first column.

In your example, the table is one way to represent the polynomial. So you have effectively input it.

If you repeatedly differentiate any polynomial function, you eventually end up with a constant. The first derivative of 2x2-3x+2 is 4x-3. The second derivative is simply 4. This means that, if you take the difference between successive values of the polynomial (for integer x), and put them in a column, then have another column which is the differences of the last column and so on, eventually you get a column where all the numbers of are the same as in the third column of your example.

Then you can calculate new values of the polynomial by adding the last column to the second to last column, then the new second to last column to the third to last column etc until you get to the first column. In your example, row 3 of column 3 must have a 4 in it which means row 4 of column 2 must have 11+4 = 15 in it which means the value for x = 5 is 22 + 55 = 37.

So the Difference Engine calculates successive values of a polynomial simply by doing repeated additions.

In maths a lot of functions can be approximated by polynomials. So you find a polynomial that approximates your function, set the Difference Engine up for that polynomial and then start churning out values. You have to be careful, because, after a while the error in the approximation will be greater than your desired accuracy and you have to stop.

• I think you don't even have to differentiate, i.e. take the limit of f(x+h) - f(x) as h tends to 0. If it's a polynomial, f(x), then f(x + h) - f(x) is always a one-order-lower polynomial. I'm leaving this comment just in case anybody thinks there's something fishy in taking discrete steps in calculating the polynomial's 'next' value. There's not. The number of levels of addition sets a limit on the complexity of the polynomial but you're still calculating the one you settle on exactly. Sep 23 '19 at 19:30
• @Tommy Erm, no. It is not always exact. It is only exact until a certain number of iterations, defined due the input values as well as the number of valid digits the calculation is done. After that it'll be an approximation due dropped digits (dropping is not rounding). Continued calculation will make the result diverge ever more. That's the reason why difference machines were never used to calculate full tables at once, but rather to fill the room between known (hand/otherwise calculated) base values. That's why selecting the right start values - and their representation - is a qualified task Sep 23 '19 at 20:08
• @Raffzahn number theory has bitten me again; what I meant to say: the process of differencing and then performing a cascading add in order to make discrete steps along a polynomial will produce exactly correct steps along that polynomial. There's no Euler-esque approximation here. That said, as well as admitting that number of columns limits polynomial complexity you're absolutely right to point out, and I was wrong to omit, the observation that finitely-sized buckets will at some point lose data. I really just meant to get at the point that this isn't an approximate integral. Sep 23 '19 at 21:15
• @Tommy Yes, from a mathematical point you're absolut right. Sorry for being so nasty. I'm to much of an engineer to let this slip. :) Sep 23 '19 at 22:46
• When I was a maths undergraduate in the 1960s, we used this method with mechanical calculators to interpolate values in six-figure log tables. Sep 24 '19 at 12:56

By our definition of programming of the last 30 years - you didn't program it, you merely gave integer parameters to a function and let it loop a few times and provide an integer answer.

Keep in mind people who performed mathematical calculations given to them during World War II were called "Computers." (i.e. computing can mean calculating and programming can mean scheduling something - like a TV program).

Charles Babbage took took much longer than he thought to design his Difference Engine; only a small part got built in his lifetime.

This is from the Science Museum in London.

There are a number of parts to this answer:

## How do you calculate log₁₀ 60 without a calculator?

We can see the row for log₁₀ 60 in the book above. First we will cover off some assumed knowledge in Babbage's book:

The logarithm is the inverse function to exponentiation. This means:

• log₁₀ 1 = 0
• log₁₀ 10 = 1
• log₁₀ 100 = 2

So log₁₀ 60 must be between 1 and 2.

Now as @xxavier has suggested - we can do logarithms using a Taylor series - ie

but this approximation only works for logs where 0 < x < 1.

Also note that this is natural log not base 10 log, so we'll have to convert back later.

So how do we get to log₁₀ 60?

We look at another Logarithmic identity:

How does that help?

We can take what we did before - and simplify things:

so

Now we can go back to our Taylor series approximation:

Which is pretty close to a value provided by a calculator (-0.510826).

Now we convert back to log₁₀ n using another logarithmic identity.

This means we can do:

So to convert back to

we can divide by

which is approx. 2.302.

So now

Which looks close to what Babbage got:

So we can confidently calculate a logarithm by hand.

But the Difference Engine can't expand Taylor series, it uses the Finite Differences Method.

## How do you calculate a logarithm by the Finite Differences Method?

Babbage used the approach of Gaspard de Prony.

We'll use our Binomial expansion above to the sixth order:

We'll do this for a sixth order polynomial:

As an Excel function - this looks like this:

``````=(C161-1)-(1/2)*POWER((C161-1),2)
+(1/3)*POWER((C161-1),3)-(1/4)*POWER((C161-1),4)
+(1/5)*POWER((C161-1),5)-(1/6)*POWER((C161-1),6)
``````

Now we build a table for this polynomial of sum of the differences between 0 and 1:

Now we shuffle top of columns to top row

Then we fill in first row with precomputed values, then populate each cell by adding the cell above and the cell above right:

So again we get

which we converted back to base 10 above.

You can see a detailed video of the finite differences method here.

## How do we use Finite Differences in relation to the Difference Engine?

When initialising the engine - you can set values:

The values you are setting are the top row of the Finite Differences method from before.

Babbage's Difference Engine No.2 was designed for 7th order polynomials (our calculation was sixth order). This means our Finite Differences Method would have had 7 columns.

On the Difference Engine each vertical axle represents a number with 31 digits - with a gear corresponding to each digit, the most significant digit at the top:

Note that the Difference Engine represents negative numbers using tens complement.

So to enter our first value -0.10536 - we have to convert it to tens complement.

10's complement of a decimal number can be found by adding 1 to the 9's complement of that decimal number. It is just like 2's complement in binary number representation. For example, let us take a decimal number 10536; 9's complement of this number will be 99999-10536 which will be 89463. Now 10s complement will be 89463+1=89464.

(I'm assuming - similar to digital logic - there is a way to flag a 10s complement number - but that wasn't available to me at the time of writing.)

So assuming we are entering the value 0.89464 - this means we'll need to turn the 31 wheels to look like:

(We have entered a decimal fraction as an integer representing the numbers scaled up - and assume that it will be scaled back down later).

Then we repeat this activity for the other 6 columns of the table - against the next 6 vertical axles of gear-values.

How does the Difference Engine Add Numbers?

In our Finite Differences method - we had to take the cell above, and the cell above right.

Here we have two wheels representing values in the calculation, and the gear in the middle that adds.

How does the Difference Engine carry values to the next significant digit during addition?

To carry numbers up - there was an external device:

This was stacked in an vertical array for each value gear on the axle:

You can see the addition gear and the carry mechanism here:

In the context of the 1800s, when ships were getting lost or crashing, and reliable log tables were worth real money, the Difference Engine was (and is) a machine of great beauty.

(The difference engine in the London Science Museum)