"for" loops (was Re: C++ vs. Modula

John Hascall hascall at atanasoff.cs.iastate.edu
Mon Feb 6 04:19:10 AEST 1989


In article <7800003 at gistdev> flint at gistdev.UUCP writes:


>It should be noted that I advocated doing this to "simple" loops where it
>works, not to every loop.  In the
>    do 10 i=0,1,0.1
>case, you can KNOW to run this loop exactly 11 times: but not if you stupidly
>try to divide 1/0.1 with binary based arithmetic, where it might round to a
>result less than 10.  Your user wrote the loop in decimal, and in decimal the
>arithmetic produces an exact result.  Think the math is too complex a task
>for your compiler to do?  Then try this simple trick:
>What the programmer wrote in their code:             do  10 i=2.5,3.75,0.25
>Move the decimal points in the text 2 places right:  do  10 i=250,375,25
>(Manipulate the text string if you feel the need.)
>Bingo, you've got an integer loop, you know to get the floating loop
>index you divide by (10 ** 2) or 100 (because you slid the decimal places
>2 positions in order to get rid of all the digits right of the decimal
>points.)  

   Sure, it works in some simple cases, but how do you propose to
   handle:

        start = expression-which-gives_2.5
	end = expression-which-gives_3.75
	incr = expression-which-gives_0.25
	do 10 i=start,end,incr

    now we don't know which power of 10 to use, having no "textual"
    information from the users program.

    Or how about:

       do i=0.0, 4.0, 2.0/3.0

    I suppose now our compiler has to do fractions too...because to
    you and I the meaning is clear, but to a compiler it's:

       do i=0.0, 4.0, 0.6666667

    giving the sequence:               instead of the expected:

       0.0000000                          0.0000000
       0.6666667                          0.6666667
       1.3333334                          1.3333333
       2.0000001                          2.0000000
       2.6666668                          2.6666667
       3.3333335                          3.3333333
       ---------                          4.0000000
       4.0000002                          ---------
					  4.3333333


      I don't know who said it, but, "If you make a system fool-proof, only
   a fool will use it"

   John Hascall
   ISU Comp Center

   p.s. liberally apply :-)'s above, I just like to disagree, it provokes
        thought.



More information about the Comp.lang.c mailing list