Why does gcc compiler output pow(10,2) as 99 not 100?(为什么 gcc 编译器输出 pow(10,2) 为 99 而不是 100?)
问题描述
#include <iostream.h>
#include <math.h>
int main()
{
int j=2;
int output;
output=pow(10,j);
cout<<output;
return 0;
}
我将上述代码写入 gcc 12 编译器并得到了输出 99 而不是 100.在各种站点上搜索时我没有得到正确的原因.编译器有问题吗?
I wrote above code to gcc 12 compiler and got the output 99 instead 100. I don't get the valid reason while searching on various sites. Is there any compiler problem?
推荐答案
因为整数截断.pow() 返回一个浮点值,由于浮点运算,大概是~99.999...;然而,由于整数截断,即使 99.999... 也会被截断为 99.
Because of integer truncation. pow() returns a floating point value, and due to floating point arithmetic, it is probably ~ 99.999...; however, due to integer truncation, even 99.999... gets truncated down to 99.
这篇关于为什么 gcc 编译器输出 pow(10,2) 为 99 而不是 100?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 gcc 编译器输出 pow(10,2) 为 99 而不是 100?
基础教程推荐
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
