One of the first examples for loops in probably every programming language course is taking a list of numbers and calculating the sum of them. And in

How to Correctly Sum Up Numbers

submited by
Style Pass
2024-10-31 09:30:21

One of the first examples for loops in probably every programming language course is taking a list of numbers and calculating the sum of them. And in every programming language, it’s trivial to write up a solution:

If you compile this with a modern optimizing compiler, you will even get very efficient vectorized code, that sums millions of integers per second. Nice!

Unfortunately, as you might have guessed, this is not the end of the story. There are several issues with this solution if used in any serious application where you want correct results, e.g., a database system:

In this post, we are exploring the world of handling exceptional cases in numerical computations. We will focus only on the initial example of adding up a list of numbers as even this simple example comes with more than enough edge cases.

Obviously, when we talk about edge cases in arithmetic on a computer, “numerical overflows” are the first thing most people will think of. Since most hardware only supports fixed-size values for the arithmetic operation, there is always a limit to the numbers the hardware can represent. Most CPU architectures support only up to 64-bit integers in their registers, for example.

Leave a Comment