A colleague and I were testing how
well Flash MX 2004 and .NET work together through web services. There are a few
issues with Int64 and Decimal, which are quite worrying, but what really
surprised us was an issue with Double. Sending Double.MaxValue was resulting in
Infinity being received by Flash.
Now
Flash has only one numeric data type called Number and according to the
documentation its precision is 1.79769313486232E+308. This translates to
1.79769313486232 * (10 ^ 308), ‘^’ meaning ‘to the power of’. The reason we
found this surprising is because according to the documentation Double.MaxValue
is exactly the same number, 1.79769313486232E+308, so everything should have
worked correctly.
What
we forgot was that a Double (as well as Single) have inexact precision. This
means that for really large numbers their decimal representation becomes
approximate. You can quickly verify this by trying the following code:
public double GetDouble() {
return
1.79769313486232e308;
}
This
won’t build and will return the following error message, “Floating-point
constant is outside the range of type 'double'”. This is happening because what
gets printed out when you use Double.MaxValue and what is printed in the
documentation is the approximate maximum value of double. You can even try
public double GetDouble() {
return
Double.Parse(Double.MaxValue.ToString());
}
and it won’t work. The real maximum
value of Double is 1.7976931348623157E+308. Just try the following
Console.WriteLine(1.7976931348623157e308
== Double.MaxValue);
and you will see that
it returns true.