C++使用负矢量大小计算的 pow 行为
C++ pow behaviour with negative vector size calculations
在调用pow
函数后,如下面的代码所示 它产生一些高数字,就好像它正在访问一些无效的内存位置一样。 我不知道为什么会发生这种情况,任何帮助将不胜感激。
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
int main() {
vector<vector<int>> G = {
{1, 2, 3},
{0, 4}
};
cout << pow(G[1].size() - G[0].size(), 2) << endl;
return 0;
}
这将打印 1.84467e+019。
.size()
的类型是unsigned
的,当左操作数小于右操作数时,你不能简单地减去它们。
试试这个:
cout << pow((long) G[1].size() - (long)G[0].size(), 2) << endl;
~~~~~~ ~~~~~~
但是,此解决方案基于以下假设:强制转换.size()
的结果适合signed long
。
如果你想要一个更具防御性的代码,试试这个:
size_t size_diff(size_t s0, size_t s1)
{
return s0 < s1? (s1 - s0) : (s0 - s1);
}
int main() {
// ...
cout << pow(size_diff(G[1].size(), G[0].size()), 2) << endl;
}
除了接受的答案之外,我还想指出,在 C++20 中,我们将拥有std::ssize()
免费函数,该函数将 size 作为有符号类型值返回。然后
std::pow(std::ssize(G[1]) - std::ssize(G[0]), 2)
将在没有显式类型强制转换的情况下生成正确的结果。
由于pow
将浮点值作为其第一个参数,因此我建议让编译器通过将差异添加到0.0
(或0.0L
(来决定正确的提升:
#include <iostream>
#include <cstdint>
#include <cmath>
using namespace std;
int main()
{
std::string name;
/// 52 of 64 bits used
uint64_t n1 = 0x000ffffffffffffd;
uint64_t n2 = 0x000fffffffffffff;
cout << "plain: " << n1 - n2 << endl;
cout << "float: " << (float)n1 - (float)n2 << endl;
cout << "double: " << (double)n1 - (double)n2 << endl;
cout << "long double: " << (long double)n1 - (long double)n2 << endl;
cout << "0.0+: " << 0.0 + n1 - n2 << endl;
cout << "0.0L+: " << 0.0L + n1 - n2 << endl;
cout << "pow(plain, 2): " << pow(n1-n2, 2) << endl;
cout << "pow(0.0+diff, 2): " << pow(0.0+n1-n2, 2) << endl;
cout << "pow(0.0L+diff, 2): " << pow(0.0L+n1-n2, 2) << endl;
}
输出
plain: 18446744073709551614
float: 0
double: -2
long double: -2
0.0+: -2
0.0L+: -2
pow(plain, 2): 3.40282e+38
pow(0.0+diff, 2): 4
pow(0.0L+diff, 2): 4
表明普通减法出错。即使投射到float
也是不够的,因为float
只提供 23 位尾数。
对于地址空间低于 4.5 PB 的进程,是使用0.0
还是0.0L
来决定实际std::vector::size()
调用返回的size_t
值的差异是理论上的。
所以我认为以下内容就可以了:
cout << pow(0.0 + G[1].size() - G[0].size(), 2) << endl;
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 如何计算文件中的"columns"数?
- 计算排序向量的向量中唯一值的计数
- 在C++中如何在没有pow的情况下进行基础计算
- C++在不使用pow或循环的情况下计算一个数字的幂
- C++使用负矢量大小计算的 pow 行为
- 当n在C 中超过64时,如何计算POW(2,N)
- 用mod和pow计算大数字
- 在不使用函数 pow 的情况下计算表达式的总和
- 如何使用 pow() 函数计算 C++ 中超过 2^32 的幂
- 用双精度计算 pow 会给出错误的结果
- 任何比 pow() 更快的方法都可以在 C++ 中计算 10 的整数幂
- Pow()计算错误
- 为什么pow()计算错误时,Webkit正在运行
- 为什么sqrt()在智能感知中使用两个pow()的内部计算是错误的
- 计算pow(double,2)得到一个巨大的数字(C)