CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22

Thread: atof error

  1. #16
    Join Date
    Apr 1999
    Posts
    27,449

    Re: atof error


  2. #17
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: atof error

    Quote Originally Posted by sandodo
    Thanks! I searched, but didnot find the answer. Maybe not searching enough.
    1/3 cannot be represented by a decimal number, because 3 is not a divisor of 10.
    Similarly, 1/5 cannot be represented by a binary float, because 5 is not a divisor of 2.

    Numbers that can be represented by binary floats are sums of powers of two.
    It's easy to see that these numbers can be expressed with this form:

    integer*2**i where i is a negative or positive integer.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  3. #18
    Join Date
    Jun 2002
    Posts
    137

    Re: atof error

    Thanks Paul McKenzie for the helpful link.

    Quote Originally Posted by SuperKoko
    1/3 cannot be represented by a decimal number, because 3 is not a divisor of 10.
    Similarly, 1/5 cannot be represented by a binary float, because 5 is not a divisor of 2.

    Numbers that can be represented by binary floats are sums of powers of two.
    It's easy to see that these numbers can be expressed with this form:

    integer*2**i where i is a negative or positive integer.
    Thanks. the example of 1/3 cannot be represent by decimal number is very good.

    However for
    Code:
    integer*2**i
    , cannot understand......

  4. #19
    Join Date
    Apr 1999
    Posts
    27,449

    Re: atof error

    Quote Originally Posted by sandodo
    However for
    Code:
    integer*2**i
    , cannot understand......
    Basically, this means that the only decimal fractions that can be represented exactly in binary are sums of powers (negative or positive) of 2.

    For example:

    0.5 can be represented exactly in binary, since:

    0.5 = 1/2 = 1 * 2^(-1) (where "^" means "to the power of"). The "**" that Superkoko mentioned is the same as "to the power or".

    Similarly, 3/16 can be represented in binary exactly since:

    3/16 = 3/(2*8) = 3/(2^(-3)).

    So basically if the denominator is 1, 2, 4, 8, 16, 32, 64, etc. the number can be represented in binary exactly.

    Regards,

    Paul McKenzie

  5. #20
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  6. #21
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: atof error

    Quote Originally Posted by exterminator
    The ** is for "to-the-power". +ve as well as -ve.
    Right.
    Good thing that you mention it, since it wasn't obvious.

    I used the ** operator, in order to avoid confusion with the C xor operator ^.
    But, I probably caused confusion with this least used convention.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  7. #22
    Join Date
    Jun 2002
    Posts
    137

    Re: atof error

    Thanks a lot to all of you. You guys are great.

    All my best!

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured