Why is my integer math with std::pow giving the wrong answer?(为什么我的 std::pow 整数数学给出了错误的答案?)
问题描述
考虑以下代码:
#include <iostream>
#include <cmath>
int main() {
int i = 23;
int j = 1;
int base = 10;
int k = 2;
i += j * pow(base, k);
std::cout << i << std::endl;
}
它输出122"而不是123".它是 g++ 4.7.2 (MinGW, Windows XP) 中的错误吗?
It outputs "122" instead of "123". Is it a bug in g++ 4.7.2 (MinGW, Windows XP)?
推荐答案
std::pow()
适用于浮点数,它没有无限精度,并且您使用的标准库的实现可能实现 pow()
以一种(较差的)方式使这种缺乏无限精度变得相关.
std::pow()
works with floating point numbers, which do not have infinite precision, and probably the implementation of the Standard Library you are using implements pow()
in a (poor) way that makes this lack of infinite precision become relevant.
但是,您可以轻松定义自己的整数版本.在 C++11 中,您甚至可以将其设为 constexpr
(以便尽可能在编译时计算结果):
However, you could easily define your own version that works with integers. In C++11, you can even make it constexpr
(so that the result could be computed at compile-time when possible):
constexpr int int_pow(int b, int e)
{
return (e == 0) ? 1 : b * int_pow(b, e - 1);
}
这是一个现场示例.
尾递归形式(归功于 Dan Nissenbaum):
Tail-recursive form (credits to Dan Nissenbaum):
constexpr int int_pow(int b, int e, int res = 1)
{
return (e == 0) ? res : int_pow(b, e - 1, b * res);
}
这篇关于为什么我的 std::pow 整数数学给出了错误的答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我的 std::pow 整数数学给出了错误的答案?


- 将函数的返回值分配给引用 C++? 2022-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- XML Schema 到 C++ 类 2022-01-01
- 将 hdc 内容复制到位图 2022-09-04
- OpenGL 对象的 RAII 包装器 2021-01-01
- 如何提取 __VA_ARGS__? 2022-01-01
- DoEvents 等效于 C++? 2021-01-01
- GDB 不显示函数名 2022-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01