-2

I use codeblock for compilation.

code:

int main(){
int l=pow(10,2);
cout<<l;
return 0;
}

OUTPUT-99

int main(){
int l=pow(10,3);
cout<<l;
return 0;
}

OUTPUT-1000

Amit Nandal
  • 118
  • 1
  • 6
  • 3
    https://0.30000000000000004.com/. The output of pow(10, 2) is probably something like 99.9999999994 and it truncates to 99 when you convert it to an int – sshashank124 Dec 28 '19 at 10:04
  • but why not for **pow(10,3)**? – Amit Nandal Dec 28 '19 at 10:06
  • 2
    Because that one probably ends up being 1000.0000000001. There are inaccuracies in floating point representation. Print out the values as double instead of int to see the issue – sshashank124 Dec 28 '19 at 10:07
  • 4
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Lukas-T Dec 28 '19 at 10:09

1 Answers1

1

In short - because of integer truncation.

The function pow operates on two floating-point values and returns a floating-point value. When working on integers, the result of pow(10,2) might be stored as 99.9999999, or 100.0000000001. Due to integer truncation, 99.9999999 gets truncated down to 99, and 100.0000000001 gets truncated down to 100.

Same goes for pow(10,3), with 999.9999999, or 1000.0000000001 - which will be truncated to 999 or 1000.

You can read more about the pow function here.