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

前端 未结 8 1290
旧巷少年郎
旧巷少年郎 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 02:09

    Casting to int truncates the number - same as if you'd called floor(currentFib). So even if currentFib is 54.999999... (a number so close to 55 that it will be rounded up when printed), (int)currentFib will produce 54.

提交回复
热议问题