我试图通过同步时间与服务器来修改第二个(long unsigned n_ticks_per_second)的定义来改进SWRTC。在uint32_t计算中转换为float float
#include <stdint.h>
#include <stdio.h>
int main(int argc, char * argv[]){
int32_t total_drift_SEC;
int32_t drift_per_sec_TICK;
uint32_t at_update_posix_time = 1491265740;
uint32_t posix_time = 1491265680;
uint32_t last_update_posix_time = 1491251330;
long unsigned n_ticks_per_sec = 1000;
total_drift_SEC = (posix_time - at_update_posix_time);
drift_per_sec_TICK = ((float) total_drift_SEC)/(at_update_posix_time - last_update_posix_time);
n_ticks_per_sec += drift_per_sec_TICK;
printf("Total drift sec %d\r\n", total_drift_SEC);
printf("Drift per sec in ticks %d\r\n", drift_per_sec_TICK);
printf("n_ticks_per_second %lu\r\n", n_ticks_per_sec);
return 0;
}
我不明白的是,我需要投total_drift_SEC到浮动才能有一个正确的结果,最终,即到底有 n_ticks_per_sec等于1000。
此代码的输出是:
总漂移秒-60在蜱0
n_ticks_per_second 1000
鉴于代码的输出每秒
漂移没有演员扮演的角色是:
总漂移秒-60每秒
漂移在蜱298054
n_ticks_per_second 299054
备选:'rift_per_sec_TICK =(1LL * total_drift_SEC)/(at_update_posix_time - last_update_posix_time);' – chux
@chux:或:...'(的int64_t)total_drift_SEC ...'只是一些具有较高的排名,然后'uint32_t'签署。 – alk
@alk我喜欢'1LL *'或类似与铸造'1LL *'不会缩小'total_drift_SEC',无论类型如何,但是当'total_drift_SEC'是某种类型的'double'时, 。 – chux