📜  C++ cstdlib abs()(1)

📅  最后修改于: 2023-12-03 14:59:44.770000             🧑  作者: Mango

C++ cstdlib abs()

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.

Syntax
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);
Parameters

The abs() function takes one parameter which can be of the following types:

  • int
  • long int
  • long long int
  • float
  • double
  • long double
Return Value

The return value of the abs() function is the absolute value of the input parameter.

Example
#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.

Conclusion

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.