C++ 取正

来源:百度知道 编辑:UC知道 时间:2024/06/16 01:26:21
急需

#include<cmath>
using namespace std;
int a=-3;
int b;
b=ads(a);

abs函数 在 <stdlib.h> 或者 <math.h> 中

函数举例:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void main( void )
{
int ix = -4, iy;
long lx = -41567L, ly;
double dx = -3.141593, dy;

iy = abs( ix );
printf( "The absolute value of %d is %d\n", ix, iy);

ly = labs( lx );
printf( "The absolute value of %ld is %ld\n", lx, ly);

dy = fabs( dx );
printf( "The absolute value of %f is %f\n", dx, dy );
}

请问是"取正"还是"取整"
取正就用y=abs(x),y=labs(x),y=fabs(x);
取整用强制类型转换y=(int)x;
如果想四舍五入取整就用y=(int)(x+0.5);

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a=-3;
a=abs(a);//如果a是double