Home > Article > Backend Development > How to express absolute value in c++
The absolute value in C is expressed as the abs function, which accepts one parameter and returns the same type of absolute value result. Syntax: #include
; double abs(double x); float abs(float x); long double abs(long double x).
Representation of absolute value in C
In C, the absolute value can be expressed by abs
function to represent. This function receives a parameter representing the expression or variable whose absolute value is to be calculated, and returns an absolute value result of the same type.
Syntax:
<code class="cpp">#include <cmath> double abs(double x); float abs(float x); long double abs(long double x);</code>
where x
is the expression or variable whose absolute value is to be calculated.
Example:
<code class="cpp">#include <iostream> #include <cmath> using namespace std; int main() { double x = -5.0; cout << "绝对值:" << abs(x) << endl; // 输出:5 float y = -1.234f; cout << "绝对值:" << abs(y) << endl; // 输出:1.234 long double z = -1234567890123.4567890123456789L; cout << "绝对值:" << abs(z) << endl; // 输出:1234567890123.4567890123456789 }</code>
The above is the detailed content of How to express absolute value in c++. For more information, please follow other related articles on the PHP Chinese website!