Dividing a time variable

This is a discussion on Dividing a time variable within the C Programming forums, part of the General Programming Boards category; So i have this - time_t t4 = time(NULL); //and then some stuff time_t t5 = time(NULL); iElapsedYah = t5-t4; ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    45

    Dividing a time variable

    So i have this -
    time_t t4 = time(NULL);

    //and then some stuff

    time_t t5 = time(NULL);


    iElapsedYah = t5-t4;


    fElapsedYah2 = iElapsedYah/iNumTimes;


    printf("It took %f seconds", fElapsedYah2);

    but it always prints 0.0000000, and i dont know why even though individually iElapsedYah = not zero and iNumTimes = not zero
    i need help!!!!!!!!!!!

    i am using windows 7 and m complier is Dev C++

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Integer division will never produce anything more precise than zero. Try casting either iElapsedYah or iNumTimes to double, you'll probably get better results.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,677
    Dividing two integral values truncates towards zero (except with some older compilers, when a negative operand is involved).


    Converting the value to floating point AFTER the truncation has occurred does not change that. So, if the elapsed time (between the two time() calls) is small enough, the result of division will be zero.

    Try "fElapsedYah2 = (double)iElapsedYah/iNumTimes;" I'm assuming fElapsedYah2 is of type double.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    yup ^ works, thanks prelude and grumpy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-10-2011, 06:00 PM
  2. Replies: 6
    Last Post: 11-05-2010, 11:34 AM
  3. Replies: 3
    Last Post: 11-20-2008, 12:31 PM
  4. Replies: 14
    Last Post: 11-17-2008, 12:31 PM
  5. Replies: 3
    Last Post: 05-23-2007, 12:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21