Pow is being weird?

1
2
3
4
5
6
7
string s = "1234567890";
int x = 0, k = 0;
for(int i = s.length(); i > 0; i--){
    x = x + pow(16, k);
    k++;
    cout << x << endl;
}

This returns
0
1
17
273
4369
69905
1118481
17895697
286331153
-2147483648
I don't understand why the last term is negative?
Last edited on
The type int can only hold a certain range of numbers. Once you go out of the range it can hold. It gives you funky numbers like that. Use a different type. Try long x.
286331153+168 is too big to fit in an int.
Topic archived. No new replies allowed.