Use fabs() (in math.h) to get absolute-value for double:

double d1 = fabs(-3.8951);
Answer from herohuyongtao on Stack Overflow
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re57.html
fabs - C in a Nutshell [Book]
December 16, 2005 - Content preview from C in a Nutshell ... fabsf( float x ); long double fabsl( long double x ); The fabs() function returns the absolute value of its floating-point argument x; if x is greater than or equal to 0, the return value is ...
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
GeeksforGeeks
geeksforgeeks.org › c language › fabs-function-in-c
fabs() Function in C - GeeksforGeeks
July 23, 2025 - fabs() function of math.h header file in C programming is used to get the absolute value of a floating point number.
🌐
Programiz
programiz.com › c-programming › library-function › math.h › fabs
C fabs() - C Standard Library
Become a certified C programmer. Try Programiz PRO! ... The fabs() function takes a single argument (in double) and returns the absolute value of that number (also in double). ... To find absolute value of an integer or a float, you can explicitly convert the number to double.
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_fabs.htm
C library - fabs() function
The C math library fabs() function accepts the parameter(x) of type double that returns the absolute value of x. This function is defined under the header that calculates the absolute value of a float point number.
Find elsewhere
🌐
Cplusplus
cplusplus.com › reference › cmath › abs
Cplusplus
double abs (double x); float abs (float x);long double abs (long double x); double abs (T x); // additional overloads for integral types ... Returns the absolute value of x: |x|. These convenience abs overloads are exclusive of C++. In C, abs is only declared in <stdlib.h> (and operates on ...
🌐
GNU
gnu.org › s › libc › manual › html_node › Absolute-Value.html
Absolute Value (The GNU C Library)
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts. This function returns the absolute value of the floating-point number number.
🌐
LabEx
labex.io › tutorials › c-evaluate-absolute-value-expressions-in-c-435174
How to Evaluate Absolute Value Expressions in C | LabEx
Enter an integer: -42 Enter a float number: -3.14 Original Integer: -42 Absolute Integer: 42 Original Float: -3.14 Absolute Float: 3.14 Absolute Float (Scientific): 3.140000e+00 ...
🌐
Vultr
docs.vultr.com › clang › standard-library › math-h › fabs
C math.h fabs() - Get Absolute Value | Vultr Docs
December 12, 2024 - Define a floating-point variable with a negative value. Use fabs() to compute the absolute value.
🌐
Linux Hint
linuxhint.com › fabs-function-in-c-language-absolute-value
Fabs() Function in C Language (Absolute Value) – Linux Hint
Then, we create our main function which is returned empty and in which we declare our variables. To get the absolute value of “x”, we first create a variable of float type and assign it the value -3.1416.
🌐
Texas Instruments E2E
e2e.ti.com › support › microcontrollers › c2000-microcontrollers-group › c2000 › f › c2000-microcontrollers-forum › 243346 › calculating-absolute-value-of-floating-point-variable-on-cla-c
Calculating absolute value of floating point variable on CLA-C
I need to calculate the absolute value of a variable on the CLA under C code. Using assembly is as simple as using MABSF32, but with CLA-C I cannot write that. I'm using a 28035. ... The intrinsic 'fabs()' is supported by the CLA-C compiler and will give you the floating point absolute.
🌐
Reddit
reddit.com › r/rust › how to efficiently compute absolute value of float in #![no_std]?
r/rust on Reddit: How to efficiently compute absolute value of float in #![no_std]?
August 19, 2023 -

I am writing a #![no_std] library that at one point needs to get the absolute value of a floating point number. This seems to need the standard library though. Currently I use #[cfg(feature = ...)] conditionals to enable a custom implementation of abs when the std feature is disabled, but this compiles to longer assembly compared to the std abs: https://godbolt.org/z/jY115ebTv. Is there a way to get this code quality in #![no_std]?

EDIT: I have compiled all answers in the thread in this godbolt link: https://godbolt.org/z/9nEEMofEG.

EDIT: Same code on the rust playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=02e74329c1fcc999e55f850c0544528b.

FINAL EDIT: For those who do not wish to read the whole thread, the code I ended up using is a combination of the comment by u/boomshroom and notes by u/matthieum:

fn abs(x: f64) -> f64 {
    f64::from_bits(x.to_bits() & !(1 << 63))
}

This compiles to the same single assembly instruction (andps) as the std abs on x86_64, which is all that's relevant for me. I have not tested other targets.

Top answer
1 of 9
112
Don't use the unsafe suggestions. It's completely unnecessary. Pointer casting or unions are needed in C, but Rust has these 2 little functions called {f32,f64}::to_bits() and {f32,f64}::from_bits() that tuck away all the ugliness so you can just focus on the safe no-op conversion. pub fn abs64(x: f64) -> f64 { f64::from_bits(x.to_bits() & (i64::MAX as u64)) } pub fn abs32(x: f32) -> f32 { f32::from_bits(x.to_bits() & (i32::MAX as u32)) } There's also the fact that this entire thread only exists because of this 4 year old issue . As shown, the implementation is trivial, but LLVM can lower it to a libm call ( but in practice, never will, not even with soft floats ), so it was decided to go in std instead of core.
2 of 9
7
I remember reading a paper many years ago about optimizing a library for FORTRAN. They were trying to get the fastest implementations of abs(), sign(), other little stuff like that. So they tried every possible combination of opcodes, from shortest to longest, up to the cycle count of the naive implementation, and tested each function against every possible input. There was a bunch of stuff in the paper about not having to test every case (e.g., start testing 0, 1, 2, -1, -2, 256, -255, etc etc before running all 4 billion bit patterns through it, making sure there's no branches that leave the range of the function, etc). It took like three weeks to figure out each function, came up with some really clever stuff (like the bit-bash patterns for counting one bits or using unary and binary math in the same expression to accomplish stuff), but they figured over the life of the compiled code they got back the time spend many many times over.
🌐
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.