Home > Backend Development > C++ > body text

How to express absolute value in c++

下次还敢
Release: 2024-05-01 16:15:21
Original
1070 people have browsed it

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>
Copy after login

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>
Copy after login

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!

Related labels:
c++
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!