📅  最后修改于: 2023-12-03 14:59:44.770000             🧑  作者: Mango
The abs()
function belongs to the cstdlib
library in C++. It is used to return the absolute value of an integer, long integer, or a floating-point number.
int abs(int x);
long int abs(long int x);
long long int abs(long long int x);
float abs(float x);
double abs(double x);
long double abs(long double x);
The abs()
function takes one parameter which can be of the following types:
int
long int
long long int
float
double
long double
The return value of the abs()
function is the absolute value of the input parameter.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
cout << abs(-5) << endl; // Output: 5
cout << abs(5) << endl; // Output: 5
cout << abs(-10.5) << endl; // Output: 10.5
cout << abs(10.5) << endl; // Output: 10.5
return 0;
}
In the above example, we have used the abs()
function to find the absolute values of integers and floating-point numbers.
The abs()
function is a useful function in C++ that can be used to find the absolute value of an integer, long integer, or a floating-point number. It can be used to perform mathematical calculations and is often used in scientific computing.