Division by a long long variable

Hi:

I need to divide an integer by a long long variable (mslong in the following code), however it always produces zero as the result (ss will be out as zero). How can I tackle this problem?

int xm = 100;
int ym = 50;
int dist_m = sqrt(xm*xm + ym*ym);

struct timeval tp;
gettimeofday(&tp, NULL);
long long mslong = (long long) tp.tv_sec * 1000L + tp.tv_usec / 1000;

float ss = dist_m/mslong;
cout <<ss<<endl;
Do you realize that integers have no fractions, something like 1/3 will yield zero.

Like jlb said, integer division leaves no decimal/fractional portion left. Only the integer portion of the result remains.

If you want to get around this, use double or float.

float ss = dist_m/mslong; could be changed to float ss = dist_m/(mslong * 1.0);

Example:
1
2
std::cout << "1/3:   " << (1/3) << std::endl;
std::cout << "1/3.0: " << (1/3.0) << std::endl;

produces the following output:
1/3:   0
1/3.0: 0.333333
Press any key to continue . . .
Topic archived. No new replies allowed.