hungrytired's blog

By hungrytired, history, 6 years ago, In English

I was trying out this question on AtCoder, https://atcoder.jp/contests/abc169/tasks/abc169_c. Actually in cpp i was doing double a = 9.79 and then long long b = 9.79*100 and weird thing b was assigned 978 not 979. I am confused why is this happening. If someone can put a blog link which might explain the problem further. Thanks a lot.

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
6 years ago, hide # |
← Rev. 3  
Vote: I like it +21 Vote: I do not like it

First $$$9.79$$$ is presented in $$$2$$$ power: $$$2^3 + 2^0 + ... + 2^{-x} + ... + 2^{-y} + ...$$$, and it is not exactly $$$9.79$$$ only approximate. Using $$$round(9.79 \cdot 100)$$$ instead.