Since they are the implementation, they are free to make as many assumptions as they want. They know the format of the double and can play tricks with that instead.

Likely (as in almost not even a question), your double is the binary64 format. This means the sign has it's own bit, and an absolute value is merely clearing that bit. For example, as a specialization, a compiler implementer may do the following:

template <>
double abs<double>(const double x)
{
    // breaks strict aliasing, but compiler writer knows this behavior for the platform
    uint64_t i = reinterpret_cast<const std::uint64_t&>(x);
    i &= 0x7FFFFFFFFFFFFFFFULL; // clear sign bit

    return reinterpret_cast<const double&>(i);
}

This removes branching and may run faster.

Answer from GManNickG on Stack Overflow
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ c โ€บ numeric โ€บ math โ€บ abs.html
abs, labs, llabs, imaxabs - cppreference.com
for 32-bit 2's complement type int, INT_MIN is -2147483648, but the would-be result 2147483648 is greater than INT_MAX, which is 2147483647. ... #include <limits.h> #include <stdio.h> #include <stdlib.h> int main(void) { printf("abs(+3) = %d\n", abs(+3)); printf("abs(-3) = %d\n", abs(-3)); // printf("%+d\n", abs(INT_MIN)); // undefined behavior on 2's complement systems }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ c_function_abs.htm
C library - abs() function
The C stdlib library abs() function is used to returns the absolute value of the specified number, where absolute represents the positive number. This function only returns the positive integer.
Discussions

How to get absolute value from double - c-language - Stack Overflow
Especially since Java has classes and can overload methods, but C does not. ... abs is only implemented for integer in C. More on stackoverflow.com
๐ŸŒ stackoverflow.com
microcontroller - Implementing an absolute value function in C - Electrical Engineering Stack Exchange
The standard C library is providing the optimized solutions for many problems with considerations based on the architecture, compiler in use and others. The abs() function defined in stdlib.h is one of these, and it is used for your purpose exactly. More on electronics.stackexchange.com
๐ŸŒ electronics.stackexchange.com
July 2, 2019
Fast abs function
Why do you think std::abs is too slow? The difference between +x and -x is not a flip of a single bit. Lookup "2's complement" And for integers, there is no -0 More on reddit.com
๐ŸŒ r/Cplusplus
34
5
March 10, 2024
Simple C Question on abs() and fabs()
It is just truncating for you and returning an int. What behavior were you expecting? More on reddit.com
๐ŸŒ r/AskProgramming
8
5
October 21, 2020
๐ŸŒ
ABS-CBN
abs-cbn.com
Leading Entertainment and News Network | ABS-CBN
Explore ABS-CBN's official website for the latest news, entertainment and shows. Stay updated with the Philippines' top media and broadcasting network.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ ref_stdlib_abs.php
C stdlib abs() Function
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... The abs() function returns the absolute (positive) value of a number.
๐ŸŒ
Linux Man Pages
man7.org โ€บ linux โ€บ man-pages โ€บ man3 โ€บ abs.3.html
abs(3) - Linux manual page
These functions compute the absolute value of the argument j of the appropriate integer type for the function.
Find elsewhere
๐ŸŒ
Draft C++ Standard
eel.is โ€บ c++draft โ€บ c.math.abs
[c.math.abs]
๐Ÿ”—constexpr floating-point-type abs(floating-point-type x);
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ c# โ€บ math functions โ€บ .abs()
C# (C Sharp) | Math Functions | .Abs() | Codecademy
November 19, 2025 - The C# Math.Abs() method is a static method that returns the absolute value of a specified number.
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
The abs() Function | C For Dummies Blog
November 12, 2016 - The point is that you find yourself needing positive values from time to time and the abs() function guarantees that result. For example, you subtract variable b from a, which returns the difference. The result could be positive or negative.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ abs() in c++
abs() in C++ | abs() Function in C++ - Scaler Topics
October 10, 2025 - The abs() function in C++ returns the absolute value of an integer number. The absolute value of a negative number is multiplied by -1, but the absolute value of positive numbers and zero is that number itself.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-abs-in-cpp
What is abs() in C++?
The abs() function in C++ is used to get the absolute value of a number. The absolute value of a number is the distance of the number from 0. In other words, it is the non-negative value of the number.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ abs() function in c
abs() Function in C - Scaler Topics
March 21, 2024 - In the C programming language, the use of the function abs in C is to return the absolute value of an integer. By integer, it means that the abs() is an arithmetics function.
๐ŸŒ
GNU
gnu.org โ€บ s โ€บ libc โ€บ manual โ€บ html_node โ€บ Absolute-Value.html
Absolute Value (The GNU C Library)
Next: Normalization Functions, Up: Arithmetic Functions [Contents][Index] These functions are provided for obtaining the absolute value (or magnitude) of a number. The absolute value of a real number x is x if x is positive, โˆ’x if x is negative.
Top answer
1 of 3
11

The standard C library is providing the optimized solutions for many problems with considerations based on the architecture, compiler in use and others. The abs() function defined in stdlib.h is one of these, and it is used for your purpose exactly. To emphasize the point, here is ARM compiler result when using abs vs a version of a homebrew abs: https://arm.godbolt.org/z/aO7t1n

Paste:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    srand(111);
    int x = rand() - 200;
    printf("%d\n", abs(x));
}

results in

main:
        push    {r4, lr}
        mov     r0, #111
        bl      srand
        bl      rand
        sub     r1, r0, #200
        cmp     r1, #0
        rsblt   r1, r1, #0
        ldr     r0, .L4
        bl      printf
        mov     r0, #0
        pop     {r4, pc}
.L4:
        .word   .LC0
.LC0:
        .ascii  "%d\012\000"

And

#include <stdio.h>
#include <stdlib.h>

int my_abs(int x)
{
    return x < 0 ? -x : x;
}

int main(void)
{
    srand(111);
    int x = rand() - 200;
    printf("%d\n", my_abs(x));
}

results in

my_abs:
        cmp     r0, #0
        rsblt   r0, r0, #0
        bx      lr
main:
        push    {r4, lr}
        mov     r0, #111
        bl      srand
        bl      rand
        sub     r1, r0, #200
        cmp     r1, #0
        rsblt   r1, r1, #0
        ldr     r0, .L5
        bl      printf
        mov     r0, #0
        pop     {r4, pc}
.L5:
        .word   .LC0
.LC0:
        .ascii  "%d\012\000"

Notice that the main code is identical (only a label name is different) in both programs as my_abs got inlined, and its implementation is the same as the standard one.

2 of 3
3

The speed of a given solution will depend greatly on the architecture, but in C I would say

return (n > 0 ? n : -n);

and let the compiler figure out the best solution.

EDIT: @jonk points out correctly that this will fail for the most-negative possible value of n, assuming that two's-complement arithmetic is used.

Yes, my solution has a conditional branch, but yours has an arithmetic operator and two bitwise operators. Can your microcontroller shift 15 places in a single clock?

๐ŸŒ
Arduino
docs.arduino.cc โ€บ language-reference โ€บ en โ€บ functions โ€บ math โ€บ abs
abs() - Arduino Documentation
April 25, 2025 - Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure.
๐ŸŒ
Reddit
reddit.com โ€บ r/cplusplus โ€บ fast abs function
r/Cplusplus on Reddit: Fast abs function
March 10, 2024 -

So, I was thinking of making fast abs function, which would be necesary to improve performance, and I had an idea to do it something like this

int abs(int input){
    return input & -0;
}

Essentially, I am trying to make a simple function that removes the sign bit. The problem is, I heard that alot of compliers would ignore this because its a zero. How could I do it that compliers wouldnt ignore it, and it would work for the intended purpose?

Edit: Thanks for all the answers the issue has been resolved!

๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ c-runtime-library โ€บ reference โ€บ abs-labs-llabs-abs64
abs, labs, llabs, _abs64 | Microsoft Learn
The abs, labs, llabs, and _abs64 functions return the absolute value of the parameter n. There's no error return. Because C++ allows overloading, you can call overloads of abs that take and return long, long long, float, double, and long double ...