Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dividing unsigned long long

Tags:

c++

casting

math

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.

like image 648
BSchlinker Avatar asked Dec 28 '22 18:12

BSchlinker


1 Answers

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.

like image 163
aioobe Avatar answered Jan 10 '23 15:01

aioobe