[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

SteveB

10/4/2002 1:37:00 AM

hello:
i did the following:
string s = double.MaxValue.ToString()
try
{
double d = double.parse(s, NumberStyles.Any)
}
catch
{
;
}
///
the above failed to convert back to a double. the
exception essially stated that the number is to large for
the lvalue.
I thought conversion of MinValue & MaxValue to string and
back to double should work.

Thanks in advance...
1 Answer

NETMaster

10/4/2002 9:37:00 AM

0

> I thought conversion of MinValue & MaxValue to string and
> back to double should work.


In theory yes, but:
- 'Double' stores values as !binary! floating point numbers,
- the conversion ToString does a !conversion! to decimal.

Now, there is NO exact/precise 1:1 representation
of decimal and binary/floating point numbers.

This is (simplified) comparable to the problem you have to write
2/3 in decimal 0.666667 or 0.66666666666_...
You have to do either some rounding or limit the number of digits.

Please try for Double.MaxValue as string:
double dex = double.Parse( "1.79769313486232E+308" );
=> exception as you know ...

and now, do change the lowest digit 2 => 1
double dok = double.Parse( "1.79769313486231E+308" );
=> it works!

Thus, the decimal number
"1.79769313486232E+308"
was rounded up and is larger then Double.MaxValue!

All this is according the IEEE-754 standard on most OS and CPUs:
http://support.microsoft.com/default.aspx?scid=kb;en-...
http://support.microsoft.com/default.aspx?scid=kb;en...


Conclusion:
never use 'Double.MaxValue' as a string for
ANY calculations / conversions / comparisons.


P.S.
also try:
double dok = double.Parse( "1.79769313486231580999999999999999E+308" );
double dex = double.Parse( "1.79769313486231581E+308" );
'dok' is currently the largest number you can parse.
(likely to be implementation depending!)


--
NETMaster (Thomas Scheidegger)
http://www.cetus-links.org/oo_c...