#include #include /* g++ -O2 -DN=1973321288 trunc.cpp => diff = 1 <- why? g++ -O2 -DFIXED_A -DN=1973321288 trunc.cpp => diff = 0 g++ -O2 -DFIXED_B -DN=1973321288 trunc.cpp => diff = 0 g++ -O2 -DN=321782473 trunc.cpp => diff = 0 g++ -O2 -DFIXED_A -DN=321782473 trunc.cpp => diff = 0 g++ -O2 -DFIXED_B -DN=321782473 trunc.cpp => diff = 0 */ int toInt32(double sample) { double tmp = sample * (sample > 0 ? 2147483647.0 : 2147483648.0); #ifdef FIXED_A printf("%.30g\n", tmp); #endif #ifdef FIXED_B return (int)floor(tmp); #else return (int)tmp; #endif } double fromInt32(int sample) { return (double)sample / (sample > 0 ? 2147483647.0 : 2147483648.0); } int main() { int r = N; double f = fromInt32(r); int c = toInt32(f); printf("%d -> %.30g -> %d, diff %d\n", r, f, c, r - c); }