double to int impossible

Started by
16 comments, last by Thaligar 16 years ago
ok, now i'm getting really crazy, i'm using msvc 2003 net, until now i never had a problem with a double to int conversion, but now it doesn't work anymore. my code looks like that (i know it have to work but it doesn't)

double dValue = x.GetSize(); // returns the size of the object
int nRet = dValue;

during debugging the assignment from GetSize to dValue works, but nRet gets to 1223296 when the size is 1.907, i also tried to cast, but still doesn't work. the other way around it works. very interesting is the fact that even the double has the same value the int changes the assigned value over time. is there any reason, or shall i try something else?? thanks in advance, tgar
Advertisement
Are you debugging a release build? Are you sure you don't have a buffer overrun or similar error that causes a non-local change to the variable?
no i'm debugging a debug build, all other variables are ok,
i also tried now this one

double dV = 3.14;int nG = dV;


same error, i also restarted my system,
i checked now also these value and got also pointer to the values,
now the really interesting part,

the debugger says -> dV = 3.14
the memory location says -> dV = 1.26+011
so both are VERY different from 1223856

if you have any suggestions, maybe msvc has several bugs...

tgar
It's much more likely that your code has bugs. Try posting a minimal, but complete code sample that demonstrates your problem.
ok,

std::vector<int> Compute(double dMinSize, double dMaxSize, double dStability, int nStepSize, bool bNeg){//LOG(dMinSize);double dV = 3.14;double pD = &dV;int nG = dV;int * pN = &nG;std::vector<int> vRet;//vRet.push_back( ... ); // here i wanted to compute something where the return value is a double, and then i add it to the int vectorvRet.push_back( 0 );return ( vRet );};


the rest is commented, not even this part works,

i tried this code snippet in another program where it worked.

i will try the code in the caller function,
i'll also try to recompile the whole program and then i'll restart my computer, this will take some time but hopefully will work...

greets,
tgar
Did you copy and paste this code exactly? Because this...

double pD = &dV


should not work. In fact, this shouldn't even compile.
I believe it will compile in C. If the source file is named *.c instead of *.cpp, I believe MSVC++ will compile it as C, and thus allow meaningless conversions as such, with no warnings. (Thank God C++ fixed those issues!)
Jason Doucette / Xona.comDuality: ZF — Xbox 360 classic arcade shmup featuring Dual Play
Quote:Original post by Jason Doucette
I believe it will compile in C.

C doesn't have std::vector, or even the components required to make it (namespaces, classes, templates).

Thaligar: Make copy and paste your new best friends when it comes to posting code to forums online. "Transcribing" code like this leaves you with fake code that's worse than useless -- it tends to cover up the actual problems you're facing, and worse, generate new problems, causing wild goose chases.
I was just referring to that single line of code, which may compile in C on it's own. But, maybe not, since they might have to be the same size in order for it to be legal.

But, you're right, the rest of the C++ stuff would make it impossible to compile in C.
Jason Doucette / Xona.comDuality: ZF — Xbox 360 classic arcade shmup featuring Dual Play
&dV is just an int that is the address of dV. So you are just assigning the address and not the value.

theTroll

This topic is closed to new replies.

Advertisement