2 ms·
If you want an exact representation, C99 has the %a format specifier for base-16 floating point. It seems that Python 3 doesn't support it but Lua 5.2 does.
by whoopdedo 13y ago
If you want an exact representation, C99 has the %a format specifier for base-16 floating point. It seems that Python 3 doesn't support it but Lua 5.2 does.
Lua 5.2.2 Copyright (C) 1994-2013 Lua.org, PUC-Rio
> print(string.format(" 0.1 (%a)\n+ 0.2 (%a)\n= %a\n+ 0.3 (%a)\n= %a", 0.1,0.2,0.1+0.2, 0.3, 0.1+0.2+0.3))
0.1 (0x1.999999999999ap-4)
+ 0.2 (0x1.999999999999ap-3)
= 0x1.3333333333334p-2
+ 0.3 (0x1.3333333333333p-2)
= 0x1.3333333333334p-1
> print(string.format(" 0.2 (%a)\n+ 0.3 (%a)\n= %a\n+ 0.1 (%a)\n= %a", 0.2,0.3,0.2+0.3, 0.1, 0.2+0.3+0.1))
0.2 (0x1.999999999999ap-3)
+ 0.3 (0x1.3333333333333p-2)
= 0x1p-1
+ 0.1 (0x1.999999999999ap-4)
= 0x1.3333333333333p-1