From: throopw@sheol.UUCP (Wayne Throop)
Newsgroups: talk.origins
Subject: Holden and the C programming language
Summary: Ted's program as posted leaves much to be desired
Date: 7 Oct 92 22:40:15 GMT

: From: news@fedfil.UUCP (news) (Ted Holden)
: Message-ID: <45@fedfil.UUCP> 
: Is that a little bit easier to comprehend?

I understood it well enough before.  I understood it well enough to see
clearly that Ted wasn't taking account of the differences in shape
between Kazmaier and sauropod dinosaurs.  He still isn't.  (He's also
bringing up the "steroids" argument again, which he agreed was not a
valid argument a few years back, after I pointed out that steroids are
naturally available, and nothing prevented sauropods from producing as
much steroid as needed, yea, even more than Kazmaier ingested.)

But that's not what I found most interesting.  After all, I'm perfectly
willing to agree that animals shaped exactly like Kazmaier but massing
more than 20000 lbs are essentially impossible in 1g acceleration.  What
interested me was Ted's inclusion of a program in C that in fact is NOT
as "easy to comprehend" as it could be.  The program itself is appended to
this message, along with some improvements which I will suggest. 

First of all, the program as posted is not ANSI conforming.  Nor is it a
legal K&R C program.  Specifically, it is illegal to declare the main
routine void in ANSI C, and in K&R C it is illegal to use void as a
prototype (that being something that the ANSI comittee invented).  So,
right off the bat, Ted's program limits its portability to those systems
that support whatever pidgin C language Ted programs in, for no
particular reason.  And, of course, the prototype declaration is
redundant in any event. 

Next, remember Kernighan and Plaugher's lament in "The Elements of
Programming Style", where they say that far too many programs are
written that sort exactly 500 integers, or print exactly three address
labels, or whatnot.  They call this "entering parameters via the
compiler".  Ted's program similarly takes no inputs, and performs only
one constant calculation.  This is clearly bad style.  It would be
better to read input from stdin, or interpret an argument to the
main routine.

Next (shades of error bars on exponents in astronomical formulae), 
Ted's program uses the approximation of .67 for (2/3).  This sort of
approximation, especially in exponents, can cause severe errors.  In the
posted program, the error in the first "pow" calculation is about 4%. 
Well, 4%, we can live with that...  but it is sloppy, and there's no
reason not to use a more accurate constant. 

( So, when Ted says that "The little program above, or anything like it,
  will tell you that at 200000 lbs, Kaz would be able to squat no more
  than around 94,950 lbs.", he is neglecting to tell you that that's the
  wrong answer for square-cube scaling: the correct answer is 92963 or so. 
  Ted's number has only one digit of accuracy, though he quotes four.  )

Finally, the program contains misleading variable names.  For example,
the variable called "weight" is actually a number proportional to cross
section, not to weight.  The variable called "base350" (why put the
number in the name for the number? again see The Elements of Programming
Style) is also a cross section factor.  The variable called "limit" is
not a limit at all, but a factor of supportable force. 

All in all, Ted's program calculates something *close* to the right
answer.  But if it were a solution to a problem assigned in a software
development methods course, any grade higher than a C would be
outrageous, and that's being generous. 

I suggest two improved versions: a verbose, self-documenting one, and a
brief and to-the-point one, depending on whether one wanted to be very
clear to future readers, or whether one wanted to hack the answer out
quickly.  In no event would I suggest using Ted's version, as it is
unportable, inflexible, inaccurate, and unclear.  The versions I suggest
are appended below, following Ted's original program.  I don't claim my
improved versions are perfect, merely that they are improved in the
specific ways I have mentioned above. 

----------- Ted's version -----------

#include <stdio.h>
#include <math.h>

void main( void);

void main()
{
   double tolift, base350, weight, limit;

   weight  =  pow( 200000.0, .67);
   tolift  =  1350.0;
   base350 =  pow(350.0, .67);

   limit   = (tolift /base350) * weight;

   printf  (" limit = %f\n", limit);

}

----------- improved version -----------

/* scalekaz MASS -- calculate squat Kazmaier could perform scaled by mass */

#include <stdio.h>
#include <math.h>

int main( n, a )
    int n;
    char **a;
{
   double kaz_area, kaz_force, mass, area, force;

   kaz_force  =  1350.0;
   kaz_area   =  pow( 350.0, 2.0/3.0 );

   mass       =  ((n>1) ? atof(a[1]) : 200000.0);
   area       =  pow( mass, 2.0/3.0 );
   force      =  area * (kaz_force / kaz_area);

   printf( "%s%f\n%s%f\n",
           "mass         = ", mass,
           "scaled force = ", force );

   return 0;
}

----------- quick and dirty version -----------

#include <stdio.h>
#include <math.h>

int main( n, a )
    int n;
    char **a;
{
    printf( "%f\n", 
        pow( atof( a[1] ), 2.0/3.0 ) * (1350.0 / pow( 350.0, 2.0/3.0 )) );
    return 0;
}

--
Wayne Throop  ...!mcnc!dg-rtp!sheol!throopw
Newsgroups: talk.origins