为什么gcc编译器输出pow(10,2)为99而不是100? [英] Why does gcc compiler output pow(10,2) as 99 not 100?

查看:936
本文介绍了为什么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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆