Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does gcc compiler output pow(10,2) as 99 not 100? [duplicate]

Tags:

c++

gcc

#include <iostream.h>
#include <math.h>
int main()
{
    int j=2;
    int output;
    output=pow(10,j);
    cout<<output;
    return 0;
}

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?

like image 728
Alok Kumar Maurya Avatar asked Aug 24 '14 17:08

Alok Kumar Maurya


1 Answers

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.

like image 103
yizzlez Avatar answered Oct 13 '22 09:10

yizzlez