Possible ToString() bug

G

gbgeek

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.7976931348623157E+308

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

"-1.79769313486232E+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
 
J

Jon Skeet [C# MVP]

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

-1797693134862315708145274237317043567980705675258449965989174768031572
60780028538760589558632766878171540458953514382464234321326889464182768
46754670353751698604991057655128207624549009038932894407586850845513394
23045832369032229481658085593321233482747978262041447231687381771809192
99881250404026184124858368
 
C

Chris Dunaway

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.7976931348623157E+308

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

"-1.79769313486232E+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:

http://www.yoda.arachsys.com/csharp/floatingpoint.html
 
G

gbgeek

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

Thanks so much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top