Website title

Computing: The Details
Lex's blog of curious computer-related shenanigans.

Blog post

Why does my car think that 38 - 2 = 34?

Published: 2025-06-01

This blog post is also available in video form! Watch it on YouTube

I can't explain this bug. It makes no sense to me. 38 minus 2 is not 34. It is 36. Obviously!

Suppose I have a computer program which has one variable called A. The program waits for an input of either "up" or "down", and then adds two to A or subtracts two from A and uses that as the new value for A.

Let's say A starts at 20. I give it the "up" input, and now A is 22. I give it the "up" input again, and now A is 24. I give it the "up" input 8 more times, and A goes through 26, 28, 30, 32, 34, 36, 38, and ends up on 40. This is all fine.

But if I then started giving it the "down" input repeatedly from A=40, and the sequence of values of A started 40, 38, 34, then you'd be surprised. You'd be thinking "hmm, this program is clearly broken. It is faulty. There is a bug here!"

Suppose you keep testing the program as far as you are able to. You go up and down the full range of numbers that the program can handle. There's no other cases where it jumps by 4 instead of by 2. It's always a step of 2, unless you go down from 38, in which case it steps by 4 and lands on 34.

I know what you're thinking. You're thinking that surely nobody has ever written software that has this bug. How could anyone possibly put a bug like that in to such a simple program?

Surely this isn't a bug that exists in the wild.

Finding this bug in the wild

The last time I owned a car, it was because I got it for free from a friend of mine. He had crashed his Smart Roadster in to the back of another car, didn't want to repair it, and didn't really use it much any more anyway. So I got this heap of metal on wheels for no charge. The retractable roof no longer retracted, instead it just leaked. The CD player was faulty in various ways. Some parts fell of it sometimes. It was hopelessly impractical for a lot of tasks. But it was sometimes useful, and the engine ran perfectly fine, so I decided the squeeze the remaining life out of it. I did repair the bumper, get it past one MoT safety inspection, and make use of the car for a while after that, but strategically sold it to a car recycler just before the next MoT inspection was due.

(MoT stands for Ministry of Transport, a UK government agency that has since been renamed to the Department for Transport (DfT), but everyone still refers to the legally mandated annual vehicle safety inspections as "MoT tests")

A terrible old car (A red Smart Roadster with a smashed bumper and a filthy roof)

That car had a speed limiter feature. I used it every time I drove the car. It's really handy. Any time I see a speed limit sign, I set the car's speed limiter to that limit, and then I don't have to worry about how fast I'm going. The car won't let me break the speed limit.

The minimum speed setting is 20mph. So, you can use the control on the stalk to increase or decrease the speed limit. Each click "up" adds 2 to the limit, up to a maximum of 120 miles per hour. I never need to go beyond 70mph though, since that's the highest speed limit on UK roads. A click "down" normally subtracts 2 from the limit, down to a minimum of 20mph. However, as you're probably expecting from the intro, a click down from 38mph subtracts 4 from the limit, and puts you on 34mph.

Most of the UK speed limits are multiples of ten. During most of the city driving I do I will only encounter speed limits of 20mph and 30mph, but I do often also use 40mph roads, and then there's 50, 60, and 70mph roads when you get out of the city. Most of the time I encounter a change of speed limit, I need to do 5 clicks in some direction to get to the new speed limit. Most of the time I'm adding or subtracting 10.

Before I discovered this bug, when leaving a 40mph zone and entering a 30mph zone, I would do 5 "down" clicks. This always resulted in me ending up on 28mph. I would then be mildly confused by the slightly slower speed than I expected, look at the display, see that it said 28, and do one "up" click to correct it.

I thought this was no big deal, until I realised that I consistently have issues with going from 40mph to 30mph. At some point while I had the car parked on my driveway, I played with the controls and discovered that I was not imagining things. I was not just failing to use the controls correctly. It was not detecting more than one input for each click of the control. It consistently jumps from 38 to 34.

How do we explain this?

I'm the kind of person that really enjoys writing computer code. I started writing code as a hobby when I was very young, and spent my entire career working in tech writing code all day. I've spent a significant amount of my life writing code. I have never come across a bug like this before, and I have not yet encountered one since.

I don't have access to the source code for this system, and I decided not to extract the code from the car to reverse engineer the binary. As fun as that would be to do, it will be a lot of effort to disassemble the car to the point where I can even find the chip that the software for this is stored on. I didn't want to take my car apart that far just for this bug.

So, let's think of some hypothetical ways that this bug could happen. I'm going to use C code for demonstration purposes. Here's a really simple thing that the program could be doing:

int limit = 20;
int get_limit(){ return limit; }

// called on up click
void up(){
    limit += 2;
    if (limit > 120) limit = 120;
}

// called on down click
void down(){
    limit -= 2;
    if (limit < 20) limit = 20;
}

This code works perfectly. No issues. We have some really simple code here, and it works fine. So clearly we're missing something, because we don't have the buggy behaviour yet, which is what we expected.

This is the first thing that anyone would write if asked to implement this. So, given that it works just fine: how did this bug get in there? Who was it that got to this point and thought “I can do better than this” and proceeded to write some code that gets basic arithmetic so obviously wrong?

If I wanted to provide some explanation for this bug and did not care about how similar my example code is to the real code, I could modify the down function to look like this.

// called on down click
void down(){
    limit -= 2;
    if (limit == 36) limit = 34;
    if (limit < 20) limit = 20;
}

I add in an extra check that looks for the value 36, on the way down, and jumps to 34 instead.

This perfectly reproduces the behaviour that I've observed, but it is not at all satisfying as an explanation, because nobody would ever write that code. Any code that goes in to a car, and can interact with critical systems within the car, such as the accelerator, goes through a very strict process that aims to keep the quality of the code high.

I don't believe that source code like this would ever be allowed into a critical system on any car. The bug can't be as obvious as this. It must be something far more subtle.

So what is it? What is the subtle bug? Well, I have two ideas to explore, but I don't like either of them as a good explanation.

Bad explanation 1: Look up tables

Idea 1 is that they didn’t use arithmetic to determine the next value, but instead had a lookup table that was manually written and contained a mistake.

This one is more plausible than just having an extra check to deliberately skip 36, but I still don’t like it. It still feels unlikely.

Suppose for example that I still have my variable called “limit” and it still stores the limit in miles per hour. Now I have a lookup table that maps a speed limit value to the next value up, and another that maps a speed limit value to the next value down.

int limit = 20;
int get_limit(){ return limit; }

int uptable[] = {
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    22,0,24,0,26,0,28,0,30,0,
    32,0,34,0,36,0,38,0,40,0,
    42,0,44,0,46,0,48,0,50,0,
    52,0,54,0,56,0,58,0,60,0,
    62,0,64,0,66,0,68,0,70,0,
    72,0,74,0,76,0,78,0,80,0,
    82,0,84,0,86,0,88,0,90,0,
    92,0,94,0,96,0,98,0,100,0,
    102,0,104,0,106,0,108,0,
    110,0,112,0,114,0,116,0,
    118,0,120,0,120
};

int downtable[] = {
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    20,0,20,0,22,0,24,0,26,0,28,0,30,0,
    32,0,34,0,34,0,38,0,40,0,
    42,0,44,0,46,0,48,0,50,0,
    52,0,54,0,56,0,58,0,60,0,
    62,0,64,0,66,0,68,0,70,0,
    72,0,74,0,76,0,78,0,80,0,
    82,0,84,0,86,0,88,0,90,0,
    92,0,94,0,96,0,98,0,100,0,
    102,0,104,0,106,0,108,0,
    110,0,112,0,114,0,116,0,
    118,
};

void up(){ limit = uptable[limit]; }
void down(){ limit = downtable[limit]; }

Urgh! That looks awful! Each table needs to have a huge blank area at the start full of zeroes for all the values that are impossible to reach, and a placeholder value in between each number, since it always steps two at a time. I’ve just put 0 in these too.

Yes I could do some more address computations to avoid all those unused entries in the table, but that wouldn't be the worst sin of this code.

Why am I even suggesting this? Well it does have some interesting properties that might be what the car manufacturer wanted to have. The advantage of this approach is that each of the up and down functions is doing exactly the same thing every time, with no branching paths in either one. That is, there is no if statement any more. It always looks up the new value in the table and stores it.

You no longer need to test if you are at the end of the range. The lookup table for going up maps 120 mph to 120 mph, and the downwards one maps 20 mph to 20 mph. There’s no need for the checks at the extremes of the range any more, they are handled by the tables.

Why is this useful? Sometimes a predictable execution time is useful. Especially in cryptography. The only problem with this theory is that it makes no sense to save that tiny bit of time spent branching. There’s no doubt a whole bunch of other code running in between the button press and the table lookup anyway.

There’s no way this code is on the critical path. Especially when you consider that it also lets you hold down the button to skip through the steps repeatedly. That must require a whole bunch of logic and timing stuff. It’s not going to be a timing constraint.

I seriously doubt that’s the reason. I also can’t think of any better reason to do it like this, and that’s why I doubt that this is the case. It’s a little more satisfying than the very deliberate bug from earlier, because the bug is really subtle this time. You have to look carefully at the tables to see it, instead of just immediately seeing something suspicious. But I'm still not convinced

Bad explanation 2: Is it floats?

Idea 2 can be summarised as “some sequence of floating point rounding errors."

Okay, first of all. If you’re writing code for an embedded system, especially when it’s embedded in a car, you should really think carefully about if you really need floating point arithmetic before you actually use it.

Floating point rounding errors can cause nasty bugs. Embedded hardware often doesn’t even have floating point arithmetic units, and so it is implemented in software, which is much slower than hardware, and certainly much slower than integer arithmetic.

But let’s put that aside for a moment. Maybe they did use floating point. Maybe, because this is a German car, all of the software was written with the assumption that all the base units were in Kilometres, and then they added in support for places where the speeds and distances are measured in miles by having a conversion layer on top of the kilometres implementation.

Well, that raises some challenges, Suppose your underlying control software needs to have the speed limit specified in kilometers per hour and you don’t want to change that. You can’t just have the displayed number be converted to miles per hour, and leave the buttons to add and subtract 2 kph, because then you’d end up with an awful user experience far worse than this bug. Pressing up would add about 1.24 miles per hour. You need to also convert the up and down numbers.

So maybe you make the up and down buttons add and subtract about 3.22 kph, then you can keep the limit stored in kph, apply the translation only when you display the number, and the control system can stay the same. You’ll need to do some rounding to be sure everything works, but this could be okay.

Okay, but how much rounding is too much? Let’s try specifying everything to two decimal places, and rounding to the nearest integer whenever we display the limit. That works! How about we get even more approximate? Only one decimal place. That works too.

Okay how about everything is just the nearest integer, except for the conversion factor? That also works. You can be really approximate about this and it still works fine! It’s not a speed unit conversion problem.

But wait a minute, what’s going to happen if there is a rounding error? At these low numbers, the rounding errors are going to be incredibly small. We’re not even going to see an error big enough to cause a problem when rounding to the nearest integer. We’re not even seeing a scenario where we are off by one, how could this possibly lead to being off by two!?

Let’s be a little more systematic about this. If there is some rounding that leads to this problem, there must be a scale where one of the converted numbers rounds to 34, and the next one up rounds to 38. What would the step size need to be?

The lowest value that can round to 38 is 37.5. You can exactly represent 37.5 in a single precision float. The next float below that one would round down to 37. The greatest value that can round down to 34 is 34.99996185302734375, which is the next float value below 34.5, which would round to 35. The difference between these values, according to regular floating point arithmetic is precisely 3.000003814697265625, which is very slightly more than 3.

Well clearly that’s no use. If I was to use 3 as the step size, then I can get this sudden step from 38 to 34, but then if I go down again from there I get to 31, not 32. So certainly this isn’t what’s going on!

We need a step size that’s close to 2, and we need a rounding function that rounds to the nearest 2. We need it to round to correctly for all numbers in the range 20 to 120, except for the number one step below 38.

Turns out this is kinda possible! If you start at 20.82 and you make steps of 2.024, then by the time you get to the number that rounds to 34, you’re actually at about 34.988, and then you take one more step and you’re at about 37.012, and the closest multiple of 2 to that number is 38, so you’ve skipped over 36. The problem is that this happens in both directions. The real bug that I experienced only skipped 36 when heading down!

So you might be thinking that there’s some rounding on the stored value on each step, but then the bug would be inconsistent. In my experience it would always skip 36 on the way down regardless of what other up and down motion had happened before that. It would never skip 36 on the way up, even if I’d been up and down across that boundary many times. If it was an accumulation of rounding errors over time then I’d expect it to be less predictable than that.

I thought about doing an exhaustive search of all possible floating point rounding modes, all possible step sizes, and all possible conversion factors, to account for the underlying data maybe being in some strange units that’s neither mph nor kph. I even started to write some code for doing that, and then realised that the search space is way too big, and so I gave up.

And that’s where I’m at. I have no idea how to explain this bug. How could someone get this wrong? I don’t know.

If you are someone that worked on the speed limiter software for Smart cars in the early 2000s, and you know for sure why this bug happened, then please please please get in touch and tell me all about how that system works. I would love to know.

Sorry this rant doesn’t have a satisfying conclusion. I wish it did.