Definition of a large integer value

I have a project where I deal with large numbers (ns-timestamps) that do not fit into an integer. So I want to use, for example, int64_t, and I'm currently writing a test case (yes!).

To test the behavior for a large number, I started with something like

int64_t val = 2*std::numeric_limits<int>::max(); qDebug() << "long val" << val; 

which returns

 long val -2 

(just as if I were defining val as int).

But if I write

 int64_t val = std::numeric_limits<int>::max(); val *= 2; qDebug() << "long val" << val; 

I get

 long val 4294967294 

which looks right.

So, for me, it looks as if 2*max() first stored in integer (truncated at this step), and then copied to int64 . Why is this happening? The compiler knows that the result is of type int64 , so it must match 2*max() directly.

+6
source share
1 answer

So, for me, it looks like 2*max() first stored in integer (truncated at this step), and then copied to int64

This is absolutely correct. According to the language specification, when all parts of an expression fit into an int , the calculation is performed in integers. In your case, both 2 and max() fit into int , so multiplication is done in integers, causing an overflow.

The compiler knows that the result is of type int64 so that it 2*max() must match directly.

The result to which the expression is assigned does not matter in this situation: the expression itself directs the calculation method. You can achieve the same result by hovering max() in int64 :

 int64_t val = 2*(int64_t)std::numeric_limits<int>::max(); 
+5
source

Source: https://habr.com/ru/post/971909/


All Articles