MATLAB、C++、OpenCV、Python、Python-Numpy近似值(取整)求取函数总结【round()、ceil()、floor()、fix()】

44 篇文章 13 订阅
31 篇文章 31 订阅

一、MATLAB中的近似值求取函数

MATLAB中有四个函数用于近似值求取,分别为round()、ceil()、floor() 、fix()。

声明:博主目前使用的MATLAB为R2019a版,所以下面提供的帮助文档链接为R2019a的帮助文档链接。

函数round()用于四舍五入为最近的小数或整数,可以控制小数部分或整数部分的位数,
详情参见 https://ww2.mathworks.cn/help/releases/R2019a/matlab/ref/round.html

函数ceil()用于朝正无穷大方向取整,既然是取整,那就不可以控制小数部分或整数部分的位数,详情见 https://ww2.mathworks.cn/help/releases/R2019a/matlab/ref/ceil.html

函数floor()用于朝负无穷方向取整,既然是取整,那就不可以控制小数部分或整数部分的位数,详情见 https://ww2.mathworks.cn/help/releases/R2019a/matlab/ref/floor.html

函数fix()用于朝坐标轴的0点方向取整,既然是取整,那就不可以控制小数部分或整数部分的位数,详情见 https://ww2.mathworks.cn/help/releases/R2019a/matlab/ref/fix.html

二、C++中的近似值求取函数

C++的标准数学库中有三个函数用于近似值求取,分别为floor函数、ceil函数和round函数 。
它们的作用与MATLAB中的三个函数基本相同,只是C++的标准库的round函数只能用来取整,不能控制小数位数。
示例代码如下:

#include <iostream>
#include <math.h>

int main() 
{
	double y1, y2, y3, y4, y5, y6, y7;
	y1 = round(0.1);
	y2 = round(0.5);
	y3 = round(0.7);

	y4 = ceil(1.1);
	y5 = ceil(-2.8);

	y6 = floor(1.8);
	y7 = floor(-2.1);

	std::cout << "y1的值为:" << y1 << std::endl;
	std::cout << "y2的值为:" << y2 << std::endl;
	std::cout << "y3的值为:" << y3 << std::endl;
	std::cout << "y4的值为:" << y4 << std::endl;
	std::cout << "y5的值为:" << y5 << std::endl;
	std::cout << "y6的值为:" << y6 << std::endl;
	std::cout << "y7的值为:" << y7 << std::endl;


	return 0;
}

运行结果如下:
在这里插入图片描述
另外C++的标准库math中,还有很多近似值(取整)函数,去往博文 https://blog.csdn.net/wenhao_ir/article/details/125639428搜索“舍入”二字即可找到这些函数的大部分。

三、OpenCV中近似值求取函数

OpenCV中提供了函数cvRound、cvCeil、cvFloor来进行近似值求取,作用与标准数学库中的那几个函数一样。但要注意,函数cvRound是五舍六入,而不是四舍五入。

博主没有发现OpenCV中直接到Mat对象的每个元素进行近似值求取的函数。

示例代码如下:

#include <iostream>
#include <opencv2/opencv.hpp>

int main() 
{
	double y1, y2, y3, y4, y5, y6, y7;
	y1 = cvRound(0.1);
	y2 = cvRound(0.5);
	y3 = cvRound(0.7);

	y4 = cvCeil(1.1);
	y5 = cvCeil(-2.8);

	y6 = cvFloor(1.8);
	y7 = cvFloor(-2.1);

	std::cout << "y1的值为:" << y1 << std::endl;
	std::cout << "y2的值为:" << y2 << std::endl;
	std::cout << "y3的值为:" << y3 << std::endl;
	std::cout << "y4的值为:" << y4 << std::endl;
	std::cout << "y5的值为:" << y5 << std::endl;
	std::cout << "y6的值为:" << y6 << std::endl;
	std::cout << "y7的值为:" << y7 << std::endl;


	return 0;
}

运行结果如下:
在这里插入图片描述

四、Python中的近似值求取函数

Python的内建函数中只有函数round(),它做的五舍六入近似,函数round()的详细介绍见博文 https://blog.csdn.net/wenhao_ir/article/details/125436091的第54点。
在math库中有floor()函数和ceil()函数,作用与上面C++和MATLAB中的相关函数一样。
示例代码如下:

import math

y1 = round(0.1)
y2 = round(0.5)
y3 = round(0.7)

y4 = math.ceil(1.1)
y5 = math.ceil(-2.8)

y6 = math.floor(1.8)
y7 = math.floor(-2.1)

运行结果如下:
在这里插入图片描述
在Python的math库中还有函数modf():用于返回浮点数的整数部分和小数部分。
示例代码如下:

import math

x1 = math.modf(2.3)
x2 = math.modf(2.4)
x3 = math.modf(2.5)
x4 = math.modf(2.6)

运行结果如下:
在这里插入图片描述
在Python的math库中还有函数trunc():用于返回浮点数的整数部分。
示例代码如下:

import math

x1 = math.trunc(2.3)
x2 = math.trunc(2.4)
x3 = math.trunc(2.5)
x4 = math.trunc(2.6)

运行结果如下:
在这里插入图片描述

五、Python-Numpy中的近似值求取函数

Python-Numpy中也提供了相关函数,比如numpy.around()、numpy.floor()、numpy.ceil(),具体的介绍可参考 https://www.runoob.com/numpy/numpy-mathematical-functions.html
这里就不多做介绍了。

从以上介绍我们可以看出,基本上任何一个数学库都有round()、ceil()、floor()这三个函数。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值