What's wrong with the code?

The code below is the example on my new book. It shows different answers and the author didn't explain it properly, so, anyone tell me? I'm lost!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
    int intVar = 1500000000;
    intVar = (intVar * 10) / 10;
    cout << "intVar = " << intVar << endl;

    intVar = 1500000000;
    intVar = (static_cast<double>(intVar) * 10) / 10;
    cout << "intVar = " << intVar << endl;
    return 0;
}   
Hello, it's because the value made by intVar*10 is too big for type int.
The highest value an int can be on most architectures is 4 bytes, which is 2,147,483,647.
int overflow causes undefined behavior.

doubles can be a lot bigger, which is why he casts the intVar to double before multiplying it by 10.
Last edited on
So wait, it's too big for type int, but the compiler can still display an output, why is that?
The program still tries to multiply intVar by 10 even though the value created by it is too big.
It is actually a run-time error, not a compile-time error, so the compiler doesn't complain (although may give a warning, but no errors).

Once the program runs, there is no error checking (for efficiency reasons) and the int overflow causes undefined behavior. It could cause your program to crash, or will most likely just make intVar now have some weird big number in it.
Last edited on
Okay, that clears it up pretty much. So, when (let's say I) I multiply the variable intVar by 10, the result - 1500000000 - is far too large to fit in a variable int, which is 2147483647, so it leads to weird numbers.

On the other hand, can you tell me when to use casts?
Add another zero to the 15-number, but yes that's a good way of wording it.

Casting (transforming a type into another) use should be minimized.
It's not a problem to cast an int to a double because a double can hold more bytes of data, but casting a larger type into a smaller type will cause problems if the number is too big for the smaller type. Also, casting a negative number into an unsigned type might give unintuitive results as well.
Hope that makes sense, couldn't really think of a better way to word it.

The most often time I see a cast is when non-integer division is needed.
In C++,
1
2
3
int a = 1;
int b = 2;
double c = a / b;
c does not become 0.5, it gives 0 because both 1 and 2 are integers.
A cast can will prevent this.
double c = static_cast<double>(a) / b;

Another case is when you want an object's position to snap to integer coordinates for whatever reason.

Edit: Also, dynamic_casts can be used for OOP inheritance stuff.
Last edited on
Thanks alot, I really appreciated it.
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
Topic archived. No new replies allowed.