N
Nicolas Blais
Hi,
I have this following class which I use as a timer:
#include <sys/time.h>
using namespace std;
class chrono {
public:
chrono() {};
~chrono() {};
int start();
float freeze();
private:
struct timeval before_time, after_time;
struct timeval sub_chrono;
};
int chrono::start() {
return gettimeofday(&before_time, 0);
}
float chrono::freeze() {
gettimeofday(&after_time, 0);
timersub(&after_time, &before_time, &sub_chrono);
// Possible clean up required here: Remove (float) cast.
return (sub_chrono.tv_sec + (float) sub_chrono.tv_usec / 1000000);
}
Can anyone tell me if it's possible to remove the (float) cast in freeze()'s
return? I can't seem to figure out why it will return a long (timersub
gives long, that I get) without the cast when the function is a float.
Shouldn't a long divided by 1000000 give a float without the need for a
cast?
Thanks,
Nicolas.
I have this following class which I use as a timer:
#include <sys/time.h>
using namespace std;
class chrono {
public:
chrono() {};
~chrono() {};
int start();
float freeze();
private:
struct timeval before_time, after_time;
struct timeval sub_chrono;
};
int chrono::start() {
return gettimeofday(&before_time, 0);
}
float chrono::freeze() {
gettimeofday(&after_time, 0);
timersub(&after_time, &before_time, &sub_chrono);
// Possible clean up required here: Remove (float) cast.
return (sub_chrono.tv_sec + (float) sub_chrono.tv_usec / 1000000);
}
Can anyone tell me if it's possible to remove the (float) cast in freeze()'s
return? I can't seem to figure out why it will return a long (timersub
gives long, that I get) without the cast when the function is a float.
Shouldn't a long divided by 1000000 give a float without the need for a
cast?
Thanks,
Nicolas.