MATLAB There are four functions in the for approximations , Respectively round()、ceil()、floor() 、fix().
Statement : Bloggers currently use MATLAB by R2019a edition , So the link to the help document provided below is R2019a Link to help documentation for .
function round() Used to round to the nearest decimal or integer , You can control the number of digits in the decimal part or integer part ,
For details, see https://ww2.mathworks.cn/help/releases/R2019a/matlab/ref/round.html
function ceil() Used to round to positive infinity , Since it is rounding , Then you can't control the number of digits in the decimal part or integer part , For details, see https://ww2.mathworks.cn/help/releases/R2019a/matlab/ref/ceil.html
function floor() Used to round to negative infinity , Since it is rounding , Then you can't control the number of digits in the decimal part or integer part , For details, see https://ww2.mathworks.cn/help/releases/R2019a/matlab/ref/floor.html
function fix() It is used for 0 Point direction rounding , Since it is rounding , Then you can't control the number of digits in the decimal part or integer part , For details, see https://ww2.mathworks.cn/help/releases/R2019a/matlab/ref/fix.html
C++ There are three functions in the standard math library for approximations , Respectively floor function 、ceil Functions and round function .
Their function and MATLAB The three functions in are basically the same , It's just C++ Of the standard library round Function can only be used to round up , Cannot control the number of decimal places .
The sample code is as follows :
#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 The value of is :" << y1 << std::endl;
std::cout << "y2 The value of is :" << y2 << std::endl;
std::cout << "y3 The value of is :" << y3 << std::endl;
std::cout << "y4 The value of is :" << y4 << std::endl;
std::cout << "y5 The value of is :" << y5 << std::endl;
std::cout << "y6 The value of is :" << y6 << std::endl;
std::cout << "y7 The value of is :" << y7 << std::endl;
return 0;
}
The operation results are as follows :
OpenCV Functions are provided in cvRound、cvCeil、cvFloor To get the approximate value , The functions are the same as those in the standard math library . But should pay attention to , function cvRound It is rounded off , Rather than rounding .
The blogger didn't find OpenCV Direct to Mat A function that approximates each element of an object .
The sample code is as follows :
#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 The value of is :" << y1 << std::endl;
std::cout << "y2 The value of is :" << y2 << std::endl;
std::cout << "y3 The value of is :" << y3 << std::endl;
std::cout << "y4 The value of is :" << y4 << std::endl;
std::cout << "y5 The value of is :" << y5 << std::endl;
std::cout << "y6 The value of is :" << y6 << std::endl;
std::cout << "y7 The value of is :" << y7 << std::endl;
return 0;
}
The operation results are as follows :
Python Of the built-in functions of, only the function round(), It makes a rounding approximation , function round() See the blog post for a detailed introduction of https://blog.csdn.net/wenhao_ir/article/details/125436091 Of the 54 spot .
stay math In the library floor() Functions and ceil() function , The function is the same as above C++ and MATLAB The correlation function in .
The sample code is as follows :
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)
The operation results are as follows :
Python-Numpy Related functions are also provided in , such as numpy.around()、numpy.floor()、numpy.ceil(), For specific introduction, please refer to https://www.runoob.com/numpy/numpy-mathematical-functions.html
I'm not going to go into that .
As we can see from the above introduction , Basically any math library has round()、ceil()、floor() These three functions .