Use fabs() (in math.h) to get absolute-value for double:
double d1 = fabs(-3.8951);
Answer from herohuyongtao on Stack OverflowCppreference
en.cppreference.com › w › cpp › numeric › math › fabs
std::abs(float), std::fabs, std::fabsf, std::fabsl - cppreference.com
The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their argument num of integer type, std::fabs(num) has the same effect as std::fabs(static_cast<double>(num)). Run this code · #include <cmath> #include <iostream> int main() { std::cout << "abs(+3.0) = " << std::abs(+3.0) << '\n' << "abs(-3.0) = " << std::abs(-3.0) << '\n'; // special values std::cout << "abs(-0.0) = " << std::abs(-0.0) << '\n' << "abs(-Inf) = " << std::abs(-INFINITY) << '\n' << "abs(-NaN) = " << std::abs(-NAN) << '\n'; } Possible output: abs(+3.0) = 3 abs(-3.0) = 3 abs(-0.0) = 0 abs(-Inf) = inf abs(-NaN) = nan ·
Videos
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 ... 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 ...
W3Schools
w3schools.com › cpp › ref_math_abs.asp
C++ Math abs() Function
One of the following: double abs(double number); float abs(float number); ❮ Math Functions · ★ +1 · Sign in to track progress · REMOVE ADS · PLUS · SPACES · GET CERTIFIED · FOR TEACHERS · BOOTCAMPS · CONTACT US ·
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).
Reddit
reddit.com › r/cpp_questions › why is abs() in cmath also getting rid of my decimals?
r/cpp_questions on Reddit: why is abs() in cmath also getting rid of my decimals?
October 26, 2022 -
Hey guys, I have a very simple code, which is the following:
#include <iostream>
#include <cmath>
int main()
{
double a = 2.1;
double b = abs(a);
std::cout << b;
}The output I get is b=2. Which doesn't really make sense to me. The output should just be 2.1, no?
Thanks in advance!
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › math_h › fabs.php
C Language: fabs function (Absolute Value of Floating-Point Number)
In the C Programming Language, the fabs function returns the absolute value of a floating-point number. The syntax for the fabs function in the C Language is: double fabs(double x); x · The value to convert to an absolute value. The fabs function returns the absolute value of a floating-point ...
Cplusplus
cplusplus.github.io › LWG › issue2294
Issue 2294: <cstdlib> should declare abs(double)
-?- To avoid ambiguities, C++ also adds the following overloads of abs() to <cstdlib>, with the semantics defined in <cmath>: float abs(float); double abs(double); long double abs(long double);
IBM
ibm.com › docs › en › zos › 2.4.0
abs(), absf(), absl() — Calculate integer absolute value
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
The abs() function is defined in the stdlib.h header file. It accepts integer values as input and returns a positive integer value. For floating-point values, use the fabs() function, defined in the math.h header file. It accepts double values as input and returns a positive double value.
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.
Programiz
programiz.com › cpp-programming › library-function › cmath › abs
C++ cmath abs() - C++ Standard Library
Note: The cmath abs() function is identical to the fabs() function. #include <iostream> #include <cmath> using namespace std; int main() { double num = -87.91, result;
GNU
gnu.org › software › libc › manual › html_node › Absolute-Value.html
Absolute Value (The GNU C Library)
Most computers use a two’s complement integer representation, in which the absolute value of INT_MIN (the smallest possible int) cannot be represented; thus, abs (INT_MIN) is not defined. Using uabs avoids this. llabs and imaxdiv are new to ISO C99. uabs, ulabs, ullabs and uimaxabs are new to ISO C2Y. See Integers for a description of the intmax_t type. Function: double fabs (double number) ¶ ·
Cprogramming
cboard.cprogramming.com › c-programming › 103197-absolute-value-double.html
absolute value of a double - C Board
You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much. Outside of your DOS world, your header file is meaningless. ... Indeed. You are right. Sometimes I am such an idiot. My best try so far, still slower than the standard implementation: ... double inline absolute(double number) { //printf("%f ", number); unsigned long long* x = (unsigned long long*)&number; *x &= 0x7fffffffffffffff; //printf("%f \n", number); return number; }
Cplusplus
cplusplus.com › reference › cmath › fabs
Cplusplus
Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double (defined for T being any integral type). x · Value whose absolute value is returned. The absolute value of x. Output: abs · Absolute value (function) labs ·
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 -2147483648 labs(LONG_MIN) returns -2147483648 llabs(LLONG_MIN) returns -9223372036854775808 _abs64(_I64_MIN) returns 0x8000000000000000 · Data conversion Math and floating-point support _cabs fabs, fabsf, fabsl imaxabs
Naukri
naukri.com › code360 › library › abs-in-cpp
abs() in C++ - Naukri Code 360
Almost there... just a few more seconds
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!
Top answer 1 of 5
50
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
2 of 5
7
I think it's important to understand that c++'s biggest advantage is that it's a nearly perfect translation to machine code. The compilers are so good that trying these tricks often makes it slower. Also, 0 is (in 4 bit terms) 0000, while attempting to flip the sign bit is 1000, or -8. Sebastian Lague has a cool video about this.