Hi Tom,
> I have the following instruction in C++.NET :
>
[quoted text clipped - 3 lines]
> where x, h, n are of type double, and x receives the value NaN
> (because the value of number is rather small).
The conversion from double to int will be compiled to a 'conv.i4' IL
instruction.
> This results in y having value of the largest negative 32-bit integer.
>
> What does the standard specify in this instance?
> Should an exception be thrown?
> Will the sign of Y always be known?
The ECMA spec (partition III, section 3.27) has this to say about conv.i4:
- "Conversion from floating-point numbers to integral values truncates
the number toward zero."
- "If overflow occurs converting a floating-point type to an integer the
value returned is unspecified."
- "No exceptions are ever thrown."
> Right now the workaround is to test the value of n, to make sure that it is
> within the range of the log10 function else force a value to y.
There is a 'conv.ovf.i4' IL instruction that will throw an
OverflowException if the conversion fails.
In C# you can ask the compiler for this behaviour by doing:
int y = checked((int)(x * h));
I don't know how to use checked arithmetic and conversion operations in
managed C++, perhaps somebody else has that answer?
Best Regards
Mike
Tom - 15 Jun 2005 01:15 GMT
Hi, Mike.
Could not find the 'checked' modifier in C++.NET, but
I did find the method: static bool Double::IsNAN(double)
so I can test for that. Thanks for your help.
-- Tom
> Hi Tom,
>
[quoted text clipped - 42 lines]
>
> Mike