Mail Archives: djgpp/2003/04/04/08:16:36
Mylon <no AT way DOT com> wrote:
> On both my Win98 and WinXP machines (I compiled the code separately on each
> one for testing) trying to divide two numbers through use of the forward
> slash results in an almost cosistant 0.
There are two errors in your source code.
> For example, the code:
> float blah;
> blah = (1 / 10);
^^^^^^
This is defined to set blah to 0.0. The reason for that is that both
1 and 10 are integers, so the division will be carried out as it is
for integers. Only then the result will be converted into a floating
point number.
Go back to you C textbook for a longer explanation.
> printf("%f %f %f\n", blah, 0.5, (11 / 10) );
^^ ^^^^^^^
This is even worse, and if you had asked it to, the compiler could
have warned you about it. 11/10 is, again, an integer which in the
absence of any cast, it will be passed as an integer to the library
routine print(). But by using the %f format specifier, you promised
that you would be passing a floating point number --- this is a very
serious programming error.
> What is the problem here?
That you don't know enough C. Learn more.
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -