www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Floating point discrepancy in (File.)printf?

reply Marco Falda <Marco_member pathlink.com> writes:
Thank you for your feedback on my previous notification. 
I post here an example of code that in my opinion should generate the same
output on the screen and on the file stream (in this case I was able to reduce
the context :-) ). With my version of dmd, the last that I found on the site
(O.S. WinXP Home SP 2), the file "test.txt" is filled with a pattern such as "n.
.. = 0.000".
It is also possible that I am confusing the modules "stream" and "file", but in
that case I do not understand why the compiler tells me nothing and why the text
is correctly written to the file.
A final observation: if you append some text at the end of "test.txt" and re-run
the executable the new text just added will not be cleared; I think that this is
a designed behaviour, but I am not able to find an explanation in the
documentation.
Thank you in advance for any clue.


import std.stream;

int main(char[][] args)
{
int i;
float n;
File f = new File("test.txt", FileMode.Out);

for (i = 0; i < 10; i++) {
n = 0.333 * i;
printf("n. %d = %1.3f\n", i + 1, n);
f.printf("n. %d = %1.3f\n", i + 1, n);
}
f.close();
return 0;
}
Apr 19 2005
parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
 printf("n. %d = %1.3f\n", i + 1, n);
 f.printf("n. %d = %1.3f\n", i + 1, n);
looks like %f means "float" in printf and "double" in _vsnprintf, which is what file.printf calls (and it's also what std.outbuffer.OutBuffer uses).
Apr 19 2005
parent Marco Falda <Marco_member pathlink.com> writes:
In article <d434qq$qv7$1 digitaldaemon.com>, Ben Hinkle says...
 printf("n. %d = %1.3f\n", i + 1, n);
 f.printf("n. %d = %1.3f\n", i + 1, n);
looks like %f means "float" in printf and "double" in _vsnprintf, which is what file.printf calls (and it's also what std.outbuffer.OutBuffer uses).
In fact I resolved the problem by replacing float with double (I do not know if it is also possible to change the conversion-type).
Apr 21 2005