Is there a strtof() function (str to float)?

Is there a strtof() function (str to float)?

Post by Rupen She » Sun, 02 Aug 1992 05:13:13



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
-------------------------------------------------------------------------------

2550 Beckleymeade Ave. MS 4004                   Phone: (214) 708-3344
Dallas, TX 75237                                   FAX: (214) 708-4827
-------------------------------------------------------------------------------
--
Rupen Sheth
-------------------------------------------------------------------------------

2550 Beckleymeade Ave. MS 4004                   Phone: (214) 708-3344

 
 
 

Is there a strtof() function (str to float)?

Post by John Ellithor » Sun, 02 Aug 1992 09:02:03




> 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
> -----------------------------------------------------------------------------

> 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
--

===============================================================================

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  
===============================================================================

 
 
 

Is there a strtof() function (str to float)?

Post by LIU CHEUNG WAH STEPH » Tue, 04 Aug 1992 02:32:22


        How about using scanf() to do the job of strtof()?

 
 
 

Is there a strtof() function (str to float)?

Post by D'Arcy J.M. Ca » Mon, 03 Aug 1992 23:23:18



>    In trying to convert a string to a floating number, I don't always
>    get the exact value.
> [...]
>    char  str[10];      /* Say, str = "+0.02" */

Not surprising.  Since most (all?) floating point formats in C are based
on binary then you would have to create a number based on the reciprocals
of the powers of two.  That means you need to build the number by adding
up selected numbers in the last column in the following table:

1/(2 **  0) = 1.0000000000000000000000000
1/(2 **  1) = 0.5000000000000000000000000
1/(2 **  2) = 0.2500000000000000000000000
1/(2 **  3) = 0.1250000000000000000000000
1/(2 **  4) = 0.0625000000000000000000000
1/(2 **  5) = 0.0312500000000000000000000
1/(2 **  6) = 0.0156250000000000000000000
1/(2 **  7) = 0.0078125000000000000000000
1/(2 **  8) = 0.0039062500000000000000000
1/(2 **  9) = 0.0019531250000000000000000
1/(2 ** 10) = 0.0009765625000000000000000
1/(2 ** 11) = 0.0004882812500000000000000
1/(2 ** 12) = 0.0002441406250000000000000
1/(2 ** 13) = 0.0001220703125000000000000
1/(2 ** 14) = 0.0000610351562500000000000
1/(2 ** 15) = 0.0000305175781250000000000
1/(2 ** 16) = 0.0000152587890625000000000
1/(2 ** 17) = 0.0000076293945312500000000
1/(2 ** 18) = 0.0000038146972656250000000
1/(2 ** 19) = 0.0000019073486328125000000
1/(2 ** 20) = 0.0000009536743164062500000
1/(2 ** 21) = 0.0000004768371582031250000
1/(2 ** 22) = 0.0000002384185791015625000
1/(2 ** 23) = 0.0000001192092895507812500

etc ... depending on how many bits available in the mantissa of your format.
Without an infinite mantissa there are lots of real numbers that cannot be
represented accurately.  0.02 (or 1/50) happens to be one of these.

Quote:>    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?

strtod() is what you think you are looking for but as I explained this
won't help you.  If you need 100% accuracy you need to store the numbers
in a different format.  If this is supposed to be money, for example, you
can just use ints or longs with an assumed decimal position.  There is
also [shudder] BCD.

--

D'Arcy Cain Consulting              |   There's no government
Toronto, Ontario, Canada            |   like no government!
+1 416 424 2871          DoD#0082   |

 
 
 

1. need explanation: str(231.126) -> float(231.1260070801) ???

Dear all,

I have a problem with str to float conversion of C.

Below is the sample code for the problem. It prints the following for
the output:
___________________________________________________
C:\>test
String is 231.126, and the float is  231.1260070801
___________________________________________________

I have tried the same scenario in HPUX cc, Borland C++ Builder 4.0,
Borland C++ 5.02. All produced similar results.

Can anybody help me?

___________________________________________________
#include <stdio.h>

main ()
{
        char *s = "231.126";
        float tmp;

        sscanf(s, "%f", &tmp);
        printf("String is %s, and the float is %15.10f\n\n", s, tmp);
___________________________________________________
--

2. Micropolis Raidion LTX performance problem

3. need VAX float to UNIX float function!!!

4. how to switch boot device on sparcstation20

5. Are str* functions okay in C++?

6. Default Documentation Language

7. why are the float math functions unimplemented or defined ?

8. Novice with a Crashed Hard Disk

9. sleep() function in Unix with sub-second granularity (floating point arg)

10. formatting float variables to fprintf function

11. float vs double vs double float

12. Help: converting a float unix motorola in float windows intel

13. Conversion of Vax float to Sun float