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?
				
        
 
            
        - 近似搜索的工作原理 2021-01-01
 - 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
 - 如何对自定义类的向量使用std::find()? 2022-11-07
 - C++ 协变模板 2021-01-01
 - 使用/clr 时出现 LNK2022 错误 2022-01-01
 - STL 中有 dereference_iterator 吗? 2022-01-01
 - 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
 - Stroustrup 的 Simple_window.h 2022-01-01
 - 静态初始化顺序失败 2022-01-01
 - 从python回调到c++的选项 2022-11-16
 
						
						
						
						
						
				
				
				
				