товіаѕ
товіаѕ

Reputation: 3244

Why can't I assign a double the same literal value of Double.Min and Max?

Decompiling the source and looking up Double.Min and Double.Max shows the following definition:

public const double MinValue = -1.79769313486232E+308;
public const double MaxValue = 1.79769313486232E+308;

This matches up with the msdn page.

If I try to assign this maximal value manually to a variable I get the following error:

Floating-point constant is outside the range of type 'double'
double d1 = -1.79769313486232E+308; // DOESN'T COMPILE
Double d2 = 1.79769313486232E+308; // DOESN'T COMPILE

Can someone explain to me why this is? Is there something wrong with the double boundary validation?

Upvotes: 4

Views: 506

Answers (1)

usr
usr

Reputation: 171188

This is a known .NET Framework and .NET Core bug. Since the Roslyn compiler likely just uses the .NET Framework parsing code the compiler invalidly rejects this double value.

harold has pointed out that the value is rounded. But if you round it down then it still does not work.

double.Parse("1.79769313486231E+308")

throws an OverflowException.

This GitHub issue links to many other floating point parsing bugs. It was surprising to me to see how broken this fundamental framework feature is.

Upvotes: 4

Related Questions