Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ Initialise double with hex

I want to initialise a double with a hex constant.

double dbl = 0xFEDCBA9876543212;
printf("dbl: %llX\n", (uint64_t)dbl);

I would expect to see as the output:

dbl: FEDCBA9876543212

But I am getting:

dbl: FEDCBA9876543000

Why is this and why are the last 3 bytes being dropped off?

like image 297
John Avatar asked May 15 '26 08:05

John


1 Answers

You are using a 64-bit value to initialize a double, which only holds 53 significant bits. There's one bit of rounding, and the remaining bits are just lost.

like image 158
Mark Ransom Avatar answered May 16 '26 21:05

Mark Ransom