a FOR loop and floating variables

Carlos checoimg at gmail.com
Thu May 2 11:14:54 PDT 2013


I have this code :

import std.stdio;
import std.c.stdlib;
void main()
{
int fahr;
write("F\tC\n");
for (fahr = 0; fahr <= 300; fahr = fahr + 20)
write(fahr, "\t", (5.0/9.0)*(fahr-32), "\n");
write("Done!\n");
exit (0);
}
  Which works. but if I change the "5.0" for "5" I get cero on the 
celsius side.

import std.stdio;
import std.c.stdlib;
void main()
{
int fahr;
write("F\tC\n");
for (fahr = 0; fahr <= 300; fahr = fahr + 20)
write(fahr, "\t", (5/9)*(fahr-32), "\n");
write("Done!\n");
exit (0);
}

So why is this ?


More information about the Digitalmars-d-learn mailing list