Home  >  Article  >  Backend Development  >  How to express absolute value in c++

How to express absolute value in c++

下次还敢
下次还敢Original
2024-05-01 16:15:211127browse

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).

How to express absolute value in c++

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn