Re: Casting double to int produces inconsistent results from VS 2003
to VS 2008
On 28 Mar., 13:40, KD <keith.degr...@gmail.com> wrote:
On Mar 27, 10:56 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
KD wrote:
I am porting some legacy code from VS 2003 to VS 2008, and I've
encountered a problem. Here is an example to illustrate:
{
double f = 10000000000.0;
int d = (int) (f);
}
When I run from VS2003, I get:
d = 1410065408
When I run from VS2008, I get:
d = -2147483648
Can someone please explain why d2 is different in VS 2008?
The value of ten billion cannot be stored in an int. The Standard sa=
ys
the behavior of the program in such a case is undefined. It was
undefined in 2003, it's still undefined in 2008. If the compiler
creators decide to define it somehow, they haven't documented it. At
least I've not seen anything particular. If you want to port undefin=
ed
behavior, you're on your own. If you want advice what to do now, I
would probably try to refactor this to remove undefined behavior.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you, Victor. I am fishing for information about how MSVC in
particular handles it. I am hoping that they documented this
somewhere, along with a note about how they decided to handle it
differently somewhere between VS2003 and VS2008.
And thanks for the advice about refactoring. Unfortunately, like I
mentioned, this is legacy code which is actually very stable even with
its undefined behavior dependent code.
You are contradicting yourself: apparently, the code is not so stable:
if it were, it would still be working.
The least amount of changes I
have to make to this code, the better. I know that borders on
sacrilege for some people, but this is the real world for me. The
last thing I want to do is go on a refactoring bender and introduce a
whole new set of bugs. Anyway, that's a whole other discussion...
I really do not believe it should be such a big deal: apparently the
program had some specific expectations when converting huge numbers
from double to int. Replace those places with a function doing the
right thing and you're done.
/Peter