Can someone explain this behavior? I am well aware of machine-level representation of floating point numbers. This seems to be related to printf and its formats.
Both numbers are represented exactly by floating-point notation (check: multiplying by 64 gives an integer).
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
double x1=108.765625;
printf("%34.30f
", x1);
printf("%9.5f
", x1);
printf("%34.30f
", x1*64);
double x2=108.046875;
printf("%34.30lf
", x2);
printf("%9.5f
", x2);
printf("%34.30f
", x2*64);
}
Output:
> 108.765625000000000000000000000000
> 108.76562
> 6961.000000000000000000000000000000
> 108.046875000000000000000000000000
> 108.04688
> 6915.000000000000000000000000000000
Note, the first number gets rounded down, and the second one gets rounded up.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…