0

I wrote something similar to this simple loop in my library:

#include <cmath>    
#include <iostream>    

int main()    
{    
    auto result {-1};      

    for (char i = 1; i < 10; ++i)   
        result += sqrt(i) / i;   

    std::cout << result << std::endl;    

    return 0;     
}    

and spent 2.5 hours figuring out what is wrong in my code.

Compiling this main.cpp file with `g++ -O3 -std=c++2a -Wall -Wpedantic -Wunused-parameter main.cpp -o main" gives me absoutely no warning about converting the double to integer.

The false output of this program using g++ (GCC) 9.1.0 is "0" and no warning about a total loss in precision. AFAIK, implicit conversion will make a double out of sqrt(i) / i and this double is then assigned to int, which should at least let me know "hey buddy, you are losing almost all precission there". Especially with -Wall.

Why is that? Did I miss something?

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
tmaric
  • 5,347
  • 4
  • 42
  • 75

0 Answers0