Rob Smyth

Tuesday 26 August 2008

Cannot Parse System.Double.MaxValue

Surpisingly it seems that System.Double does not parse maximum and minimum values. I've found that double.MaxValue cannot be parsed by double.Parse(MaxValue) nor via the Convert method overload. System.Double.NaN can be passed though.

Here is a test fixture demonstrating the problem:

[TestFixture]
public class DoubleTests
{
[Test]
[ExpectedException(typeof(OverflowException))]
public void MaxValue_ThrowsException_WhenParsing()
{
double readValue = double.Parse(double.MaxValue.ToString());
Assert.AreEqual(double.MaxValue, readValue);
}

[Test]
[ExpectedException(typeof(OverflowException))]
public void MinValue_ThrowsException_WhenParsing()
{
double readValue = double.Parse(double.MinValue.ToString());
Assert.AreEqual(double.MinValue, readValue);
}

[Test]
public void NanValue_ThrowsException_WhenParsing()
{
double readValue = double.Parse(double.NaN.ToString());
Assert.AreEqual(double.NaN, readValue);
}
}

No comments: