Getting different output from seemingly identical calculations

前端 未结 4 367
抹茶落季
抹茶落季 2021-01-21 03:15

Can anyone tell me why the calculations on lines 9 and 11, which seem to be identical, produce two different outputs. I know the difference isn\'t that great, but I am using the

4条回答
  •  梦谈多话
    2021-01-21 03:48

    This is an instance of GCC's most common non bug

    That's how float behaves. Because of rounding error you don't get an exact result. It is extremely close to 410 for example 409.99999923. However, if you print it as a float, by default c++ round to 6 figures and thus gives you 410. In the second time, you assign it to integer. In this case, c++ doesn't perform a rounding but a truncation. This is why 409.

提交回复
热议问题