need explanation: str(231.126) -> float(231.1260070801) ??? 
Author Message
 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);

Quote:
}

___________________________________________________
--



Tue, 14 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???

Quote:

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

Your code prints out a double equivalent of your float variable
(that's how the printf works, that's how ellipsis works).  The
conversion adds some digits that become visible.  The actual
number (when it is of type float) probably contains only 7 decimal
digits (common implementation of the float type based on IEEE).
So, whatever you see beyond those 7 digits, is brought to you by
the conversion.  Know that it's there and why, and ignore it.

Quote:
> }

Victor
--
Please remove capital A's from my address when replying by mail
--



Wed, 15 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???

Quote:
>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?
>     printf("String is %s, and the float is %15.10f\n\n", s, tmp);

Limit this field to "%.3f" if you don't want to see anything after
..xxx. Also, %15.10f means, "15 spaces including the decimal point
with 10 of the spaces to the right of the decimal point always
to be printed". Thus you are only padding 4 digits to the left
of the decimal point.

--

- Mark (email information at http://www.embeddedfw.com)

Above all, try to be more like me - it makes me feel validated.
--



Wed, 15 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???
See the C FAQ:

http://www.eskimo.com/~scs/C-faq/s14.html

-Mike

Quote:

> 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);
> }
> ___________________________________________________
> --


--



Wed, 15 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???

Quote:

> 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);
> }
> ___________________________________________________

the problem isn't in the conversion from a string.  you would still
have the same problem even if you had just directly assigned 231.126
to tmp.  the problem is that many (most?) decimal numbers just aren't
capable of being exactly represented internally in a float variable.

here's an explanation from the comp.lang.c FAQ:

14.1:   When I set a float variable to, say, 3.1, why is printf printing
        it as 3.0999999?

A:      Most computers use base 2 for floating-point numbers as well as
        for integers.  In base 2, one divided by ten is an infinitely-
        repeating fraction (0.0001100110011...), so fractions such as
        3.1 (which look like they can be exactly represented in decimal)
        cannot be represented exactly in binary.  Depending on how
        carefully your compiler's binary/decimal conversion routines
        (such as those used by printf) have been written, you may see
        discrepancies when numbers (especially low-precision floats) not
        exactly representable in base 2 are assigned or read in and then
        printed (i.e. converted from base 10 to base 2 and back again).
        See also question 14.6.

---
"... What with you being his parents and all, I think that you could
be trusted not to shaft him."  -- Robert Chang, rec.games.board


--



Wed, 15 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???
That's because float numbers (as well as double or long double numbers) are
not 100% accurate. 231.1260070801 is the nearest float value to
231.1260070801.


Quote:
> 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);
> }
> ___________________________________________________
> --


--



Wed, 15 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???
[snip]

Quote:
> ___________________________________________________
> C:\>test
> String is 231.126, and the float is  231.1260070801
> ___________________________________________________

Floating point doesn't gaurantee an exact representation.  This must be in
the clc FAQ somewhere:

http://www.eskimo.com/~scs/C-faq/top.html

--Steve
--



Wed, 15 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???

Quote:

> 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);
> }
> ___________________________________________________
> --


You're running across the resolution of floating point representation
of numbers.  Float (and double, etc) numbers are represented as a
signed mantissa (the most significant bits of the number) and a signed
exponent (power of two).  All this still fits into 32 bits (IIRC, 64 for
double).  That leaves you only 2^32 different numbers you can represent,
which means your accuracy suffers.  The first 16 bits of mantissa for
231.126 are 11100111.00100000 (the next bits are 0100 0001 1000 1001 ...)
In exactly the same way you can't write 1/3 out simply in base 10, you
can't represent 231.126 in base 2 without infinite digits.

231.1260070801 is 11100111.001 000 000 100 001 00
231.1260000000 is 11100111.001 000 000 100 000 110 001 001 001 101 110
                           100 101 111 000 110 101 001 111 110 111 110
                           011 101 101 100 100 010 110 100 001 110 010
                           101 100 000 010 000 011 000 100 100 110 111
                           010 010 111 100 011 010 100 111 111 011 111
                           ... remaining bits left as an exercise

So you can see that your nice clean decimal number was rounded to the
nearest 1/32768's (or possibly 1/65536's).

If you're really concerned with keeping numbers accurate to the
nearest 0.001, multiply all of them by 1000 and treat them as
scaled integers.

                                        Mario

--
 <|  A crash reduces          |>
  |> Your expensive computer <|

--



Wed, 15 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???
The explanation is very simple.  Computers are not decimal
machines, they are binary.  As a result, everything decimal
must be converted to binary.  Some decimal fractions are
irrationals when expressed in binary, and as such, it is
impossible to have an exact binary representation.  The
printout is showing you the actual decimal value used which
is the nearest binary equivalent to your decimal input.

--
                Fletcher Glenn

Quote:

> 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);
> }
> ___________________________________________________
> --


--



Wed, 15 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???

Quote:

> String is 231.126, and the float is  231.1260070801
> ...
> Can anybody help me?

Floating-point representations are inherently imprecise.
You have discovered that converting decimal fractions
to binary fractions exposes that inexactness, because
the trailing infinitely long set of bits is truncated
to fit within the finite set of bits used in the
representation.

Double-precision gives you a few more digits of precision
(and usually wider dynamic range), but there is no escape
from this general property.  So don't depend on exactness
and you should get along fine.
--



Wed, 15 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???

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

How much better do you want it? 231.126 is not exactly representable in
binary. It is actually well within spec for a float (that only requires
about six figure accuracy).

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Wed, 15 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???

Quote:

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

The problem is with the floating point representation itself rather
than a problem with the language. Your question is however answered
in the comp.lang.c FAQ :

        http://www.eskimo.com/~scs/C-faq/s14.html

Erik
--
+-------------------------------------------------+

+-------------------------------------------------+
"In my opinion, shareware tends to combine the worst of
commercial software (no sources) with the worst of free
software (no finishing touches). I simply do not believe
in the shareware market at all."  -- Linus Torvalds
--



Wed, 15 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???

Quote:

> Can anybody help me?

        What is the problem? If you need higher accuracy, use higher-precision
types.

        DS
--



Wed, 15 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???

Quote:
> 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
> ___________________________________________________
> ...
>    char *s = "231.126";
>    float tmp;

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

It is the nature of floats (or doubles): they cannot represent every
value accurately, and since. 231.1260070801 is probably as close as it
gets to 231.126, since the numbers are stored as a binary mantissa and a
binary exponent. Try "0.25", that should convert accurately, but "0.26"
will not.

You might print the value with a precision equal to the number of
significant digits:

printf("String is %s, and the float is %15.3f\n\n", s, tmp);

You might even make the precision variable:

int precision = 3;
printf("String is %s, and the float is %15.*f\n\n", s, precision, tmp);

--
Nick Well
---------------------------------------------

Web-based organizer: http://beta.caldonia.com
--



Thu, 16 Jan 2003 03:00:00 GMT  
 need explanation: str(231.126) -> float(231.1260070801) ???

Quote:

<snip>

> the problem is that many (most?) decimal numbers just aren't
> capable of being exactly represented internally in a float variable.

Clearly the answer must be 'most', since there are only a finite
number of float representations, but an infinite number of decimal
numbers.  In fact, one could probably prove that no decimal numbers
are representable as floats :)

/peter

(irrelevant group comp.unix.programmer removed)
--



Fri, 17 Jan 2003 03:00:00 GMT  
 
 [ 19 post ]  Go to page: [1] [2]

 Relevant Pages 

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

2. '>>' on char explanation needed

3. Float to Str

4. Floating Point Math Precision - Definitive Explanation?

5. char **str vs. char *str[]

6. str -> time_t

7. _T(str) vs (CString)str?

8. char *str vs. char str[]

9. Dialogs: m_myCtrl.GetWindowText(str) or GetDlgItemText(IDC_MY_CTRL, str)?

10. Help >>on Float and Double Type

11. SIMPLE EXPLANATION NEEDED.

12. Need some explanation using ctime.

 

 
Powered by phpBB® Forum Software