C / C ++ type int variable calculation results in rotation float / double Inquiry

In programming, often a result of the conversion is used in two type int variables result of the division in order to preserve the accuracy and converted to float or double type, but it is the easiest place to make mistakes is that if you use the following type conversion embodiment, the conversion is a two int type variables to a result obtained by dividing an int type, int type and then converting this result to float or double type, rather than direct calculation result of the reservation with accuracy.

int main ()
{
    int x = 50;
    int y = 12;
    
    cout << x / y << endl;
    
    cout << (float) (x / y) << endl;
    
    cout << (double) (x / y) << endl;
    
    cout << (double) x / (double) y << endl;
    
    return 0;
}

 

4
4
4
4.16667

 

Guess you like

Origin www.cnblogs.com/MK-XIAOYU/p/12466711.html