Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why c++ is rounding of big numbers to ceil and small numbers to floor [duplicate]

"x","y" are two long long type variables in c++ to which i have assigned two different numbers.

Variable type is long long but i have assigned decimals to the integer.

so i expected that it will trim the decimal part and display only integer part.

it trimmed off the numbers after the decimal and retured an integer.

enter image description here

Output :

enter image description here

i was expecting floor() of x but it returned some integer ending with 6 instead of 5, i mean it returned ceil(x).but in the second case it returned floor(y).

and its only occurring when the integer is too long.

So what might be the possible reason for this ?

I am using minGW c++17 version on visual studio code .. but same is happening with online compiler also.

like image 771
Cheemakurthi Mukesh Avatar asked Dec 10 '22 00:12

Cheemakurthi Mukesh


1 Answers

Each initialization involves two conversions, first from the decimal numeral in the source text to double, and then from double to long long.

Let’s discuss the second declaration first. Because 2.001 is a double constant, the decimal source text 2.001 must be converted to double. Assuming your C implementation uses IEEE-754 binary64, the result is 2.000999999999999889865875957184471189975738525390625. Then, for the initialization, this double value is converted to long long. This conversion to an integer type discards the fraction, so the result is 2.

In the first declaration, when 9223372036854775.001 is converted to double, the result is 9223372036854776. This is because the two double numbers nearest 9223372036854775.001 are 9223372036854774 and 9223372036854776. The latter one is closer, so it is chosen. Then this double value is converted to long long. There is no fraction part, so the result is simply 9223372036854776.

Thus, the first conversion rounds up because it is not simply converting to the nearest long long value. It first has to round to the nearest double value. And, at the scale of that number, the double format does not have enough resolution to represent every integer. It is representing only every second integer: 9223372036854770, …772, …774, …776, …778, and so on. So 9223372036854775 is not a candidate.

like image 52
Eric Postpischil Avatar answered Dec 12 '22 14:12

Eric Postpischil