Possible ToString() bug

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gbgeek@gmail.com

    Possible ToString() bug

    I've looked around the web a bit and didn't find much luck in locating
    a solution. Here the bit of code that I'm working with:

    strMyValue = "";

    for (double d = double.MinValue ; d < double.MaxValue && numPoints <
    maxPoints; d+=1.1F)
    {
    strMyValue = d.ToString();
    //Here I hand off the string to be used by another object
    }

    The strangeness that I get comes in the conversion d's value to a
    string. In the debug window, I see this value for d:

    -1.7976931348623 157E+308

    After I call d.ToString(), I receive this value for strMyValue:

    "-1.7976931348623 2E+308"

    Somehow the ToString() function managed to round the last few digits
    (157 becomes a 2). Normally, this wouldn't be an issue, but since I'm
    on the edge of the minimum value for a double, this rounding causes the
    object that I had this off to to fail. The way I see it (and I know
    these are two seperate things but hang with me), if the debug window
    can display a textual representation of d's value, then the ToString()
    function should be able to do as good a job.

    Why did I ever create this snippet? I am testing a COM object to
    determine if the object is able to successfully coerce strings back
    into their repective data types. Of course, since the number I'm
    representing is now outside the range of a valid double due to the
    rounding issue, the coercion fails.

    Has anyone else had trouble with the ToString() method? I've tested
    some of the other types and, to this point, only doubles have given me
    any hassle.

    Thanks,
    Gary

  • Chris Dunaway

    #2
    Re: Possible ToString() bug

    gbgeek@gmail.co m wrote:
    I've looked around the web a bit and didn't find much luck in locating
    a solution. Here the bit of code that I'm working with:
    >
    strMyValue = "";
    >
    for (double d = double.MinValue ; d < double.MaxValue && numPoints <
    maxPoints; d+=1.1F)
    {
    strMyValue = d.ToString();
    //Here I hand off the string to be used by another object
    }
    >
    The strangeness that I get comes in the conversion d's value to a
    string. In the debug window, I see this value for d:
    >
    -1.7976931348623 157E+308
    >
    After I call d.ToString(), I receive this value for strMyValue:
    >
    "-1.7976931348623 2E+308"
    >
    Somehow the ToString() function managed to round the last few digits
    (157 becomes a 2). Normally, this wouldn't be an issue, but since I'm
    on the edge of the minimum value for a double, this rounding causes the
    object that I had this off to to fail. The way I see it (and I know
    these are two seperate things but hang with me), if the debug window
    can display a textual representation of d's value, then the ToString()
    function should be able to do as good a job.
    >
    Why did I ever create this snippet? I am testing a COM object to
    determine if the object is able to successfully coerce strings back
    into their repective data types. Of course, since the number I'm
    representing is now outside the range of a valid double due to the
    rounding issue, the coercion fails.
    >
    Has anyone else had trouble with the ToString() method? I've tested
    some of the other types and, to this point, only doubles have given me
    any hassle.
    I can't say what the debugger might be doing differently when
    displaying the values, but the problem is most likely not with
    ToString. Some values cannot be represented exactly in binary. See
    this link:



    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Possible ToString() bug

      <gbgeek@gmail.c omwrote:

      <snip>
      The way I see it (and I know
      these are two seperate things but hang with me), if the debug window
      can display a textual representation of d's value, then the ToString()
      function should be able to do as good a job.
      You're not telling ToString what format to use, so it's using the
      default format. If you use "r" (round-trip) format you'll see the same
      thing as the debugger showed.

      In fact, the actual exact value here is

      -179769313486231 570814527423731 704356798070567 525844996598917 4768031572
      607800285387605 895586327668781 715404589535143 824642343213268 89464182768
      467546703537516 986049910576551 282076245490090 389328944075868 50845513394
      230458323690322 294816580855933 212334827479782 620414472316873 81771809192
      998812504040261 84124858368

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • gbgeek

        #4
        Re: Possible ToString() bug

        Doh! I'll certainly give that a try. It never crossed my mind to send
        down a format.

        Thanks so much!

        On Nov 8, 5:15 pm, Jon Skeet [C# MVP] <s...@pobox.com wrote:
        <gbg...@gmail.c omwrote:<snip>
        >
        The way I see it (and I know
        these are two seperate things but hang with me), if the debug window
        can display a textual representation of d's value, then the ToString()
        function should be able to do as good a job.You're not telling ToString what format to use, so it's using the
        default format. If you use "r" (round-trip) format you'll see the same
        thing as the debugger showed.
        >
        In fact, the actual exact value here is
        >
        -179769313486231 570814527423731 704356798070567 525844996598917 4768031572
        607800285387605 895586327668781 715404589535143 824642343213268 89464182768
        467546703537516 986049910576551 282076245490090 389328944075868 50845513394
        230458323690322 294816580855933 212334827479782 620414472316873 81771809192
        998812504040261 84124858368
        >
        --
        Jon Skeet - <s...@pobox.com >http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        Working...