0

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
Olli
  • 1,231
  • 15
  • 31
RajSanpui
  • 11,556
  • 32
  • 79
  • 146

3 Answers3

1

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

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);
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
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.

Luke
  • 3,742
  • 4
  • 31
  • 50
  • He's using the format flag `%f`, so `nsec` should be a `float`, not a `double`. – Lightness Races in Orbit Mar 28 '11 at 12:11
  • %f is a printf formatting flag and isn't related to what you are describing. %f is suitable for displaying both floats and doubles. The f in %f simply means 'floating point' it doesn't mean single precision floating point. %f will display a double just fine. – Luke Mar 28 '11 at 12:16
  • @Luke: Really? Then what's `%lf`? (It _is_ related, because if he's using the wrong type then he's invoking UB.) – Lightness Races in Orbit Mar 28 '11 at 12:19
  • Yes really, the `l` modifier doesn't apply to floating point, see 'length' in the link below. If you mean `%Lf`, then that is a long double, which is more precise than a double. http://www.cplusplus.com/reference/clibrary/cstdio/printf/ – Luke Mar 28 '11 at 12:33
  • @Luke: Nah, I meant `%lf`. If `%f` is safe with `double`, then OK. However I'd prefer a C reference to a C++ reference. C++ is a different language. (And there *are* compatibility differences in C++'s C standard library.) – Lightness Races in Orbit Mar 28 '11 at 12:35
  • http://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output suggests that the `l` modifier most certainly does apply. – Lightness Races in Orbit Mar 28 '11 at 12:37
  • The `l` is used and valid for `scanf`, but it makes no difference for `printf` due to argument promotion. When `%f` is used, floats and doubles alike are promoted to doubles, thus there is no difference and `%lf` will be treated exactly like `%l` when used in `printf`. Because of its use in `scanf`, people are often confused and think that it should be used with `printf` as well. Just a note, `printf` is part of the standard library and is not part of either the C nor the C++ programming language. You are correct, your syntax is valid. But it is not needed. – Luke Mar 28 '11 at 12:53
  • http://stackoverflow.com/questions/210590/why-does-scanf-need-lf-for-doubles-when-printf-is-okay-with-just-f backs up my claim above. – Luke Mar 28 '11 at 12:54
  • @Luke: OK, that's cool then. :) (BTW, in C++, the library *is* part of the language. Not sure whether the same could be said for C, but I was clearly referring to the language+library body as a whole anyway.) – Lightness Races in Orbit Mar 28 '11 at 14:31