### priojeet_priyom's blog

By priojeet_priyom, history, 9 months ago, ,

I used double for this problem and my solution were hacked. when I changed the variable from double to long double it got accepted. hacked submission , accepted submission.

I tried printing out numeric limits of the double and long double with these

cout<<numeric_limits<double>::max()<<endl;
cout<<numeric_limits<long double>::max()<<endl;

cout<<sizeof(double)<<endl;
cout<<sizeof(long double)<<endl;


OUTPUT: 1.79769e+308 1.18973e+4932 16 8

my question is how can double store 10^308 in 8 Byte memory. If it can, why it is giving WA for 10^18(approx problem limit)?

•
• -19
•

 » 9 months ago, # |   +8 Where is no better way to answer you question than go to google and read about how are floating point numbers made in C++.why it is giving WA for 10^18Double can't store all ~64 digits of numbers up to 10^18 (cause it's 8 byte), so here goes significant precision error.
 » 9 months ago, # |   +20 Yes, double can store numbers up to 10^308. But in order to archive this, it has to use some tricks. From the 8 bytes of data, 1 bit is used for the sign and 11 bits are used as an exponent e. The represented number will be equal to the remaining bits multiplied by 2e. This way it can also represent also very big numbers, but since only 52 bits are used for the actual number, it cannot represent every one of them accurately. E.g. try this short code: double x = 1e18; cout << (x + 1 == x ? "Equal" : "Not equal") << endl; The precision is not good enough to distinguish between x + 1 and x. But that is not a new thing. There are obviously infinitely different numbers between 0 and 1, and you can't express everyone of them using double. And something like the following will also give the "wrong" result: double x = 1; cout << (x + 1e-30 == x ? "Equal" : "Not equal") << endl; That's the dangerous thing about double. The values are usually only an approximation, and not the exact value.For more information read: Double-precision floating-point formatBtw, there are lots of solutions to the problem without using long double. I've even saw a solution with float. I recommend learning from them, as long double will not be able to save you every time.
•  » » 9 months ago, # ^ |   0 Fun fact, cout << (x + 1 == x ? "Equal" : "Not equal") << endl; actually gives "Not equal" for numbers less than 2 * 1e19 (afaik even without optimizer).
•  » » » 9 months ago, # ^ |   0 Double starts giving Equal about 1e16. Long double start giving Equal about 2e19. Tested on my device.