开发者

Dividing unsigned long long

Currently attempting开发者_如何转开发 to divide an unsigned long long by a static value.

The unsigned long long contains the number of microseconds it took for an operation to complete. I want to take this value and convert it into seconds.

Here is the code snippit:

double udDiffTime = ullHighResDiffTime / (unsigned long long)1000000;

In one case with my debugger, I can see ullHighResDiffTime = 639. Therefore, udDiffTime = 0.000639. However, I'm getting udDiffTime = 0.

I'm sure I'm making a mistake somewhere. I've tried using 1000000LL instead of casting it using (unsigned long long), but there is no difference.


You're doing an integer division, thus if the result is 0.000639 it will get truncated to 0.

If you want a floating-point result, you'll have to use at least one floating-point operand. Try for instance to change (unsigned long long) 1000000 to 1000000.0.

An unsigned long long can range up to 18,446,744,073,709,551,615. A double can reach up to 1.7E308 (i.e., a one with 308 zeros). The catch is that the higher the value gets, the less precision it will have, so what you need to ask yourself is if the large values really need to be that precise, or if it's more important which magnitude the number has.


Change it to

double udDiffTime = ullHighResDiffTime / 1000000.0;

to get the result you expect.


If the type of ullHighResDiffTime is unsigned long, then you are dividing an integer by and integer which won't invoke floating point arithmetic.

Use this instead:

double udDiffTime = ullHighResDiffTime / 1000000.0;


unsigned long long is an integral type, so integral division is used. Only after the division is the value converted to a double. Try dividing by 1000000.0.


Dividing unsigned long long (or any integer) with the same type results in the same type. Then you convert the value to double, but the result is already zero.

Divide it with a double.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜