S
Stu Smith
A feature we'd like to see in VS 2004 is a compiler warning for equality
comparisons on floating values. Why? Because the behaviour allowed under the
ECMA spec is somewhat suprising. (Well it suprised us).
If you run the program below in debug mode (F5), it prints True. If you run
it in non-debug mode (Ctrl-F5), you get False.
We had a look at the ECMA spec (CLI - 12.1.3 line 35) and this behaviour
seems perfectly acceptable.
"Storage locations for floating point numbers (statics, array elements, and
fields of classes) are of fixed size. The supported storage sizes
are float32 and float64. Everywhere else (on the evaluation stack, as
arguments, as return types, and as local variables) floating point numbers
are represented using an internal floating-point type. In each such
instance, the nominal type of the variable or expression is either R4 or
R8, but its value may be represented internally with additional range
and/or precision. The size of the internal floating-point representation
is implementation-dependent, may vary, and shall have precision at least as
great as that of the variable or expression being represented."
A compiler warning for our admitted error would probably be a useful thing.
using System;
namespace FloatWeirdness
{
class Floaty
{
public Floaty( float a, float b )
{
_a = a;
_b = b;
}
public float Total
{
get
{
return _a + _b;
}
}
float _a, _b;
}
class Class1
{
static float storedValue;
[STAThread]
static void Main(string[] args)
{
Floaty floaty = new Floaty( 145.347519f, 6.878819f );
storedValue = floaty.Total;
Console.WriteLine( floaty.Total == storedValue );
Console.Read();
}
}
}
(NB According to 12.1.3 line 8 even storing the value in the static isn't
guaranteed to change the representation, but on our X86 VS.NET 2003, the
demo shows the problem).
Any thoughts?
Stu & Lee
comparisons on floating values. Why? Because the behaviour allowed under the
ECMA spec is somewhat suprising. (Well it suprised us).
If you run the program below in debug mode (F5), it prints True. If you run
it in non-debug mode (Ctrl-F5), you get False.
We had a look at the ECMA spec (CLI - 12.1.3 line 35) and this behaviour
seems perfectly acceptable.
"Storage locations for floating point numbers (statics, array elements, and
fields of classes) are of fixed size. The supported storage sizes
are float32 and float64. Everywhere else (on the evaluation stack, as
arguments, as return types, and as local variables) floating point numbers
are represented using an internal floating-point type. In each such
instance, the nominal type of the variable or expression is either R4 or
R8, but its value may be represented internally with additional range
and/or precision. The size of the internal floating-point representation
is implementation-dependent, may vary, and shall have precision at least as
great as that of the variable or expression being represented."
A compiler warning for our admitted error would probably be a useful thing.
using System;
namespace FloatWeirdness
{
class Floaty
{
public Floaty( float a, float b )
{
_a = a;
_b = b;
}
public float Total
{
get
{
return _a + _b;
}
}
float _a, _b;
}
class Class1
{
static float storedValue;
[STAThread]
static void Main(string[] args)
{
Floaty floaty = new Floaty( 145.347519f, 6.878819f );
storedValue = floaty.Total;
Console.WriteLine( floaty.Total == storedValue );
Console.Read();
}
}
}
(NB According to 12.1.3 line 8 even storing the value in the static isn't
guaranteed to change the representation, but on our X86 VS.NET 2003, the
demo shows the problem).
Any thoughts?
Stu & Lee