🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_abs.htm
C library - abs() function
Create another c program to get the absolute value of specified integer. Using the abs() function. #include<stdio.h> #include<stdlib.h> int main(){ int x; //absolute of negative number x = abs(-10*100); printf("Absolute of -10*100: %d\n", x); }
🌐
Scaler
scaler.com › home › topics › abs() function in c
abs() Function in C - Scaler Topics
March 21, 2024 - This article covers what abs in C is, along with its syntax, parameter, return value & example. You will also learn the required header, along with some more examples.
Discussions

microcontroller - Implementing an absolute value function in C - Electrical Engineering Stack Exchange
\$\begingroup\$ Your final comment is perfect. Since the result is know at compile time, the codes do not actually calculate the abs. \$\endgroup\$ ... \$\begingroup\$ @vangelo Are you saying I have cheated? :) Well, let me get another example \$\endgroup\$ 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
Write a function that computes the absolute value of an integer.
Although your problem statement says "the standard library provides a similar function", I am sure your task here is not to simply call that standard library function. It's asking you to write your own function, with its own independent implementation, that has the same overall documented behaviour. How could you implement _abs in a way that it doesn't call abs? More on reddit.com
🌐 r/C_Programming
11
0
July 6, 2022
Math.isclose: what exaclty does 'abs_tol' do?
Definition in documentation is abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) More on reddit.com
🌐 r/learnpython
2
1
May 4, 2016
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › abs.html
abs, labs, llabs, imaxabs - cppreference.com
for 32-bit 2's complement type ... #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 }...
🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c – arithmetic functions › c – abs() function
C abs() function | C Arithmetic functions | Fresh2Refresh
September 23, 2020 - C abs() function:abs( ) function in C returns the absolute value of an integer. The absolute value of a number is always positive.
🌐
W3Schools
w3schools.com › c › ref_stdlib_abs.php
C stdlib abs() Function
C Reference C Keywords C <stdio.h> ... · ❮ C stdlib Library · Display the absolute value of an integer: int value = abs(-5); printf("%d", value); Try it Yourself » ·...
🌐
Tutorial Gateway
tutorialgateway.org › c-abs-function
C abs function
March 25, 2025 - #include <stdio.h> #include <math.h> int main() { printf("\n The Absolute Positive Value of 75 = %d ", abs(75)); printf("\n The Absolute Positive Value of -15 = %d ", abs(15)); printf("\n The Absolute Positive Value of -152 = %d ", abs(152)); ...
🌐
Cplusplus
cplusplus.com › reference › cmath › abs
std::abs
double abs (double x); float abs (float x);long double abs (long double x); double abs (T x); // additional overloads for integral types
🌐
Programming Simplified
programmingsimplified.com › c › math.h › abs
abs in C | Programming Simplified
#include <stdio.h> #include <math.h> int main() { int n, result; printf("Enter an integer to calculate its absolute value\n"); scanf("%d", &n); result = abs(n); printf("Absolute value of %d = %d\n", n, result); return 0; } Output of program: You can implement you own function as follows: long ...
Find elsewhere
🌐
Cplusplus
cplusplus.com › reference › cstdlib › abs
abs
Returns the absolute value of parameter n ( /n/ ). In C++, this function is also overloaded in header <cmath> for floating-point types (see cmath abs), in header <complex> for complex numbers (see complex abs), and in header <valarray> for valarrays (see valarray abs).
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › abs-labs-llabs-abs64
abs, labs, llabs, _abs64 | Microsoft Learn
The absolute value of -4 is 4 The absolute value of -41567 is 41567 The absolute value of -9876543210 is 9876543210 The absolute value of 0xffffffffffffffff is 0x0000000000000001 Microsoft implementation-specific results: abs(INT_MIN) returns ...
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › stdlib_h › abs.php
C Language: abs function (Absolute Value of Integer)
The abs function returns the absolute value of an integer represented by x. In the C Language, the required header for the abs function is:
🌐
Draft C++ Standard
eel.is › c++draft › c.math.abs
[c.math.abs]
🔗constexpr floating-point-type abs(floating-point-type x);
🌐
CodeToFun
codetofun.com › c › math-abs
C abs() Function | CodeToFun
November 27, 2024 - Let's dive into another example to illustrate how the abs() method works on integer. math-abs.c · Copied · #include <stdio.h> #include <math.h> int main() { float num = -3.14; float result = fabs(num); printf("Absolute value of %.2f is %.2f\n", num, result); return 0; } Output ·
🌐
University of Auckland
cs.auckland.ac.nz › ~mjd › prog_contest › www.cppreference.com › c › math › abs
c:math:abs
The abs() function returns the absolute value of num. For example: int magic_number = 10; cout << "Enter a guess: "; cin >> x; cout << "Your guess was " << abs( magic_number - x ) << " away from the magic number." << endl;
🌐
Programiz
programiz.com › cpp-programming › library-function › cmath › abs
C++ cmath abs() - C++ Standard Library
#include <iostream> #include <cmath> using namespace std; int main() { double num = -87.91, result; result = abs(num); cout << "abs(" << num << ") = |" << num << "| = " << result; return 0; }
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › stdlib.h › abs
C Programming/stdlib.h/abs - Wikibooks, open books for an open world
A very possible implementation ... be: function abs (number n) { if n >= 0 return n; else return -n; } [edit | edit source] These functions are defined in the standard header math.h for C and cmath for C++: int abs (int i); long labs (long i); double fabs (double i); float fabsf (float i); long double fabsl (long double i); Retrieved ...
🌐
IBM
ibm.com › docs › en › rdfi › 9.6.0
ILE C/C++ Runtime Library Functions
We cannot provide a description for this page right now
🌐
C For Dummies
c-for-dummies.com › blog
The abs() Function | C For Dummies Blog
November 12, 2016 - 0 - -10 = 10 abs( 10) 0 - -9 = 9 abs( 9) 0 - -8 = 8 abs( 8) 0 - -7 = 7 abs( 7) 0 - -6 = 6 abs( 6) 0 - -5 = 5 abs( 5) 0 - -4 = 4 abs( 4) 0 - -3 = 3 abs( 3) 0 - -2 = 2 abs( 2) 0 - -1 = 1 abs( 1) 0 - 0 = 0 abs( 0) 0 - 1 = -1 abs( 1) 0 - 2 = -2 abs( 2) 0 - 3 = -3 abs( 3) 0 - 4 = -4 abs( 4) 0 - ...
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?

🌐
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!