例如:
1.向下取整⌊ ⌋
⌊100/150⌋=0,⌊-100/150⌋=-1
(100/150=2/3,并且0<2/3<1,所以向下取整后为0。-100/150=-2/3,并且-1<-2/3<0,所以向下取整之后为-1)
2.向上取整⌈ ⌉
⌈100/150⌉=1,⌈-100/150⌉=0
(100/150=2/3,并且0<2/3<1,所以向上取整后为1。-100/150=-2/3,并且-1<-2/3<0,所以向上取整之后为0)
上C++代码!!!
注意头文件
#include<cmath>
或者直接用万能头
#include<bits/stdc++.h>
(手动狗头)
//向下取整函数floor( )
cout<<floor(1.1)<<" ";
cout<<floor(1.9)<<" ";
cout<<floor(-1.1)<<" ";
cout<<floor(-1.9)<<" ";
cout<<floor(0.0)<<" ";
输出1 1 -2 -2 0
//向上取整函数ceil( )
cout<<ceil(1.1)<<" ";
cout<<ceil(1.9)<<" ";
cout<<ceil(-1.1)<<" ";
cout<<ceil(-1.9)<<" ";
cout<<ceil(0.0)<<" ";
输出2 2 -1 -1 0
当然,如果不愿意用这两个函数或者不愿意记/闲的,也可以自己写:
double floor(double x){
int y=(int)x;
if(y>x) return y-1;
else return y;
}
double ceil(double x){
int y=(int)x;
if(y<x) return y+1;
else return y;
}
不喜勿喷。
标签:150,cout,int,double,C++,取整,向下,100 From: https://blog.csdn.net/smx100509/article/details/137438888