0

I am writing a program where I want to know a number can be written in the form of x^y.

I know that log(base, a) = log2(a)/log2(base);

input => 823543
output => 7^7

So if I am doing:

double res = log(823543)/log(7);
int intRes = (int) res;
cout << res << ", "<<intRes << endl;

I'm getting

>>> 7, 6

Why does 7 become 6 when assigning to int from double?

Evg
  • 25,259
  • 5
  • 41
  • 83
  • `res` might be so close to `7` that the output is rounded up. But since it's not *exactly* `7` the conversion to `int` will truncate it to `6`. – Some programmer dude Apr 06 '22 at 06:26
  • Conversion to integer truncates the decimal places after the comma. Since floating point math is not an exact representation of math in the reals, it is possible that the calculated floating point number is slightly lower than `7` and will be truncated to `6`. – user17732522 Apr 06 '22 at 06:26

0 Answers0