Hi,
Not sure if I am preaching to the converted, or that this is all obvious, but maybe it might be helpful to someone :+)
If you were sure the number should always be positive, then make it
unsigned
or even
unsigned long long
this will allow a much larger maximum.
I like to use the
<cstdint>
which has the number of bits it uses in the name of the type. For example
uint64_t
is unsigned 64 bit.
http://www.cplusplus.com/reference/cstdint/
there is also
std::size_t
which is usually the maximum unsigned
int
your system will handle - presumably the same as
uintmax_t
The problem with casting to
double
is that it cannot represent all numbers exactly, so that might cause one problems possibly. This is despite having 15 or 16 significant figures in a double.
So when doing int math, stick to integral types as much as possible, only convert to double if the number is bigger than
std::size_t
unless you have a reason like the ones
Ganado mentioned.
Cheers