home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / atof.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  62 lines

  1. /***
  2. *atof.c - convert char string to floating point number
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Converts a character string into a floating point number.
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include <cruntime.h>
  14. #include <fltintrn.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17.  
  18. /***
  19. *double atof(nptr) - convert string to floating point number
  20. *
  21. *Purpose:
  22. *       atof recognizes an optional string of whitespace, then
  23. *       an optional sign, then a string of digits optionally
  24. *       containing a decimal point, then an optional e or E followed
  25. *       by an optionally signed integer, and converts all this to
  26. *       to a floating point number.  The first unrecognized
  27. *       character ends the string.
  28. *
  29. *Entry:
  30. *       nptr - pointer to string to convert
  31. *
  32. *Exit:
  33. *       returns floating point value of character representation
  34. *
  35. *Exceptions:
  36. *
  37. *******************************************************************************/
  38.  
  39. double __cdecl atof(
  40.         REG1 const char *nptr
  41.         )
  42. {
  43.  
  44. #ifdef _MT
  45.         struct _flt fltstruct;      /* temporary structure */
  46. #endif  /* _MT */
  47.  
  48.         /* scan past leading space/tab characters */
  49.  
  50.         while ( isspace((int)(unsigned char)*nptr) )
  51.                 nptr++;
  52.  
  53.         /* let _fltin routine do the rest of the work */
  54.  
  55. #ifdef _MT
  56.         return( *(double *)&(_fltin2( &fltstruct, nptr, strlen(nptr), 0, 0 )->
  57.         dval) );
  58. #else  /* _MT */
  59.         return( *(double *)&(_fltin( nptr, strlen(nptr), 0, 0 )->dval) );
  60. #endif  /* _MT */
  61. }
  62.