【发布时间】:2015-01-28 17:08:26
【问题描述】:
我创建了一个脚本来收集以秒和微秒为单位的时间戳。然后我试图计算这些值的差异。只要整数为正,结果就符合预期。
下一步,我将整数转换为字符串,并在它们之间用一个点连接它们。人类可读的格式更容易被用户阅读。
当整数为负数时会出现问题。首先,当我将负 int 转换为字符串时,我无法弄清楚如何打印负号。 (例如 3sec .-3microsec)
其次,如果两个整数都是负数并且我有两个负号,情况可能会变得更糟。 (例如 -3sec .-3microsec)
所以我的问题是,由于我试图减去两个实际上是浮点数的整数,是否还有其他替代方法可以将它们连接成浮点数然后应用减法? (例如 int 3sec int 3microsec 连接到 float 3.3sec)
我是 C 语言的初学者,所以我不确定这是否可行,这就是我经历了所有这些过程的原因(将 int 转换为字符串,然后进行连接)。
可执行代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <sys/timeb.h>
#include <inttypes.h>
#include <assert.h>
#define TIME_CHAR 22
#define PROCESS_CHARACTERS 32
typedef struct rec {
char time_1[TIME_CHAR];
char time_2[TIME_CHAR];
char time_3[TIME_CHAR];
char process[PROCESS_CHARACTERS];
}RECORD;
char *u2s(long unsigned int number) {
RECORD *ptr_record = malloc (sizeof(RECORD));
if (ptr_record == NULL) {
printf("Out of memory!\nExit!\n");
exit(0);
}
const int n = snprintf(NULL, 0, "%lu", number);
assert(n > 0);
char buf[n+1];
snprintf(buf, n+1, "%lu", number);
assert(buf[n] == '\0');
memcpy(ptr_record->process, buf , sizeof buf);
return ptr_record->process;
}
uint32_t ClockGetTime() {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return (int32_t)ts.tv_sec * 1000000LL + (int32_t)ts.tv_nsec / 1000LL;
}
void cleanup(RECORD** ptr_record) {
free(*ptr_record);
*ptr_record = NULL;
}
int main(int argc, char *argv[]) {
char *dot = ".";
RECORD *ptr_record;
ptr_record = malloc (sizeof(RECORD));
if (ptr_record == NULL) {
printf("Out of memory!\nExit!\n");
exit(0);
}
/* Originate Time Stamps for time_1 */
time_t time_1_sec = time(NULL);
uint32_t client_1_sec = time_1_sec;
uint32_t client_1_microsec = ClockGetTime();
char *client_1_sec_string = u2s(client_1_sec);
char *client_1_microsec_string = u2s(client_1_microsec);
memset( ptr_record->time_1 , '\0' , sizeof ptr_record->time_1 );
strncat( ptr_record->time_1 , client_1_sec_string , strlen(client_1_sec_string) );
strncat( ptr_record->time_1 , dot , strlen(dot) );
strncat( ptr_record->time_1 , client_1_microsec_string , strlen(client_1_microsec_string) );
/* Originate Time Stamps for time_2 */
time_t time_2_sec = time(NULL);
uint32_t client_2_sec = time_2_sec;
uint32_t client_2_microsec = ClockGetTime();
char *client_2_sec_string = u2s(client_2_sec);
char *client_2_microsec_string = u2s(client_2_microsec);
memset( ptr_record->time_2 , '\0' , sizeof ptr_record->time_2 );
strncat( ptr_record->time_2 , client_2_sec_string , strlen(client_2_sec_string) );
strncat( ptr_record->time_2 , dot , strlen(dot) );
strncat( ptr_record->time_2 , client_2_microsec_string , strlen(client_2_microsec_string) );
printf("This is buffer time_2: %s\n",ptr_record->time_2);
int32_t d_positive_sec = client_2_sec - client_1_sec;
int32_t d_positive_microsec = client_2_microsec - client_1_microsec;
printf("This is the positive difference in sec: %"PRId32"\n",d_positive_sec);
printf("This is the positive difference in microsec: %"PRId32"\n",d_possitive_microsec);
int32_t d_negative_sec = client_1_sec - client_2_sec;
int32_t d_negative_microsec = client_1_microsec - client_2_microsec;
printf("This is the negative difference in sec: %"PRId32"\n",d_negative_sec);
printf("This is the negative difference in microsec: %"PRId32"\n",d_negative_microsec);
char *n_sec_string = u2s(d_negative_sec);
char *n_microsec_string = u2s(d_negative_microsec);
memset( ptr_record->time_3 , '\0' , sizeof ptr_record->time_3 );
strncat( ptr_record->time_3 , n_sec_string , strlen(n_sec_string) );
strncat( ptr_record->time_3 , dot , strlen(dot) );
strncat( ptr_record->time_3 , n_microsec_string , strlen(n_microsec_string) );
printf("This is negative concatenated %s\n",ptr_record->time_3);
cleanup(&ptr_record);
return 0;
} /* End of main(){} */
输出样本:
This is the positive difference in sec: 0
This is the positive difference in microsec: 37
This is the negative difference in sec: 0
This is the negative difference in microsec: -37
This is negative concatenated 0.18446744073709551579
很容易看出,在我们应该期望看到 (0.-37) 的串联之后,最好的结果将是 (-0.37)。
【问题讨论】:
-
0s 加 37us 将得到 0.000037s 而不是 0.37s!
-
这绝对是真的,我能做些什么来解决这个问题?有什么建议吗?
-
在进行计算时切换到较小的分辨率。完成后,缩小到所需的单位。
-
在您的情况下,您可以检查 cplusplus.com/reference/cstdio/snprintf :此将格式化输出写入大小缓冲区。您还可以检查 cplusplus.com/reference/cstdio/printf 以获取不同的格式说明符。
-
@VishalGupta 你提供给我的链接真的很有用。感谢您花时间和精力帮助我。
标签: c concatenation subtraction negative-number