Quote:
|
Would the program be faster if you be more precise?
|
Most likely not. Most likely for standard PC's even slower. The standard didn't provide a formal definition for that. Typically for such questions, one needs to test. In almost every case, the answer is the same: test.
There is another issue.
If there are no special requirements to meet when it comes to saving memory, then don't do too much work. For example, if you implement a variable using
unsigned char, the coder that depends on your code, might end up with nasty surprises because he expected a plain
integer.
For your conveniance, assume the
int is the standard integer type, and
double is the default floating point precision type. The ISO says so by the way. Only when special requirement need to be met, deviate.
Examples.
- Salary is usual up to 2 decimals accurate (cents).
float seems to suffice. Nevertheless, implement
double:
Code:
double MyHourSalary = 12.04;
- Index for an array or matrix. Often these containers can hold quite a bit of data. A normal
int often doesn't suffice.
unsigned seems is ok, but might be problematic as well. To make sure we have
type-wise a valid index, we should use
std::size_t :
Code:
#include <cstdlib>
std::size_t Idx = 12;
Double TheArray[Idx];
//Fill the array here. Then we iterate:
for(std::size_t i = 0; i < Idx; ++i}
{
cout<<TheArray[i]<<endl;
}
Was a good question.