相关疑难解决方法(0)

浮动和双重比较最有效的方法是什么?

比较两个double或两个float值的最有效方法是什么?

简单地这样做是不正确的:

bool CompareDoubles1 (double A, double B)
{
   return A == B;
}
Run Code Online (Sandbox Code Playgroud)

但是像这样:

bool CompareDoubles2 (double A, double B) 
{
   diff = A - B;
   return (diff < EPSILON) && (-diff < EPSILON);
}
Run Code Online (Sandbox Code Playgroud)

似乎浪费处理.

有谁知道更聪明的浮动比较器?

c++ algorithm floating-point optimization

495
推荐指数
13
解决办法
39万
查看次数

了解c ++中的浮点变量和运算符(也可能是书籍错误)

我正在编写一个开始的C++类,我的书(从C++ Early Objects第7版开始)有一个非常糟糕的例子,说明如何检查浮点变量的值.

有问题的书籍例子(文件名pr4-04.cpp):

// This program demonstrates how to safely test a floating-point number
// to see if it is, for all practical purposes, equal to some value.
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double result = .666667 * 6.0; 

    // 2/3 of 6 should be 4 and, if you print result, 4 is displayed.
    cout << "result = " << result << endl;

    // However, internally result is NOT precisely equal to 4.
    // …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point g++ cmath pearson

2
推荐指数
1
解决办法
280
查看次数

标签 统计

c++ ×2

floating-point ×2

algorithm ×1

cmath ×1

g++ ×1

optimization ×1

pearson ×1