【发布时间】:2013-07-29 14:08:20
【问题描述】:
我正在尝试通过执行一系列计算来执行计算,以从当前时间的总微秒返回到 HHMMSSFFF 格式的当前时间。但是,当我尝试从总微秒数中减去 hours*3600000000 时,返回的值不正确(它偏离了一个数量级,并且数字本身是错误的。有谁知道如何解决这个问题?我试过了使用 long long int 和 long double 但它们都输出了相同的值。我复制了下面的代码和控制台中的结果输出。
当数据进入时,时间存储在一个向量中(也就是一个时间戳),这就是为什么有一个 temp_counter。我在 boost::posix_time 库中使用 time_duration。
long double total = cur_time.at(temp_counter).total_microseconds();
cout << total << endl;
int hours = total/(3600000000);
cout << hours << endl;
long long int temp = hours*3600000000;
cout << std::setprecision(20) << temp << endl;
total = total - temp;
cout << total << endl;
输出:
35465976558
9
2335228928
33130747630
根据我的计算,temp 实际上应该是 32400000000,新的总数应该是 3065976558。
【问题讨论】:
-
在哪个操作系统上?
-
我相信 hours*3600000000 会导致溢出。 long long int 的大小是多少?即使 64 位可能还不够。即使它足够大,您也需要将小时数转换为它,而不是用作 int。
-
Linux,确切地说是 Ubuntu
-
试试
hours*3600000000LL。 -
那么我可以使用不会导致溢出的最大值是多少?因为我知道 long long in 可以容纳高达 2^64 的值
标签: c++ eclipse boost int long-integer