Home > OS >  Why does this output 234 in CPP
Why does this output 234 in CPP

Time:08-03

#include <iostream>

using namespace std;

template <typename T, typename E, typename R>
R multiply(T x, E y, R r) {
    return static_cast<typeof(r)>(x * y);
}

int main() {
    double x = multiply(6, 80, uint8_t());
    cout << x << endl;
    return 0;
}

why is this output 224 if 6*80 = 480

  • how can I write this better?

CodePudding user response:

why is this output 224 [sic]

Because you demanded the result to be a byte, and 480 is larger than a byte, so it will roll over.

how can I write this better?

Pretty much any other way, really. For example, you can simply multiply your values, you don't need that function.

If you insist on a templated function, use the same type for the operands. You can nothing by having three different types there if you're multiplying numbers:

template <typename T>
T multiply(T x, T y, T r) {
    return x * y;
}

called like:

auto res = multiply<uint8_t>(6, 80); // 224
auto res = multiply(6, 80);          // 480

CodePudding user response:

like why exactly the result is 224

The value 480 has this representation in binary:

1 1110 0000

That's 9 bits. The 1s represent 256 128 64 32.

If you store this value in an eight bit variable, it can only hold the lowest 8 bits:

1110 0000

The value of this binary number is 128 64 32. In other words, 224.

It's exactly 256 shy of the original value, because a uint8_t does not have a bit to represent 256.

Any non-negative value x stored in a uint8_t will have the value x % 256.

how can I write this better?

Store the result in a variable that can represent 480.

For example,

double x = multiply(6, 80, uint16_t());

Or, for simplicity, since you're already converting the result to a double:

double x = multiply(6, 80, double());
  •  Tags:  
  • c
  • Related