delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/04/04/08:31:33

From: Walter Briscoe <wbriscoe AT ponle DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Division isn't working?
Date: Fri, 4 Apr 2003 14:19:52 +0100
Lines: 86
Message-ID: <wOmLFHB4ZYj+Ew$z@ponle.demon.co.uk>
References: <Xns9353511EB990EMylonnoway AT 65 DOT 32 DOT 1 DOT 8>
NNTP-Posting-Host: ponle.demon.co.uk
Mime-Version: 1.0
X-Trace: news.demon.co.uk 1049462493 24943 194.222.194.7 (4 Apr 2003 13:21:33 GMT)
X-Complaints-To: abuse AT demon DOT net
NNTP-Posting-Date: Fri, 4 Apr 2003 13:21:33 +0000 (UTC)
User-Agent: Turnpike/6.02-U (<rq8rxKjVGtMiYN2c4l2LNUa4Mz>)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

In message <Xns9353511EB990EMylonnoway AT 65 DOT 32 DOT 1 DOT 8> of Fri, 4 Apr 2003
12:55:36 in comp.os.msdos.djgpp, Mylon <no AT way DOT com> writes
>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.
>
>For example, the code:
>
>       float blah;
>       blah = (1 / 10);
>       printf("%f %f %f\n", blah, 0.5, (11 / 10) );
>
>prints "0.000000 0.500000 0.000000"
>
>while the code:
>
>       int blah;
>       blah = (1 / 10);
>       printf("%f %f %f\n", blah, 0.5, (1 / 2) );
>
>prints "0.000000 0.000000 0.000000" (not even the .5 displays properly.)
>
>What is the problem here?

The problem is that you do not understand what you write;  I advise
reading documentation; C is not arithmetic; 1/10 is not 0.1; 0.1 is not
a float value; a %f specifier does not take an int or a float argument.
Somebody will probably do you NO failure and tell you what the
corrections are. The following may provide some clues for YOU to correct
your code.

C:\WINNT\Temp\42> nl foo.c
     1  void foo(void) {
     2          float blah;
     3          blah = (1 / 10);
     4          printf("%f %f %f\n", blah, 0.5, (11 / 10) );
     5  }


C:\WINNT\Temp\42> nl bar.c
     1  void foo(void) {
     2          int blah;
     3          blah = (1 / 10);
     4          printf("%f %f %f\n", blah, 0.5, (11 / 10) );
     5  }


C:\WINNT\Temp\42> gcc  -O2 -DMSDOS -Iproto  -Wall -Dinterrupt= -Dfar= -DMAXMEM=512 -D_NAIVE_DOS_REGS -c foo.c
foo.c: In function `foo':
foo.c:4: warning: implicit declaration of function `printf'
foo.c:4: warning: double format, different type arg (arg 4)

C:\WINNT\Temp\42> gcc  -O2 -DMSDOS -Iproto  -Wall -Dinterrupt= -Dfar= -DMAXMEM=512 -D_NAIVE_DOS_REGS -c bar.c
bar.c: In function `foo':
bar.c:4: warning: implicit declaration of function `printf'
bar.c:4: warning: double format, different type arg (arg 2)
bar.c:4: warning: double format, different type arg (arg 4)

C:\WINNT\Temp\42> lint -u foo.c
PC-lint for C/C++ (NT) Ver. 7.50wfb, Copyright Gimpel Software 1985-1997

--- Module:   foo.c
        blah = (1 / 10);
                       ^
foo.c(3) : Warning 653: Possible loss of fraction
        printf("%f %f %f\n", blah, 0.5, (11 / 10) );
        ^
foo.c(4) : Info 718: printf undeclared, assumed to return int
foo.c(4) : Info 746: call to printf() not made in the presence of a prototype
                                                   ^
foo.c(4) : Warning 559: Size of argument no. 4 inconsistent with format
C:\WINNT\Temp\42> lint -u bar.c
PC-lint for C/C++ (NT) Ver. 7.50wfb, Copyright Gimpel Software 1985-1997

--- Module:   bar.c
        printf("%f %f %f\n", blah, 0.5, (11 / 10) );
        ^
bar.c(4) : Info 718: printf undeclared, assumed to return int
bar.c(4) : Info 746: call to printf() not made in the presence of a prototype
                                                   ^
bar.c(4) : Warning 559: Size of argument no. 2 inconsistent with format
bar.c(4) : Warning 559: Size of argument no. 4 inconsistent with format
C:\WINNT\Temp\42>

-- 
Walter Briscoe

- Raw text -


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