[Solved] C/C++ divisions with double and following shift operation


A very small rounding difference, within the range possible for the difference between 64 and 80 bits, could account for the different output. The combination of the truncate to int and shift can magnify a tiny difference. This program:

#include <stdio.h>
int main(){
    double zaehler = -20;
    double teiler = 0.08;
    printf("ergebnis = %d \n", (int) (zaehler/teiler) );
    printf("ergebnis = %d \n", (int) (-20/0.08));
}

prints:

ergebnis = -249 
ergebnis = -250 

If the division gives an answer even a tiny bit less than 250 the truncation to int will get 249. I suggest rounding instead of truncating.

Eclipse, MinGW:

Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\\main.cpp" 
g++ -o TestCPP.exe main.o 

solved C/C++ divisions with double and following shift operation