abs function equivalent in c++

7 views (last 30 days)
Manu Chaudhary
Manu Chaudhary on 24 Sep 2022
Commented: Walter Roberson on 24 Sep 2022
Hi everyone, I am presently struggling with converting a matlab prototype code into a c++ equivalent. I am struggling with the abs function.
What should I do in C++/C to get the similar output?

Answers (1)

Walter Roberson
Walter Roberson on 24 Sep 2022
In C++ abs() is overloaded and for double precision will call fabs()
fabs() was not a formal part of C, but it was common for C vendors to provide fabs(). It was common for fabs to be implemented as a macro, but that led to arguments, especially as ieee 754 double precision defines an fabs hardware operation that does not require any branching.
Walter Roberson
Walter Roberson on 24 Sep 2022
https://en.cppreference.com/w/cpp/numeric/complex shows the complex class for C++ and shows using a suffix of i for imaginary part, and shows abs() as the function name

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!