- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在学习 C++ 入门类(class),我的书(从 C++ 早期对象开始第 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.
// So test to see if it is "close" to 4.
if (abs(result - 4.0 < .0001))
cout << "result DOES equal 4!" << endl;
else
cout << "result DOES NOT equal 4!" << endl;
return 0;
}
我在 Ubuntu 中使用 g++ 来编译我的代码,如下所示:
g++ pr4-04.cpp -o pr4-04 && ./pr4-04
我得到这个错误:
error: call of overloaded ‘abs(bool)’ is ambiguous
我可以通过将 abs() 更改为 fabs() 来解决这个问题,但这仍然非常令人困惑!为什么这本书给了我们无法编译的东西,或者这只是我?为什么 'result' 的 cout 给出 4 而不是 4.000002?为什么这个值在 if{} 语句中使用时似乎发生了变化?
我知道我们不能只使用 == 来检查等价性,但为什么我需要使用绝对值?无论我是否使用它,我都会得到相同的答案。那有什么意义呢?
更不用说,这似乎是一种非常糟糕的检查浮点等价性的方法。有一个更好的方法吗? This topic seems awfully important .
我找到了 this topic在 stackoverflow 上,但他们的解决方案:
fabs(f1 - f2) < precision-requirement
fabs(f1 - f2) < max(fabs(f1), fabs(f2)) * percentage-precision-requirement
在我 4 章的 C++ 经验中,这对我来说意义不大。我将不胜感激一些帮助。我们的书给了我多达 6 句话的文字来解释所有这些。
编辑:根据一些人的建议,我试图找到一个勘误表页面,但在搜索教科书、互联网和我的类(class)网站 30 分钟后,我只能找到 this downloadable zip file。 , 这需要登录 -_-
我也完美复制了代码。那不是我的错字,我直接从带有代码的 CD 上复制了它。书上也是这样打的。
最佳答案
if (abs(result - 4.0 < .0001))
括号错了,你的意思可能是:if (abs(result-4.0) < .0001)
.
至于为什么没有编译,标准在 §26.8p8 中确定
In addition to the double versions of the math functions in , C++ adds float and long double overloaded versions of these functions, with the same semantics.
表达式(result-4.0 < .0001)
产生 bool
,并且没有重载 abs
这需要 bool
参数,但有多个版本的 abs
其参数可从 bool
隐式转换.编译器没有找到比其余转换序列更好的转换序列之一,并因歧义错误而退出。
关于c++ - Understanding floating point variables and operators in c++(也可能是书上的错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12649478/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎离题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a minim
家庭作业 必须同时重载 operator>(istream &in, Complex &value); public: // constructor Compl
这个问题在这里已经有了答案: What are the basic rules and idioms for operator overloading? (8 个答案) 关闭 5 年前。 有什么区别
这是文档中一个不清楚的示例,使用此运算符:http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#at 最佳答案 请注
我不明白这种行为: > sort([1,2,3,4]) ~~ sort([1,2,3,4]) False 你能给我解释一下吗? 为什么这两个列表(显然是相等的)根据 Perl 6 不相等。 更新 有趣
我正在尝试将 Ø 设为逻辑否定运算符。 ¬ True; multi sub prefix: ($n) { return not $n; } 当我运行上面的程序时,它返回以下错误: $
class Port { private: char * brand; char style[20]; // i.e., tawny, ruby, vintage int bo
早上好。我有一些问题。我有这些字段: name: "Mike", city: "NY", address: "something", pets: ["dog", "cat"] 我创建了索引 db.pe
我有以下代码使用 Javascript Webcrypto-API 解密 AES 加密数据,但它会导致“OperationError”并显示消息“操作因操作特定原因而失败”: function loa
我制作了一个自定义 Airflow 操作符,这个操作符接受一个输入,这个操作符的输出在 XCOM 上。 我想要实现的是使用一些定义的输入调用运算符,将输出解析为可在分支运算符内部调用的 Python,
int a = 1; a += ++a; cout << a << endl; // 4 int a = 1; a += a++; cout << a << endl; // 3 为什么这两个例子有不
我有这个队列的实现: #include using namespace std; template struct elem_q { T inf; elem_q* link; }; template
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我对 C++ 中的运算符重载有疑问。我有代表复数的结构,我正在重载运算符,所以我可以用复数进行计算。Visual Studio 2012 给我这个错误:1 IntelliSense:没有运算符“!=”
我正在尝试创建一个 BoolArray 类,它表示一个 bool 值数组,而不是为每个 bool 变量保存 1 个 bool 值。它使用 1 个字符变量来表示使用位的 8 个 bool 值,并节省内存
我类有 2 个运算符有点问题。 我的类(class)宣布: template class MyMap{ keyType keys[MAX]; valueType values[MAX
我有类的迭代器模板和用于 for 语句的类。 template class Itr2 { public: Itr2() { } ~Itr2() { } typedef t
Section 7.2 Enumeration declarations 没有说明任何关于 operator!=() 和作用域的 operator==()枚举。但是下面的代码可以编译。 #includ
我需要将一个 int 序列化到本地文件并将其读入内存。这是代码 #include "stdafx.h" #include #include using namespace std; int _tm
operator += 这样定义对吗?! void operator +=(const BigNumber& other) { *this=(*this) + other; } 在这样的类中:
我是一名优秀的程序员,十分优秀!