Is there a way to divide two floating point numbers that were cast from an unsigned int that does a negative float result?

unsigned int deltaTime = now() - last();
unsigned int freq = getFreq();
float timeOfGap = float(deltaTime) / float(freq);

ASSERT(0.0f <= timeOfGap, "WHAT?");

I got an excellent result from my program. I did not catch the scene that happened. But I have a magazine that says it’s really raised at least a few times.

Is there a possible way of dividing two floating point numbers that were cast from an unsigned int that makes the float negative?


Ok, thanks guys. I am glad to see your attention. Let me try to add some descriptions. It will be useful for you.

I am working on an online game, and this situation rarely occurred, but often when I played hundreds of millions of times. I cannot debug or catch the exact snapshot, but it is written to the db error messages.

TimeOfGap should be NaN, as you noticed, but the log did not return a value exactly. TimeOfGap accumulates as shown below:

mTotalFrameRateTimeAsASecond += timeOfGap;

And this program registers mTotalFrameRateTimeAsASecond, which is a negative value. But besides the fact that this attribute of value was negative, it is the correct value. Meaning, this is a point in time, as well, if I share the final value with the frame rate. The hypothetical playback time was 500 seconds, it was -30000 (with a frequency of 60 frames per second).

+3
source share
4 answers

Perhaps this is not negative, but NAN. 0.0f <= NAN- false. This can happen if both operands are equal to zero.

+3
source

nan . , :

float f = 0.0 / 0.0;

assert(f > 0 || f <= 0);

!

0

:

unsigned int deltaTime = now() - last();
unsigned int freq = getFreq();
float timeOfGap = float(deltaTime) / float(freq);
char s[50] ;
sprintf (s, "WHAT? %u/%u < 0!", deltaTime, freq);

ASSERT(0.0f <= timeOfGap, s);
0

You can do a simple test to check for something other than unsigned int for float.

Try the following:

unsigned int toto = 0xFFFFFFFF; 
float convertedToto = float(toto);

convertedTotowill be -1 if the conversion (unsigned int → float) is actually (int → float). You should get 2 * 10e31 with the correct conversion.

-1
source

All Articles