机器Epsilon是最小的EPS(ε),因此1 + EPS不等于1。机器Epsilon是与机器相关的浮点值,由于浮点算术中的舍入,它提供了相对误差的上限。在数学上,对于每种浮点类型,它等于1.0与大于1.0的最小可表示值之间的差。
在C语言中,机器epsilon在标准标头中指定,名称为FLT_EPSILON,DBL_EPSILON和LDBL_EPSILON。这三个宏分别为浮点型,双精度型和长双精度型提供了机器epsilon。
在C++中,标准标头中提供了类似的宏。 C++中的首选方法是使用std :: numeric_limits :: epsilon()–在标准标头中指定。
在Java,它称为ULP(最后一个单位)。您可以使用Java.lang.Math包和Math.ulp()方法找到它。
在Python3中,该信息在sys.float_info中可用,该信息与C99中的float.h相对应。
这与std :: numeric_limits :: epsilon()的C++等效。
>>>导入系统
>>> sys.float_info.epsilon
CPP
// C++ program to find machine epsilon
#include
#include
using namespace std;
// Function for Machine Epsilon with an
// initial value provided as EPS.
void machineEpsilon(float EPS)
{
// taking a floating type variable
float prev_epsilon;
// run until condition satisfy
while ((1+EPS) != 1)
{
// copying value of epsilon into previous epsilon
prev_epsilon = EPS;
// dividing epsilon by 2
EPS /=2;
}
// print output of the program
cout << "Machine Epsilon is : " << prev_epsilon << endl;
}
// Driver Code
int main()
{
// calling function which calculate machine epsilon
// with initial value provided as 0.5
machineEpsilon(0.5);
return 0;
}
Java
// Java program to find machine epsilon
import java.util.*;
class Count{
// Function for Machine Epsilon with an
// initial value provided as EPS.
public static void machineEpsilon(double EPS)
{
// taking a floating type variable
double prev_epsilon = 0.0;
// run until condition satisfy
while ((1+EPS) != 1)
{
// copying value of epsilon
// into previous epsilon
prev_epsilon = EPS;
// dividing epsilon by 2
EPS /=2;
}
// print output of the program
System.out.print( "Machine Epsilon is : "
+ prev_epsilon);
}
public static void main(String[] args)
{
// calling function which
// calculate machine epsilon
// with initial value provided as 0.5
machineEpsilon(0.5);
}
}
// This code is contributed by rishabh_jain
Python3
# Python program to find machine epsilon
# Function for Machine Epsilon with an
# initial value provided as EPS.
def machineEpsilon(EPS):
# taking a floating type variable
# run until condition satisfy
while ((1+EPS) != 1):
# copying value of epsilon
# into previous epsilon
prev_epsilon = EPS
# dividing epsilon by 2
EPS = EPS / 2
# print output of the program
print( "Machine Epsilon is : " ,
prev_epsilon )
# Driven code
# calling function which
# calculate machine epsilon
# with initial value provided as 0.5
machineEpsilon(0.5);
# This code is contributed by
# "rishabh_jain".
输出:
Machine Epsilon is : 1.19209e-07
请注意,以上结果因机器而异。
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。