The aerospace bug
The story of Ariane 5 and how a small software decision can have major consequences.
TL;DR Too lazy to read? Click here.The bug that blew up a rocket
Exploring space has become such a common routine that technological advances no longer make the news with as much emphasis. Just at the end of 2018, for example, NASA managed to land the Insight probe on Mars, where it is now acting as a base for studying the planet’s interior.

We used a robotic arm to place a seismometer on the Martian soil, which will not only listen to but also measure asteroid impacts, landslides, and earthquakes. Knowing the frequency and intensity of marsquakes is extremely important so that we can start thinking about how we will build our housing developments there soon.
In addition, Insight dug a hole 5 meters deep over 3 months to check how much heat Mars lets escape.

A lot also happened here on Earth: the Space Station was resupplied by SpaceX rockets, and in Alcântara the Brazilian Air Force successfully took five experiments to an altitude of 120 km. What did not go so well was the return of the rocket that went to the Space Station, which, while trying to land, had a problem with one of its stabilizers and began to spin out of control. Thanks to incredible structural resilience and control algorithms, however, the rocket headed toward the sea, where it was then recovered without major damage.
These events serve as a reminder of how difficult it is to explore space, with unexpected things able to happen at any moment and for the most trivial reasons. In this article, I will describe a case involving Ariane 5, a rocket that cost 7 billion dollars to build and exploded 39 seconds into its first flight: an absurd loss that could have been prevented with a few more lines of code.
To understand how to handle unexpected failures, let’s start by writing a simple little program that will take two integers and divide them to give us a result. With that, we already have everything we need to write a Java method that calculates exactly that:
public class Div {
static int divisao(int a, int b) {
return (a/b);
}
public static void main(String args[]) {
System.out.println(divisao(25, 5));
System.out.println(divisao(40, 2));
System.out.println(divisao(100, 10));
}
}
In the code above, we perform ordinary division (25 by 5, 40 by 2, and 100 by 10). As you can see, our program seems to be working. The problem is that we forgot certain details! For example, when we enter 1 divided by 2, we get 0 instead of 0.5 because integer division in Java always rounds down.
To solve the problem above, we will introduce floating-point numbers, abbreviated as “floats,” into our code. Numbers stored this way can represent rational numbers by shifting the “.” that separates the integer part from the fractional part of a number, using scientific notation. The code will look like this:
public class Div {
static float divisao(float a, float b) {
return (a/b);
}
public static void main(String args[]) {
System.out.println(divisao(25, 5));
System.out.println(divisao(40, 2));
System.out.println(divisao(100, 10));
System.out.println(divisao(1, 2));
}
}
In our example, the point floats all the way out, and a multiplication by 10 to the power of -1 takes its place, giving us the expected result: 0.5. We increased the precision of our program’s results by using floats. If you know how to count in binary, what matters now is that the more bits we have to represent a floating-point number, the greater its precision will also be. Right now, we are programming with a 32-bit float, but Java gives us the option of using a “double,” which has 64.
Similarly, there is an integer type called “short” that uses 2 bytes instead of 4. Converting a number from one type to another is possible, but it can lead to unexpected situations that we will now explore.
Since each bit can hold 0 or 1, two bytes, 16 bits, can describe 2¹⁶ distinct numbers. Dividing these 65,536 possibilities by two, we can describe values ranging from -32,768 to 32,767. We simply pretend that numbers greater than that do not exist. But if, for some reason, our program returns a larger value, such as 42 thousand, we have to perform an operation called casting, which takes our 2-byte short and turns it into a 32-bit int. Magically, we have a new, much larger range that includes 42 thousand, and we happily carry on.
Without this operation, our number would have exceeded the established limits, spilling over into unknown territory. (They call this overflow.)
In programming, our number line is actually a circle, so 32,767 + 1 would result in -32,768. It is because of this circle that doing the reverse (casting 42 thousand to a short, where it does not fit) results in a negative number. The first 32 thousand of the 42 thousand fit, and the rest overflows into the other, negative half of the circle. One way to handle this is with exceptions.
One condition we have not protected ourselves against yet is what happens when dividing by 0: a fatal error occurs. We can therefore wrap these lines in a block that will try to execute our code but, if something goes wrong, say that something unexpected happened and safely end the program.
public class Div {
static float divisao(float a, float b) {
try {
return (a/b);
} catch (Exception e) {
System.out.println("Deu ruim, você dividiu por 0!");
return Integer.MAX_VALUE;
}
}
public static void main(String args[]) {
System.out.println(divisao(1, 0));
}
}
Now our program finally works as expected! These blocks that handle exceptions can protect us from various bugs, and their absence was partly responsible for the Ariane 5 accident.
The idea behind the European Space Agency’s development of Ariane 5 was to replace Ariane 4 with a more powerful rocket, capable of taking much larger payloads into space, such as two satellites weighing three tons each. With this, Europe was on the verge of becoming a benchmark in commercial space exploration after an investment of 7 billion dollars (27 billion reais) and 10 years of research. On this first flight, four uninsured satellites worth 500 million dollars were being carried. The flight trajectory itself was controlled by an onboard computer, which, 36.7 seconds after launch, thought that a course correction was needed because of very strange data the inertial guidance system was receiving from instruments such as accelerometers and gyroscopes. These bizarre and impossible readings were actually an error message that was interpreted as valid information; the system itself had crashed and was inoperative.
This shutdown was caused by an overflow when converting the rocket’s horizontal velocity (which was being stored in a double-precision, 64-bit float) to a 16-bit integer. Reproducing this same problem in our program with 84 thousand divided by 2, for example, we see that we get the same absurd result as before instead of 42 thousand, because the value simply does not fit in 16 bits.
public class Div {
static short divisao(double a, double b) {
return (short) (a/b);
}
public static void main(String args[]) {
System.out.println(divisao(84000, 2));
}
}
#Exemplo do código do foguete
This part of the rocket’s code was not protected by exceptions because the engineers had basically reused the code from Ariane 4, which was physically incapable of reaching such a high horizontal velocity!
And worse: this entire calculation was no longer useful at all after liftoff; its only purpose was to make sure everything was in order before launch. But in earlier versions of Ariane, the programmers had already decided to allow a 40-second grace period to make system restarts easier in the event of small delays during the countdown. In other words, when the older rockets were being developed, no one imagined that the source code would simply be reused in later versions.
And the scientists working on the more modern rocket, seeing portions of a program that were not used, did not test this part, but also did not remove it because doing so could cause a series of additional problems. With the spacecraft’s processor already so overloaded, why spend computational resources handling exceptions in sections that should not even be executed in the first place?
After the system shut down, control was passed to a backup that was completely redundant because it was an exact copy, leading to the same error mere milliseconds later. As a result, the rocket made an unnecessary course correction, compensating for an incorrect movement that had not occurred. From that point on, Ariane 5 was nothing more than a missile with a surprise target. The forces involved began to tear the rocket apart, which then correctly triggered its self-destruct routine because of the aerodynamic stresses that were ripping off the boosters.
And that is how, at an altitude of four kilometers over the swamps of French Guiana, what must have been the most expensive fireworks display in history took place.

Today, Ariane 5 is one of the most reliable rockets, with 97 of its 102 launches successful. Exploring space takes work and is difficult, but in the long run it is definitely worth it.
The greatest challenge is escaping the butterfly effect, which, as happened here, caused a small error to be amplified to the point of causing billions in losses.
Thank you for reading my article!
If you made it this far, it means I entertained you!
Do not be afraid to send me an email!
diego.luidiblu@gmail.com