c++ - I am getting a negative value when I try to convert a uint64_t to struct timeval? //edited -
#include <iostream> #include <sys/time.h> #include <stdint.h> void convert(uint64_t offset ) { struct timeval t; t.tv_sec = offset / 1000000; std::cout << "currenttimeoffset " << offset << "starttimeoffset " << t.tv_sec << std::endl; t.tv_usec = offset % 1000000; std::cout << "stattime usec " << t.tv_usec << std::endl ; } int main(int argc , char** argv) { uint64_t t = 18446744073709551615; convert(t ); return 0; } is there rounding error ? how accomplish ? routine called elsewhere code in convert. wrote small script example of uint64_t giving me negative number
offset / 1000000 produces value of 1.8446744073709551615 × 10^13 large tv_sec of type int32. max value can stored in int32 2.147483647 × 10^9.
you're overflowing integer you're storing result in, , wrapping around , becoming negative.
Comments
Post a Comment