C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ Math fabs()

C++ Math fabs()

它计算给定数字的绝对值。
假设数字为'x':
fabs(x) = |x|;

语法

float fabs(float x);
double fabs(double x);
int fabs(int x);  

参数

x : 要确定其绝对值的值。

返回值

它返回x的绝对值。

示例1

让我们看一下x值为正数时的简单示例。
#include <iostream>
#include<math.h>
using namespace std;
 int main()
{
float x=11.2;
 std::cout << "Value of x is :" <<x<< std::endl;
 cout<<"Absolute value of x is : "<<fabs(x);
 return 0;
}
输出:
Value of x is :11.2
Absolute value of x is : 11.2   
在此示例中,fabs()函数确定x = 11.2的绝对值。

示例2

让我们看一下该值时的简单示例x的值为负。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
   float x=-9.4;
   std::cout << "Value of x is :" <<x<< std::endl;
   cout<<"Absolute value of x is : "<<fabs(x);
    return 0;
}
输出:
Value of x is :-9.4
Absolute value of x is : 9.4
在此示例中,当x的值等于-9.4时,fabs()函数计算x的绝对值。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4