You're probably getting rounding errors,
so, you may try std::lround
as:
key += cur_foo.get_num_at(cur_foo_num) * std::lround(std::pow(10, cur_foo.get_foo_length() - cur_foo_num - 1));
or write your own pow_int
function to avoid to use float
:
constexpr int pow_int(int x, unsigned int n)
{
// x ** (2n + 1) == ((x * x) ** n) * x
// x ** 2n == (x * x) ** n
// x ** 0 == 1
return (((n >> 1) == 0) ? 1 : pow_int(x * x, n >> 1)) * (((n & 1) == 0) ? 1 : x);
}
or (linear version)
int pow_int(int x, unsigned int n)
{
int res = 1;
for (unsigned int i = 0; i != n; ++i) {
res *= x;
}
return res;
}