1

Question related to Why does this double to int conversion not work?



    #include <iostream>
    int main()
    {
        double x = 4.10;
        double j = x * 100;

        int k = (int) j;

        std::cout << k;
     }

     Output: 409

BUT with 10 instead of 100 :



    #include <iostream>
    int main()
    {
        double x = 4.10;
        double j = x * 10;

        int k = (int) j;

        std::cout << k;
     }

     Output: 41

Since the value actually stored is something like 4.099999... I would expect to get 40 because of truncate and not 41 which is the exact value.

What happened ?

olivv
  • 11
  • 2

0 Answers0