我試圖通過同步時間與服務器來修改第二個(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