PC Review


Reply
Thread Tools Rate Thread

Double.MaxValue Casting Problems

 
 
=?Utf-8?B?S2V2aW4gQmxha2VsZXk=?=
Guest
Posts: n/a
 
      15th Feb 2007
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
 
 
 
 
Laura T.
Guest
Posts: n/a
 
      15th Feb 2007
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" <(E-Mail Removed)> ha scritto nel
messaggio news:13280E1B-CDAC-4939-B829-(E-Mail Removed)...
>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
 
Peter Bromley
Guest
Posts: n/a
 
      15th Feb 2007
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
 
=?Utf-8?B?S2V2aW4gQmxha2VsZXk=?=
Guest
Posts: n/a
 
      15th Feb 2007
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:

> 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
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reflection - Difference between DateTime.MaxValue andDecimal.MaxValue Joey Fontaine Microsoft Dot NET 2 4th Dec 2008 02:12 PM
[Reflection] Difference between DateTime.MaxValue andDecimal.MaxValue Property Data Joey Fontaine Microsoft Dot NET Framework 0 9th Oct 2008 07:50 PM
double.Parse(double.MaxValue.ToString()) yields an Exception =?Utf-8?B?TWFya3VzIEtsaW5n?= Microsoft C# .NET 5 11th Nov 2005 08:36 AM
Casting double to int Barry Microsoft Dot NET Framework 4 9th Feb 2005 09:29 PM
double.Parse does not work correctly with MinValue and MaxValue =?Utf-8?B?VGF5bG9yTWljaGFlbEw=?= Microsoft Dot NET Framework 0 4th Oct 2004 09:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:32 PM.