HighTechTalks DotNet Forums  

Double.MaxValue Casting Problems

Dotnet Framework microsoft.public.dotnet.framework


Discuss Double.MaxValue Casting Problems in the Dotnet Framework forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Kevin Blakeley
 
Posts: n/a

Default Double.MaxValue Casting Problems - 02-15-2007 , 11:26 AM






I have some code that takes Double.MaxValue and converts it to a string, but
when I try to convert it back to a double, it fails. Shouldn't the value be
able to be converted back to a double without error?

Here is a simple code sample that demonstrates the problem:

try
{
Double dbl = double.MinValue;
//String sdbl = dbl.ToString(); // This errors as well
String str = dbl.ToString("N");

// This throws "Value was either too large or too small
for a Double."
Double dbl2 = Convert.ToDouble(str);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message);
}

Reply With Quote
  #2  
Old   
Laura T.
 
Posts: n/a

Default Re: Double.MaxValue Casting Problems - 02-15-2007 , 11:58 AM






Floating point operations are always approximative, conversions of them to
string even worse.
If you need to convert, it might be better to check the inherent limits
(Min/Max/Infinity etc) and handle them as special case, like:

Double dbl2;

if(str!=double.MinValue.ToString())
dbl2 = Convert.ToDouble(str);
else
dbl2=double.MinValue


"Kevin Blakeley" <KevinBlakeley (AT) discussions (DOT) microsoft.com> ha scritto nel
messaggio news:13280E1B-CDAC-4939-B829-B9FA2CADB7D8 (AT) microsoft (DOT) com...
Quote:
I have some code that takes Double.MaxValue and converts it to a string,
but
when I try to convert it back to a double, it fails. Shouldn't the value
be
able to be converted back to a double without error?

Here is a simple code sample that demonstrates the problem:

try
{
Double dbl = double.MinValue;
//String sdbl = dbl.ToString(); // This errors as well
String str = dbl.ToString("N");

// This throws "Value was either too large or too small
for a Double."
Double dbl2 = Convert.ToDouble(str);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message);
}



Reply With Quote
  #3  
Old   
Peter Bromley
 
Posts: n/a

Default Re: Double.MaxValue Casting Problems - 02-15-2007 , 04:53 PM



Kevin Blakeley wrote:
Quote:
I have some code that takes Double.MaxValue and converts it to a string, but
when I try to convert it back to a double, it fails. Shouldn't the value be
able to be converted back to a double without error?

Here is a simple code sample that demonstrates the problem:

try
{
Double dbl = double.MinValue;
//String sdbl = dbl.ToString(); // This errors as well
String str = dbl.ToString("N");

// This throws "Value was either too large or too small
for a Double."
Double dbl2 = Convert.ToDouble(str);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message);
}
Perhaps you should try the round-trip format string instead

String str = dbl.ToString("R");


Peter


Reply With Quote
  #4  
Old   
Kevin Blakeley
 
Posts: n/a

Default Re: Double.MaxValue Casting Problems - 02-15-2007 , 05:01 PM



Thanks! That worked like a champ. I tried a few of the fomatting types but
did not try that one. I even tested to see if my value was equal to
Double.Minvalue and it evaluated to true.

Thanks again.

"Peter Bromley" wrote:

Quote:
Kevin Blakeley wrote:
I have some code that takes Double.MaxValue and converts it to a string, but
when I try to convert it back to a double, it fails. Shouldn't the value be
able to be converted back to a double without error?

Here is a simple code sample that demonstrates the problem:

try
{
Double dbl = double.MinValue;
//String sdbl = dbl.ToString(); // This errors as well
String str = dbl.ToString("N");

// This throws "Value was either too large or too small
for a Double."
Double dbl2 = Convert.ToDouble(str);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message);
}
Perhaps you should try the round-trip format string instead

String str = dbl.ToString("R");


Peter


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.