Rating changes for the last round are temporarily rolled back. They will be returned soon. ×

### euler1's blog

By euler1, history, 5 weeks ago, ,

I participated in a recent contest, wherein I submitted this solution for the Multiplication table problem.

My solution passed the pretests but failed on a TC in system testing. In the failed TC, I was printing the square root of a perfect square and it was printed as 1e9 instead of 1000000000.

The square root function was not printing in scientific notation for smaller numbers, but it did for sqrt(1e18) i.e. 1e9. Does anyone know why this happened?

• +4

 » 5 weeks ago, # |   +6 This behavior is due to cout rather than sqrt().
•  » » 5 weeks ago, # ^ |   0 Why does this happen with cout? Do you know any reason for this?
•  » » » 5 weeks ago, # ^ |   +5 cout by default outputs in scientific notation for large float numbers. Usecout << fixed; for fixed point notation.
 » 5 weeks ago, # |   +1 To overcome the problem you can typecast the result into long long , eg., typedef long long ll; ll n = 1e18; ll num = sqrt(n); cout << num << endl;
•  » » 5 weeks ago, # ^ |   0 Yes, it works this way. But do you know the exact reason behind this.. so that I can avoid similar mistakes in future
 » 5 weeks ago, # |   +10 std::sqrt returns a double, not an int. When numbers in doubles are printed, larger numbers are printed in scientific notation. To print it precisely, you have to typecast it into an int.Also, it is better to use std::sqrtl instead of sqrt, because sqrt sometimes fails with numbers upto $10^{18}$.
•  » » 5 weeks ago, # ^ |   +5 Got it. Thanks!!