Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 0.69 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Odd behavior when converting C strings to/from doubles
  2. #include <errno.h>
  3. #include <float.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. int main(int argc, char **argv) {
  9.     double x, y;
  10.     const char *s = "1e-310";
  11.  
  12.     /* Should print zero */
  13.     x = DBL_MIN/100.;
  14.     printf("DBL_MIN = %e, x = %en", DBL_MIN, x);
  15.  
  16.     /* Trying to read in floating point number smaller than DBL_MIN gives an error */
  17.     y = strtod(s, NULL);
  18.     if(errno != 0)
  19.         printf("  Error converting '%s': %sn", s, strerror(errno));
  20.     printf("y = %en", y);
  21.  
  22.     return 0;
  23. }
  24.        
  25. DBL_MIN = 2.225074e-308, x = 2.225074e-310
  26.   Error converting '1e-310': Numerical result out of range
  27. y = 1.000000e-310