home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.misc:3076 comp.unix.programmer:4056 comp.unix.questions:9661
- Newsgroups: comp.unix.misc,comp.unix.programmer,comp.unix.questions
- 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
- From: jdell@maggie.mit.edu (John Ellithorpe)
- Subject: Re: Is there a strtof() function (str to float)?
- In-Reply-To: rupen@falcon.ssc.gov's message of Fri, 31 Jul 1992 20:13:13 GMT
- Message-ID: <JDELL.92Jul31190203@maggie.mit.edu>
- Sender: news@athena.mit.edu (News system)
- Nntp-Posting-Host: maggie.mit.edu
- Organization: Massachusetts Institute of Technology
- References: <1992Jul31.201313.11253@sunova.ssc.gov>
- Date: Sat, 1 Aug 1992 00:02:03 GMT
- Lines: 52
-
- On Fri, 31 Jul 1992 20:13:13 GMT, rupen@falcon.ssc.gov (Rupen Sheth) said:
- > Originator: rupen@falcon
- > Nntp-Posting-Host: falcon.ssc.gov
-
- > Hi:
- > Env: Sun Sparc.
-
- > In trying to convert a string to a floating number, I don't always
- > get the exact value.
-
- > For example:
- > float value;
- > char str[10]; /* Say, str = "+0.02" */
-
- > sscanf(str, "%g", &value);
- > OR
- > sscanf(str, "%f", &value);
-
- > value is 0.0199999995529651642
-
- > Is there a strtof() function that can convert a string to a float?
- > If not, is there another way I can convert a string %f or %g to a
- > float without these rounding errors?
- > I am sure there is a way around this that I am not aware of.
-
- > Please help !!!
- > Respond by email to the address below.
- > Thank you.
- > ---
- > Rupen Sheth
- > -----------------------------------------------------------------------------
- > Superconducting Super Collider Laboratory EMail: rupen@falcon.ssc.gov
- > 2550 Beckleymeade Ave. MS 4004 Phone: (214) 708-3344
- > Dallas, TX 75237 FAX: (214) 708-4827
- > -----------------------------------------------------------------------------
-
- Well, I think one of your key problems is that the value you are printing out
- has far more significant figures than is real. For floats, you only have
- 6 or 7 significant digits, which would then give you 0.02. I would suggest
- using doubles. I tried this on my SPARC and it seemed to work fine anyway
- I did it, always giving me exactly 0.02. Sorry I couldn't be more help.
-
- John
- --
-
- ===============================================================================
- John Ellithorpe | Internet: jdell@maggie.mit.edu
- Dept. of Physics, Rm 26-349 | Phone : (617) 253-3074 Office
- Massachusetts Institute of Technology | (617) 253-3072 Lab
- Cambridge, MA 02139 | (617) 236-4910 Home
- ===============================================================================
-
-