Search code examples
c++castinglong-integeroperator-precedence

c++ order of precedence - casting before multiplying


In the C++ function below, why is numbers[max_index1] cast to long long and then multiplied by numbers[max_index2]? I would of thought that you'd multiply the numbers and then cast?

Also would it not make more sense to make the vector numbers type long long instead of int therefore the casting wouldn't be necessary?

long long MaxPairwiseProductFast(const vector<int>& numbers) {

    int n = numbers.size();

    int max_index1 = -1;
    cout << "value at max_index1 is " << numbers[max_index1] << std::endl;
    for(int i = 0; i < n; i++)
        if((max_index1 == -1) || (numbers[i] > numbers[max_index1]))
            max_index1 = i;


    int max_index2 = -1;
    for(int j = 0; j < n; j++)
        if((numbers[j] != numbers[max_index1]) && ((max_index2 == -1) || (numbers[j] > numbers[max_index2])))
            max_index2 = j;

    return ((long long)(numbers[max_index1])) * numbers[max_index2];
}

int main() {
    int n;
    cin >> n;
    vector<int> numbers(n);
    for (int i = 0; i < n; ++i) {
        cin >> numbers[i];
    }

    long long result = MaxPairwiseProductFast(numbers);
    cout << result << "\n";
    return 0;
}

Solution

  • ((long long)(numbers[max_index1])) * numbers[max_index2];
    

    numbers[max_index2] will be promoted to long long before multiplication is performed. If you multiply two int's and the result overflowed, there is nothing you can achieve by casting that result to long long, so you cast first, then multiply.

    Also would be not make more sense to make the vector numbers type long long instead of int therefore the casting wouldn't be necessary?

    If you know that the individual numbers will fit an int, but the result of multiplying two int's can overflow, this will help save space.