abs function equivalent in c++

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?

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 9 月 24 日

0 投票

https://cplusplus.com/reference/cstdlib/abs/
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.

3 件のコメント

Manu Chaudhary
Manu Chaudhary 2022 年 9 月 24 日
Thank for the response.
Your link provide a abs method for c++.
But if suppose, the problem is
abs(0.234 +j0.43)^2 + abs(0.24 +j0.643)^2
Then what should I do in C++?
Do the term like below make sense or do it require some change.
n=pow(abs(0.234),2) + pow(abs(0.43),2) + pow(abs(0.24),2) + pow(abs(0.643),2);
Thank you in advance.
Manu Chaudhary
Manu Chaudhary 2022 年 9 月 24 日
編集済み: Manu Chaudhary 2022 年 9 月 24 日
Thank you for great help. Your solution worked.
Walter Roberson
Walter Roberson 2022 年 9 月 24 日
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

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2022 年 9 月 24 日

コメント済み:

2022 年 9 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by