Jump to content
  • Advertisement
Sign in to follow this  

A function that works well in one project but fails in another, why ?

This topic is 4411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The function converts double64 to int32, using magic number trick:
#define  magic 6755399441055744.0

inline int RoundToInt64 (double dval)
{
    dval += magic;
    return *(int*)&dval;
}
I have tested it with a number of data, and it works. But recently when I used it in one project, it failed. I tried to set some values to the low 32 bits of dval:
dval = magic;
int* pint=(int*)&dval;
*pint |= 0x000000ff;
and found that after "dval += magic", *pint==0 ! It's too strange ! The compiler that I used was VC++ 2003. I doubted the problem was caused by some compiler options, but I failed to find that. I am almost crazy now ! Pleast help me out.

Share this post


Link to post
Share on other sites
Advertisement
You were saying that it works in one project but fails in another?

Could you post the relevant code for each project. Try looking at the compiler options for floating point generation for each of your projects. There is (Precise, Fast, and one other I think)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yes. The relevant code are exactly same in the two projects (The assembly are same, too). One works but another fails.

I have checked the compiler options also, and found little options which concern with floating point, and none of them looks suspicious.

The code is compiled in debug mode, no optimizations.

By the way, I'm not a native English speaker. I hope I have made my words clear enough :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!