6
#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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

8

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
yizzlez
  • 8,757
  • 4
  • 29
  • 44