delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/04/04/08:16:36

From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Division isn't working?
Date: 4 Apr 2003 13:14:38 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 35
Message-ID: <b6k0fu$fjm$1@nets3.rz.RWTH-Aachen.DE>
References: <Xns9353511EB990EMylonnoway AT 65 DOT 32 DOT 1 DOT 8>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 1049462078 15990 137.226.32.75 (4 Apr 2003 13:14:38 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 4 Apr 2003 13:14:38 GMT
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019