使用 time_t 解析用户输入

发布于 2024-10-26 17:00:32 字数 583 浏览 2 评论 0原文

我的想法是,如果用户输入 t = 2.5,那么我将 2 和 0.5 分别提取到 2 个不同的变量中。但我无法做到这一点。

这是代码:

$ export LT_LEAK_START=1.5
$ echo $LT_LEAK_START
   1.5

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
 double d;
 time_t t;
 long nsec;

 d=strtod(getenv("LT_LEAK_START"), NULL);
 t=(time_t)d;

 nsec=d-(double)((time_t)d); // Extract fractional part as time_t always whole no.
 printf("d = %lf\n",d);
 printf("t = %u, nsec = %f\n",d,nsec);
}

输出是:

# ./a.out 
  d = 1.500000
  t = 0, nsec = 0.000000

My idea is, if user enters t = 2.5, then I extract 2 and 0.5 separately in 2 different variables. But I am unable to do that.

Here is the code:

$ export LT_LEAK_START=1.5
$ echo $LT_LEAK_START
   1.5

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
 double d;
 time_t t;
 long nsec;

 d=strtod(getenv("LT_LEAK_START"), NULL);
 t=(time_t)d;

 nsec=d-(double)((time_t)d); // Extract fractional part as time_t always whole no.
 printf("d = %lf\n",d);
 printf("t = %u, nsec = %f\n",d,nsec);
}

Output is:

# ./a.out 
  d = 1.500000
  t = 0, nsec = 0.000000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

机场等船 2024-11-02 17:00:32

你的输出坏了。实际上,您在以下代码中写入了两次 d 的值:

nsec=d-(double)((time_t)d); // Extract fractional part as time_t always whole no.
printf("d = %lf\n",d);
printf("t = %u, nsec = %f\n",d,nsec);

如果您这样写:

nsec=d-(double)((time_t)d); // Extract fractional part as time_t always whole no.
printf("d = %lf\n",d);
printf("t = %u, nsec = %f\n",t,nsec);

那么您将得到输出:

d = 1.500000
t = 1, nsec = 0.000000

现在可以更清楚地看出您有舍入错误。在这种情况下,您可以通过将 0.5 分配给 nsec(一个 long)来舍弃所有小数位。将 nsec 改为 float

Your output is broken. You're actually writing the value of d twice in the following code:

nsec=d-(double)((time_t)d); // Extract fractional part as time_t always whole no.
printf("d = %lf\n",d);
printf("t = %u, nsec = %f\n",d,nsec);

If you'd written this:

nsec=d-(double)((time_t)d); // Extract fractional part as time_t always whole no.
printf("d = %lf\n",d);
printf("t = %u, nsec = %f\n",t,nsec);

Then you'd have the output:

d = 1.500000
t = 1, nsec = 0.000000

It's now clearer that you have a rounding error. In this case, you cast away all the decimal places by assigning 0.5 to nsec, a long. Make nsec a float instead.

埋葬我深情 2024-11-02 17:00:32

您还尝试在 long 中存储小数值。您需要将其乘以 1000 或将 nsec 设为双精度。

nsec=d-(double)((time_t)d);

如果 d 为 1.5,则右侧的结果将为 0.5,当它存储在 nsec 中时,它会隐式向下转换为 0 。

You are also trying to store a fractional value in a long. You either need to multiply this by 1000 or make nsec a double.

nsec=d-(double)((time_t)d);

If d is 1.5, the result of the right hand side would be 0.5, which will implicitly cast down to 0 when it gets stored in nsec.

浮生未歇 2024-11-02 17:00:32

您尝试将 .5 分配给 long,但这不会发生。

double d = 1.5;
int i = (int)d;
double j = d - (double)i;

printf("%d %f\n",i,j);

You're trying to assign .5 to a long which isn't going to happen.

double d = 1.5;
int i = (int)d;
double j = d - (double)i;

printf("%d %f\n",i,j);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文