Here's the first error. x isdouble c = a*x; // c = 507462687

**not**equal to the mathematical constant 0.507462687, because double can't represent it.

x represents the value 0.507462687 +/- eps, where eps is some value of the order of about 1e-15 (double's precision). And as a result of that, when you multiply x by 1e10, you don't get the exact integer 507462687. Instead you get c = 507462687 +/- eps2, where eps2 is now about 0.00001.

Same thing for variable d - it's the integer you want it to be, plus or minus an epsilon of about 0.00001.

Next, the following statement:

tells the computer to find the difference between c and d, round itlong long num = (long long) (c-d);

**down**to the lower integer, and store it in variable num. But because c and d are not integers, c-d most likely also will not be an integer. It could end up being something like 418.999999, which after cast to long long will become 418, and not 419 as you might have hoped.

I hope this sheds some light on this mystery with rounding. Here are a couple of pages for further reading, if you're interested - [1], [2].