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

flint at gistdev.UUCP flint at gistdev.UUCP
Tue Feb 7 09:19:00 AEST 1989


/* Written 12:19 pm  Feb  5, 1989 by atanasoff.cs.iastate.edu!hascall in gistdev:comp.lang.c */

>   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.

>From my previous posting: (as George Bush would say: "Read my lips" :-) )
>>It should be noted that I advocated doing this to "simple" loops where it
>>works, not to every loop.  

I believe we both just said the same thing, so I'll assume you are agreeing
with me: you just provided an example of a case you can't optimize as I
stated there would be.


>    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
 
The compiler has two choices here:  1. Do the division right away, as you
just did (something I believe is an error: to use your arguments, "if I wanted
0.666667 instead of 2/3, I would have said 0.66667!"), or 2. be smart, and
multiply the denominators through: this is exactly what it is doing in the
case of decimals.  ie, (so that it is obvious :-) ), this loop:
(Why are we debating this using Fortran syntax for God's sake?)
    do  i = 3.25,4.1,.1
is really the same (when written in terms of fractions) as this one:
    do  i = 325./100.,41./10.,1./10.
and when you multiply everything by 100 and it becomes integer, ie:
   do   i = 325,410,10

So if your user had written (hascall's previous example below)
>       do i=0.0, 4.0, 2.0/3.0
you just multiply everything by 3 to convert it into this:
        do i=0,12,2
        float=i/3.0
And if they wrote this:
        do i=2./3.,15./4.,1./12.
You can multiply each by 12 to get this loop:
	do i=8,45,1
	float=i/12.0

(How to decide what the smallest integer value to multiply through by is left
as a 7th grade level arithmetic exercise for the reader.)

In other words, I was already advocating that the compiler do fractions,
you assumed that I only wanted it to deal with the specific case where
the denominators in the fractions are powers of 10.  If the user writes
2/3 in their code instead of 0.667, and the compiler can make use of the
extra information that 2/3 conveys, then it ought to do so.

I don't think (but I could be wrong: it has happened before :-) ) that
there is any loop you can write that contains only constants for the
loop start/end/increment that cannot be converted into an integer
controlled loop in a straightforward manner.  If you can optimize someone's
program for speed, you ought to be willing to optimize it for accuracy
as well.



More information about the Comp.lang.c mailing list