日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ 多种取整函数的使用和区别: ceil() floor() round() trunc() rint() nearbyint()

發(fā)布時間:2024/3/13 c/c++ 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ 多种取整函数的使用和区别: ceil() floor() round() trunc() rint() nearbyint() 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

取整函數(shù):

ceil()?????右向取整:數(shù)軸上右邊最靠近的整數(shù),向大的方向取值;ceil “天花板”
floor()???左向取整:數(shù)軸上左邊最靠近的整數(shù),向小的方向取值;floor “地板”
round() 接近取整:不管正負(fù),即四舍五入后留下整數(shù)部分??round(x) = floor(x+0.5)
trunc()??零向取整:不管正負(fù),直接舍掉小數(shù)部分只留下整數(shù)部分,向原點(diǎn)0的方向取值
rint() nearbyint():四種方法任選其一,由?fesetround()?配合設(shè)置,見例程二說明

例程一:ceil() floor() round() trunc()用法

#include <iostream> #include <cmath>using namespace std;int main() {double num[4] = {13.6, 3.23, -13.6, -3.23};for (auto n:num){cout<< " ceil(x) floor(x) round(x) trunc(x):x = " << n << endl;cout<< "\t" << ceil(n) << "\t" << floor(n) << "\t"<< round(n) << "\t" << trunc(n) << endl << endl;}return 0; }/*ceil(x) floor(x) round(x) trunc(x):x = 13.614 13 14 13ceil(x) floor(x) round(x) trunc(x):x = 3.234 3 3 3ceil(x) floor(x) round(x) trunc(x):x = -13.6-13 -14 -14 -13ceil(x) floor(x) round(x) trunc(x):x = -3.23-3 -4 -3 -3-------------------------------- Process exited after 0.6186 seconds with return value 0 請按任意鍵繼續(xù). . . */

例程二:fegetround() fesetround()用法

函數(shù)原型:
int fesetround(int mode); 設(shè)置取整模式
int fegetround(void); 獲取當(dāng)前取整模式

mode 值:
Round to nearest value ? FE_TONEAREST? ? ?== 0
Round downward? ? ? ? ? ? FE_DOWNWARD? ? ?== 1024
Round upward? ? ? ? ? ? ? ? ?FE_UPWARD? ? ? ? ? ?== 2048
Round towards zero? ? ? ? FE_TOWARDZERO ?== 3072
default value:? ? ? ? ? ? ?? ?FE_TONEAREST? ? ? == 0

#include <iostream> #include <cmath> #include <cfenv> using namespace std;int getround(void) {switch (fegetround()) { case FE_TONEAREST:cout << "\nRound to nearest value \t FE_TONEAREST\t== "; break; //round()case FE_DOWNWARD:cout << "\nRound downward \t\t FE_DOWNWARD\t== "; break; //ceil()case FE_UPWARD:cout << "\nRound upward \t\t FE_UPWARD\t== "; break; //floor()case FE_TOWARDZERO:cout << "\nRound towards zero \t FE_TOWARDZERO\t== "; break; //trunc()default: cout << "unknown";}return fegetround(); }int main(void) {for(int i=0;i<4;i++){fesetround(i*1024);cout << getround();}cout << "\n\nDaufult: TONEAREST == 0" << endl; return 0; }/*Round to nearest value FE_TONEAREST == 0 Round downward FE_DOWNWARD == 1024 Round upward FE_UPWARD == 2048 Round towards zero FE_TOWARDZERO == 3072Daufult: TONEAREST == 0-------------------------------- Process exited after 0.8902 seconds with return value 0 請按任意鍵繼續(xù). . . */

例程三:nearbyint() rint()用法

#include <iostream> #include <array> #include <cmath> #include <cfenv> using namespace std;int main(void) {array<double,4> num={13.6, 3.23, -13.6, -3.23};array<string,6> str={"nearbyint(x), x = \n", "\nmode = ","FE_TONEAREST(default) = ","FE_DOWNWARD = ","FE_UPWARD = ","FE_TOWARDZERO = " };cout<<str.at(0);for (auto n:num) cout<<"\t"<<n; cout<<endl;for (int i=0;i<4;i++){fesetround(i*1024);cout<<str.at(1)<<str.at(i+2)<<fegetround()<<endl;for (auto n:num) cout<<"\t"<<nearbyint(n); cout<<endl;}return 0; }/* nearbyint(x), x =13.6 3.23 -13.6 -3.23mode = FE_TONEAREST(default) = 014 3 -14 -3mode = FE_DOWNWARD = 102413 3 -14 -4mode = FE_UPWARD = 204814 4 -13 -3mode = FE_TOWARDZERO = 307213 3 -13 -3-------------------------------- Process exited after 0.6 seconds with return value 0 請按任意鍵繼續(xù). . . */

?

總結(jié)

以上是生活随笔為你收集整理的C++ 多种取整函数的使用和区别: ceil() floor() round() trunc() rint() nearbyint()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。