Because the cuberoot
is very close to 10 but not quite 10. std::cout
truncates and rounds the number to 10, but a double to integer conversion will strip the decimal which is why a = 9
. To solve this problem, you can use std::round()
:
int a=round(cuberoot);