473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

Nov 8 '06 #1
3 1153
gb****@gmail.com 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.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

Nov 8 '06 #2
<gb****@gmail.comwrote:

<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

-17976931348623157081452742373170435679807056752584 49965989174768031572
60780028538760589558632766878171540458953514382464 234321326889464182768
46754670353751698604991057655128207624549009038932 894407586850845513394
23045832369032229481658085593321233482747978262041 447231687381771809192
99881250404026184124858368

--
Jon Skeet - <sk***@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
Nov 8 '06 #3
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.comwrote:
<gbg...@gmail.comwrote:<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

-17976931348623157081452742373170435679807056752584 49965989174768031572
60780028538760589558632766878171540458953514382464 234321326889464182768
46754670353751698604991057655128207624549009038932 894407586850845513394
23045832369032229481658085593321233482747978262041 447231687381771809192
99881250404026184124858368

--
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
Nov 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ronald Fischer | last post by:
Assume the following JavaScript function: function bracketize(s) { return ''; } This function which doesn't assume anything about its argument except that it must be convertible to a string.
3
by: SRLoka | last post by:
I was getting a "Specified cast is not valid" error when sending to a MSMQ. This happens very occasionally and so cannot reproduce to debug. So I modified my function as shown below. sb is a...
11
by: Gene | last post by:
I want to create a certain number of arrays on the same time to use, is it possible to do that?
13
by: Alison Givens | last post by:
....... that nobody knows the answer. I can't imagine that I am the only one that uses parameters in CR. So, my question again: I have the following problem. (VB.NET 2003 with CR) I have a...
2
by: Chakravarthy | last post by:
Given an XML stream as below, is there any possibility to convert the same into any treditional class style with . (dots) as seperator of the nodes of the xml file... for instance ...
4
by: Pritcham | last post by:
Hi all I've got a number of classes already developed (basic entity classes) like the following: Public Class Contact Private _firstname as String Private _age as Integer Public Property...
9
by: Zytan | last post by:
"A static member 'function' cannot be marked as override, virtual or abstract" Is it possible to make a static class member function (which is also static, obviously) that is an override to...
3
by: | last post by:
I am enjoying making generalized methods to serve common needs, such as: ImageProcessing.MakeQuickResize(); ImageProcessing.Sharpen(); FileOps.CreateYearAndMonthAndDayDirectoryBasedOnDate()...
0
by: Jon Harrop | last post by:
xahlee@gmail.com wrote: This does not even start running in Mathematica 6, let alone produce any correct results. The first line was deprecated some time ago. I don't know what is wrong with the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.