在本文中,我们将看到比较浮点数的问题,并且将讨论比较两个浮点数的正确方法。
通常比较浮点数有什么问题?
让我们首先在关系运算符(==)的帮助下比较两个浮点数。
示例:使用“ ==”进行比较
CPP
// C++ program to compare
// floating point numbers
#include
using namespace std;
void compareFloatNum(double a, double b)
{
if (a == b) {
cout << "The numbers are equal"
<< endl;
}
else {
cout << "The numbers are not equal"
<< endl;
}
}
// Driver code
int main()
{
double a = (0.3 * 3) + 0.1;
double b = 1;
compareFloatNum(a, b);
}
Java
// Java program to compare
// floating point numbers
class GFG
{
static void compareFloatNum(double a, double b)
{
if (a == b)
{
System.out.print("The numbers are equal" + "\n");
}
else
{
System.out.print("The numbers are not equal" + "\n");
}
}
// Driver code
public static void main(String[] args)
{
double a = (0.3 * 3) + 0.1;
double b = 1;
compareFloatNum(a, b);
}
}
// This code is contributed by 29AjayKumar
Python
# Python program to compare
# floating point numbers
def compareFloatNum(a, b):
if (a == b):
print("The numbers are equal")
else:
print("The numbers are not equal")
# Driver code
a = (0.3 * 3) + 0.1
b = 1
compareFloatNum(a, b)
# This code is contributed by mohit kumar 29
C#
// C# program to compare
// floating point numbers
using System;
class GFG
{
static void comparefloatNum(double a, double b)
{
if (a == b)
{
Console.Write("The numbers are equal" + "\n");
}
else
{
Console.Write("The numbers are not equal" + "\n");
}
}
// Driver code
public static void Main(String[] args)
{
double a = (0.3 * 3) + 0.1;
double b = 1;
comparefloatNum(a, b);
}
}
// This code is contributed by PrinciRaj1992
C++
// C++ program to compare
// floating point numbers correctly
#include
using namespace std;
void compareFloatNum(double a, double b)
{
// Correct method to compare
// floating-point numbers
if (abs(a - b) < 1e-9) {
cout << "The numbers are equal "
<< endl;
}
else {
cout << "The numbers are not equal "
<< endl;
}
}
// Driver code
int main()
{
double a = (0.3 * 3) + 0.1;
double b = 1;
compareFloatNum(a, b);
}
Java
// Java program to compare
// floating point numbers correctly
class GFG
{
static void compareFloatNum(double a, double b)
{
// Correct method to compare
// floating-point numbers
if (Math.abs(a - b) < 1e-9)
{
System.out.print("The numbers are equal "
+"\n");
}
else
{
System.out.print("The numbers are not equal "
+"\n");
}
}
// Driver code
public static void main(String[] args)
{
double a = (0.3 * 3) + 0.1;
double b = 1;
compareFloatNum(a, b);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python program to compare
# floating ponumbers correctly
def compareFloatNum(a, b):
# Correct method to compare
# floating-ponumbers
if (abs(a - b) < 1e-9):
print("The numbers are equal ");
else:
print("The numbers are not equal ");
# Driver code
if __name__ == '__main__':
a = (0.3 * 3) + 0.1;
b = 1;
compareFloatNum(a, b);
# This code is contributed by PrinciRaj1992
C#
// C# program to compare
// floating point numbers correctly
using System;
class GFG
{
static void comparefloatNum(double a, double b)
{
// Correct method to compare
// floating-point numbers
if (Math.Abs(a - b) < 1e-9)
{
Console.Write("The numbers are equal "
+"\n");
}
else
{
Console.Write("The numbers are not equal "
+"\n");
}
}
// Driver code
public static void Main(String[] args)
{
double a = (0.3 * 3) + 0.1;
double b = 1;
comparefloatNum(a, b);
}
}
// This code is contributed by 29AjayKumar
输出:
The numbers are not equal
为什么会出现此问题?
对于浮点数,关系运算符(==)不会产生正确的输出,这是由于在对浮点数进行四舍五入时存在内部精度错误。
在上面的示例中,我们可以看到使用“ ==”运算符比较两个浮点数时的不准确性。两个数字“ a”和“ b”相等(等于(0.3 * 3)+ 0.1 = 1),但是程序会导致输出错误。
让我们仔细看看下一个片段中的数字。
// C++ program to compare
// floating point numbers
#include
using namespace std;
void printFloatNum(double a, double b)
{
// To print decimal numbers up to 20 digits
cout << setprecision(20);
cout << "a is : " << a << endl;
cout << "b is : " << b << endl;
}
// Driver code
int main()
{
double a = (0.3 * 3) + 0.1;
double b = 1;
printFloatNum(a, b);
}
输出:
a is : 0.99999999999999988898
b is : 1
现在我们可以看到浮点数内部的舍入误差。数字“ a”没有正确地四舍五入为1,
四舍五入有一个内部错误,一个很小的错误,但是当我们比较这些数字时却有很大的不同。
如何正确比较浮点数?
如果我们必须比较两个浮点数,则不必使用“ ==”运算符,我们将发现数字之间的绝对差(如果正确表示,则差将为0),并将其与非常小的值进行比较数字1e-9(即10 ^ -9,这个数字很小),如果相差小于这个数字,我们可以肯定地说两个浮点数相等。
例子:
C++
// C++ program to compare
// floating point numbers correctly
#include
using namespace std;
void compareFloatNum(double a, double b)
{
// Correct method to compare
// floating-point numbers
if (abs(a - b) < 1e-9) {
cout << "The numbers are equal "
<< endl;
}
else {
cout << "The numbers are not equal "
<< endl;
}
}
// Driver code
int main()
{
double a = (0.3 * 3) + 0.1;
double b = 1;
compareFloatNum(a, b);
}
Java
// Java program to compare
// floating point numbers correctly
class GFG
{
static void compareFloatNum(double a, double b)
{
// Correct method to compare
// floating-point numbers
if (Math.abs(a - b) < 1e-9)
{
System.out.print("The numbers are equal "
+"\n");
}
else
{
System.out.print("The numbers are not equal "
+"\n");
}
}
// Driver code
public static void main(String[] args)
{
double a = (0.3 * 3) + 0.1;
double b = 1;
compareFloatNum(a, b);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python program to compare
# floating ponumbers correctly
def compareFloatNum(a, b):
# Correct method to compare
# floating-ponumbers
if (abs(a - b) < 1e-9):
print("The numbers are equal ");
else:
print("The numbers are not equal ");
# Driver code
if __name__ == '__main__':
a = (0.3 * 3) + 0.1;
b = 1;
compareFloatNum(a, b);
# This code is contributed by PrinciRaj1992
C#
// C# program to compare
// floating point numbers correctly
using System;
class GFG
{
static void comparefloatNum(double a, double b)
{
// Correct method to compare
// floating-point numbers
if (Math.Abs(a - b) < 1e-9)
{
Console.Write("The numbers are equal "
+"\n");
}
else
{
Console.Write("The numbers are not equal "
+"\n");
}
}
// Driver code
public static void Main(String[] args)
{
double a = (0.3 * 3) + 0.1;
double b = 1;
comparefloatNum(a, b);
}
}
// This code is contributed by 29AjayKumar
输出:
The numbers are equal
这段代码会产生正确的输出,因此每当比较两个浮点数时,我们将使用上述技术,而不是使用“ ==”运算符。
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。