home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / misc / 3076 < prev    next >
Encoding:
Internet Message Format  |  1992-07-29  |  2.8 KB

  1. Xref: sparky comp.unix.misc:3076 comp.unix.programmer:4056 comp.unix.questions:9661
  2. Newsgroups: comp.unix.misc,comp.unix.programmer,comp.unix.questions
  3. Path: sparky!uunet!usc!wupost!darwin.sura.net!jvnc.net!yale.edu!think.com!snorkelwacker.mit.edu!bloom-picayune.mit.edu!news.mit.edu!jdell
  4. From: jdell@maggie.mit.edu (John Ellithorpe)
  5. Subject: Re: Is there a strtof() function (str to float)?
  6. In-Reply-To: rupen@falcon.ssc.gov's message of Fri, 31 Jul 1992 20:13:13 GMT
  7. Message-ID: <JDELL.92Jul31190203@maggie.mit.edu>
  8. Sender: news@athena.mit.edu (News system)
  9. Nntp-Posting-Host: maggie.mit.edu
  10. Organization: Massachusetts Institute of Technology
  11. References: <1992Jul31.201313.11253@sunova.ssc.gov>
  12. Date: Sat, 1 Aug 1992 00:02:03 GMT
  13. Lines: 52
  14.  
  15. On Fri, 31 Jul 1992 20:13:13 GMT, rupen@falcon.ssc.gov (Rupen Sheth) said:
  16. > Originator: rupen@falcon
  17. > Nntp-Posting-Host: falcon.ssc.gov
  18.  
  19. > Hi:
  20. >     Env: Sun Sparc.
  21.  
  22. >     In trying to convert a string to a floating number, I don't always
  23. >     get the exact value.
  24.  
  25. >     For example:
  26. >     float value;
  27. >     char  str[10];      /* Say, str = "+0.02" */
  28.  
  29. >     sscanf(str, "%g", &value);
  30. >     OR
  31. >     sscanf(str, "%f", &value);
  32.  
  33. >     value is 0.0199999995529651642
  34.  
  35. >     Is there a strtof() function that can convert a string to a float?
  36. >     If not, is there another way I can convert a string %f or %g to a
  37. >     float without these rounding errors?
  38. >     I am sure there is a way around this that I am not aware of.
  39.  
  40. >     Please help !!!
  41. >     Respond by email to the address below.
  42. > Thank you.
  43. > ---
  44. > Rupen Sheth
  45. > -----------------------------------------------------------------------------
  46. > Superconducting Super Collider Laboratory        EMail: rupen@falcon.ssc.gov
  47. > 2550 Beckleymeade Ave. MS 4004                   Phone: (214) 708-3344
  48. > Dallas, TX 75237                                   FAX: (214) 708-4827
  49. > -----------------------------------------------------------------------------
  50.  
  51. Well, I think one of your key problems is that the value you are printing out
  52. has far more significant figures than is real.  For floats, you only have
  53. 6 or 7 significant digits, which would then give you 0.02.  I would suggest 
  54. using doubles.  I tried this on my SPARC and it seemed to work fine anyway
  55. I did it, always giving me exactly 0.02.  Sorry I couldn't be more help.
  56.  
  57. John
  58. --
  59.  
  60. ===============================================================================
  61. John Ellithorpe                           | Internet: jdell@maggie.mit.edu  
  62. Dept. of Physics, Rm 26-349               | Phone   : (617) 253-3074  Office
  63. Massachusetts Institute of Technology     |           (617) 253-3072  Lab   
  64. Cambridge, MA  02139                      |           (617) 236-4910  Home  
  65. ===============================================================================
  66.  
  67.