Why does (int)55 == 54 in C++?

前端 未结 8 1258
旧巷少年郎
旧巷少年郎 2020-12-31 01:32

So I\'m learning C++. I\'ve got my \"C++ Programming Language\" and \"Effective C++\" out and I\'m running through Project Euler. Problem 1...dunzo. Problem 2...not so mu

8条回答
  •  不思量自难忘°
    2020-12-31 01:50

    Due to floating point rounding, that 55 row is computing something like 54.99999. Casting double to int truncates the .99999 right off.

    On my machine, printing a column displaying (currentFib-(int)currentFib) shows errors on the order of 1.42109e-14. So it's more like 0.999999999999986.

提交回复
热议问题