Something you should know about the abs function

It is a rule in math that the absolute value of a number must be no less than zero. Is it true in computer languages? Well, most of the case except one. Let me take C language for example.

For a 32-bits C interger, its value ranges from [-2147483648, 2147483647] .So for x = -2147483648, what is the result of abs(x)? 2147483648, -2147483648, 2147483647, 0 or something else?

The result depends on your compiler! Please be careful to handle this undefined result in your coding.

Let’s look into the implementation of the abs function in C:

So a potential bug may skip into your code like this situration:

But if you really test the above code in your computer, probobly you will find that the computer gives you the expected output. Here is a Demo on; You are suggested trying on your machine!

What happens to the correct output occassion is that the compiler actually return a longer integer type like long long which is 8 bytes usually. For long long y = abs(INT_MIN);, y is equal to INT_MAX + 1 for sure.

This is as much as the modern compiler can help you reduce your bugs. If you code in this way:

This time bugs are more likely to happen on you. Nigel Jones gives some good solutions to the problem in his article: The absolute truth about abs().

In summary, there are several ways to avoid the abs bug: 1. Always use a longer type to store your abs result, like long long for int. But what type for long long? Therefore the better one is double, in the cost of casting time and more CPU cycles. 2. Write your own abs function like this one: safe abs function; 3. Carry out the comparision in the negative space because negetive space is larger than the postive space.

Both solution 2 & 3 require you to use your own abs function.

That’s my report on delving the absolute function in C language. Just as a saying goes: Think twice before you code 🙂