Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > -atof- function...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 10 Topic 25349 of 27317
Post > Topic >>

-atof- function , example from section 4.2 K&R2

by arnuld <looting@[EMAIL PROTECTED] > Apr 4, 2008 at 12:42 PM

This is the example from section 4.2, page 71 of K&R2:


double atof( char s[] )
{
  int i, sign;
  double val, power;


  for( i = 0; isspace( s[i] ); ++i )
    {
      /* skipe the leading whitespace */
    }
  
  sign = ( (s[i] == '-') ? -1 : 1 );

  if( s[i] == '+'|| s[i] == '-' )
    {
      ++i;
      /* skip the leading sign, of any */
    }
  
  for( val = 0.0; isdigit( s[i] ); ++i )
    {
      val = (10.0 * val) + (s[i] - '0');
      
      /* s[i] - '0' (zero in quotes)
       * always gives "int i" as output
       */
    }

  if( s[i] == '.' )
    {
      ++i;
      /* skip the dot(.) of a floating point */
    }

  for( power = 1.0; isdigit( s[i] ); ++i )
    {
      val = (10.0 * val) + (s[i] - '0');
      power *= 10.0;
    }

  return sign * val / power;
}


I can't really think of that kind of clever-tricks shown here. checking
for leading space and dot (.) are fine,they are very general things and
easily come to my mind but look at how cleverly the K&R created the
expressions  like (val = 10.0 * val) and (power *= 10.0). these loops
using variables "val" and "power" are pretty much the work of people with
high IQs.  After some work I can understand them but I can not create them
at very 1st place, I dont have that kind of thinking.

These tricks never came to my mind. Is this what we call programming ? if
yes, it is pretty much harder to come to my mind, may be never. I just do
not think this way.
 




 10 Posts in Topic:
-atof- function , example from section 4.2 K&R2
arnuld <looting@[EMAIL  2008-04-04 12:42:14 
Re: -atof- function , example from section 4.2 K&R2
Nick Keighley <nick_ke  2008-04-04 05:08:39 
Re: -atof- function , example from section 4.2 K&R2
CBFalconer <cbfalconer  2008-04-04 09:08:07 
Re: -atof- function , example from section 4.2 K&R2
Richard Heathfield <rj  2008-04-04 17:10:51 
Re: -atof- function , example from section 4.2 K&R2
"Joachim Schmitz&quo  2008-04-05 12:15:32 
Re: -atof- function , example from section 4.2 K&R2
Barry Schwarz <schwarz  2008-04-04 06:24:20 
Re: -atof- function , example from section 4.2 K&R2
danzona@[EMAIL PROTECTED]  2008-04-04 09:44:23 
Re: -atof- function , example from section 4.2 K&R2
CBFalconer <cbfalconer  2008-04-04 16:57:00 
Re: -atof- function , example from section 4.2 K&R2
"Dann Corbit" &  2008-04-04 12:23:39 
Re: -atof- function , example from section 4.2 K&R2
Richard Heathfield <rj  2008-04-04 20:26:44 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Sep 4 23:49:10 CDT 2008.