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

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

取整函数:


ceil()     右向取整:数轴上右边最靠近的整数,向大的方向取值;ceil “天花板”


floor()   左向取整:数轴上左边最靠近的整数,向小的方向取值;floor “地板”


round() 接近取整:不管正负,即四舍五入后留下整数部分  round(x) = floor(x+0.5)


trunc()  零向取整:不管正负,直接舍掉小数部分只留下整数部分,向原点0的方向取值


rint() nearbyint():四种方法任选其一,由 fesetround() 配合设置,见例程二说明



例程一: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.6
        14      13      14      13
    ceil(x) floor(x) round(x) trunc(x):x = 3.23
        4       3       3       3
    ceil(x) floor(x) round(x) trunc(x):x = -13.6
        -13     -14     -14     -13
    ceil(x) floor(x) round(x) trunc(x):x = -3.23
        -3      -4      -3      -3
--------------------------------
Process exited after 0.6186 seconds with return value 0
请按任意键继续. . .
*/


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


   函数原型:


   int fesetround(int mode); 设置取整模式

   int fegetround(void); 获取当前取整模式


   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  == 3072
Daufult: TONEAREST == 0
--------------------------------
Process exited after 0.8902 seconds with return value 0
请按任意键继续. . .
*/



例程三: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.23
mode = FE_TONEAREST(default) = 0
        14      3       -14     -3
mode = FE_DOWNWARD = 1024
        13      3       -14     -4
mode = FE_UPWARD = 2048
        14      4       -13     -3
mode = FE_TOWARDZERO = 3072
        13      3       -13     -3
--------------------------------
Process exited after 0.6 seconds with return value 0
请按任意键继续. . .
*/


目录
相关文章
|
算法 Java 数据库连接
Java 与 C++ 区别深入剖析及应用实例详解
本文深入剖析了Java和C++两种编程语言的区别,从编译与执行机制、面向对象特性、数据类型与变量、内存管理、异常处理等方面进行对比,并结合游戏开发、企业级应用开发、操作系统与嵌入式开发等实际场景分析其特点。Java以跨平台性强、自动内存管理著称,适合企业级应用;C++则因高性能和对硬件的直接访问能力,在游戏引擎和嵌入式系统中占据优势。开发者可根据项目需求选择合适语言,提升开发效率与软件质量。附面试资料链接:[点此获取](https://pan.quark.cn/s/4459235fee85)。
1103 0
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
373 0
|
安全 C++
【c++】继承(继承的定义格式、赋值兼容转换、多继承、派生类默认成员函数规则、继承与友元、继承与静态成员)
本文深入探讨了C++中的继承机制,作为面向对象编程(OOP)的核心特性之一。继承通过允许派生类扩展基类的属性和方法,极大促进了代码复用,增强了代码的可维护性和可扩展性。文章详细介绍了继承的基本概念、定义格式、继承方式(public、protected、private)、赋值兼容转换、作用域问题、默认成员函数规则、继承与友元、静态成员、多继承及菱形继承问题,并对比了继承与组合的优缺点。最后总结指出,虽然继承提高了代码灵活性和复用率,但也带来了耦合度高的问题,建议在“has-a”和“is-a”关系同时存在时优先使用组合。
1089 6
|
存储 安全 编译器
在 C++中,引用和指针的区别
在C++中,引用和指针都是用于间接访问对象的工具,但它们有显著区别。引用是对象的别名,必须在定义时初始化且不可重新绑定;指针是一个变量,可以指向不同对象,也可为空。引用更安全,指针更灵活。
|
程序员 C++ 容器
在 C++中,realloc 函数返回 NULL 时,需要手动释放原来的内存吗?
在 C++ 中,当 realloc 函数返回 NULL 时,表示内存重新分配失败,但原内存块仍然有效,因此需要手动释放原来的内存,以避免内存泄漏。
|
存储 前端开发 C++
C++ 多线程之带返回值的线程处理函数
这篇文章介绍了在C++中使用`async`函数、`packaged_task`和`promise`三种方法来创建带返回值的线程处理函数。
988 6
|
编译器 C语言 C++
C++入门3——类与对象2-2(类的6个默认成员函数)
C++入门3——类与对象2-2(类的6个默认成员函数)
337 3
|
存储 编译器 C++
C++入门3——类与对象2-1(类的6个默认成员函数)
C++入门3——类与对象2-1(类的6个默认成员函数)
350 2
|
C++
C++ 多线程之线程管理函数
这篇文章介绍了C++中多线程编程的几个关键函数,包括获取线程ID的`get_id()`,延时函数`sleep_for()`,线程让步函数`yield()`,以及阻塞线程直到指定时间的`sleep_until()`。
501 0
C++ 多线程之线程管理函数
|
编译器 C语言 C++
详解C/C++动态内存函数(malloc、free、calloc、realloc)
详解C/C++动态内存函数(malloc、free、calloc、realloc)
3285 1