tags:

views:

104

answers:

2

I'm trying to divide a timeval by an integer. Here's what I've got so far:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
     struct timeval my_time;
     struct timeval my_time_quotient;
     int i;

     gettimeofday(&my_time, NULL);
     i = 5;

     my_time_quotient = my_time / i;

     printf("%d secs, %d msecs\n", (int)my_time_quotient.tv_sec, (int)my_time_quotient.tv_usec);

     return 0;
}

When I compile I get:

jen@ubuntu:~/$ gcc -g -otimespike timespike.c
timespike.c: In function ‘main’:
timespike.c:15: error: invalid operands to binary / (have ‘struct timeval’ and ‘int’)

What's the correct way to find the quotient?

+3  A: 

You cannot directly divide a struct by a numeric type, even when the struct represents some kind of number.

You will have to first convert your struct to an integer type representing the number of microseconds. Because the elements are of type int, you will need to find a numeric type with almost twice as many bits as an int. Hopefully your compiler supports long long int or __int64, both of which are 64-bits.

After converting the contents of the struct to a numeric type, the division proceeds directly as with any division.

After performing the division, you will need to store the result back to the timeval, probably making use of the "remainder after division" or "modulus" operator, x % y.

Heath Hunnicutt
+1  A: 

As this may be a homework question, I won't give you any code, but I'll give you the explanation as to why it's not working, and the approach to use to make it work.

When you're working with plain old data types (integers, doubles), you can divide. However, a timeval is not a plain old data type - it's a combination of two in a struct. timevals structs don't know how to be divided by integers (that's what it means when it says invalid operands to binary / (have ‘struct timeval’ and ‘int’)).

So you have to do this manually - divide the number of seconds (tv_sec), and then divide the number of nanoseconds (tv_usec), and insert the results of these divisions back into your my_time_quotient timeval. Handling fractions of seconds is left as an exercise to the reader - but the approach suggested by Heath is one approach.

Smashery
This won't quite work as stated -- the result of dividing the seconds would need to retain any fractional result (i.e., complicated mathematics either floating- or fixed-point fractionals), accumulate the fractions of a second into the microseconds counter, detect overflow of microseconds and carry the increment into seconds counter...
Heath Hunnicutt
I direct your attention to the note "Handling fractions of seconds is left as an exercise to the reader - but the approach suggested by Heath is one approach." I'm not claiming you'd divide the long int by the divisor to get the number of seconds. I'm claiming that you need to handle the division yourself in whatever method, yours being a correct approach.
Smashery
I'm sorry, I missed that note about left to the reader. ;)
Heath Hunnicutt