I'm pretty sure it's a bug in printDouble function (at least in the version I used).
I've simulated it in python
import sys
val = float(sys.argv[1]);
precision = int(sys.argv[2])
sys.stdout.write(str(int(val)))
sys.stdout.write('.')
if val>0:
frac = (val-int(val))*precision
else:
frac = (int(val)-val)*precision
frac1 = frac
while (frac1/10!=0):
frac1 = frac1/10
if frac1!=0:
precision=precision/10
while (precision/10!=0):
precision = precision/10
sys.stdout.write('0')
sys.stdout.write(str(int(frac))+'\n')
If I write python buggyprintDouble.py 1.057 1000 it says 1.56,
but 1.157 1000 says 1.157
My first try to fix it,
import sys
val = float(sys.argv[1]);
precision = int(sys.argv[2])
sys.stdout.write(str(int(val)))
sys.stdout.write('.')
if val>0:
frac = (val-int(val))*precision
else:
frac = (int(val)-val)*precision
lfact = 10
while int(lfact*(val-int(val)))==0 and lfact<precision:
lfact*=10
sys.stdout.write('0')
frac1 = frac
while (frac1/10!=0):
frac1 = frac1/10
if frac1!=0:
precision=precision/10
sys.stdout.write(str(int(frac))+'\n')
That's, commet the while(precision/10!=0) bucle and add before a custom lfact correction (just for printing the left zeros).
Now python newprintDouble 1.057 1000 says 1.056 and python newprintDouble 1.157 1000 says 1.057.
Lets traslate it to C++ 8)