Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #72420

Re: Program to compute elapsed time, learning structures and functions

From Bartc <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: Program to compute elapsed time, learning structures and functions
Date 2015-10-10 21:52 +0100
Organization A noiseless patient Spider
Message-ID <mvbtm5$sgf$1@dont-email.me> (permalink)
References (1 earlier) <mv8bku$22a$1@dont-email.me> <d861f166-2cc0-4879-80c3-68bb2759139d@googlegroups.com> <kfn8u7a25d7.fsf@x-alumni2.alumni.caltech.edu> <mvbe1p$3fb$1@dont-email.me> <871td2ijkw.fsf@bsb.me.uk>

Show all headers | View raw


On 10/10/2015 20:56, Ben Bacarisse wrote:
> Bartc <bc@freeuk.com> writes:

>> What would that last function look like without compound literals? Perhaps:
>>
>>    struct time seconds_to_tims(Seconds seconds) {
>>      struct time T = {seconds/60/60, seconds/60%60, seconds%60};
>>      return T;
>>    }
>>
>> Not too bad.
>
> That's comparing different things.  If you want to de-clutter (and loose
> a little clarity) by removing the designated initialisers and some of
> the white space, the real equivalent code is
>
>    struct time seconds_to_tims(Seconds seconds) {
>      return (struct time) {seconds/60/60, seconds/60%60, seconds%60};
>    }
>
> which is still shorter and more direct than your suggestion.

Yes, I got designated initialisers tied to compound literals. In fact 
either can be used independently of the other, giving more alternatives.

>>>       struct time
>>>       seconds_to_time( Seconds seconds ){
>>>           signed  sign = seconds < 0 ? -1 : 1;
>>>           return  (struct time){
>>>               .hour    =   seconds * sign   / 60  / 60   * sign,
>>>               .minutes =   seconds * sign   / 60  % 60   * sign,
>>>               .seconds =   seconds * sign         % 60   * sign
>>>           };
>>>       }
>>
>> Perhaps an easier way is to take your original function and add this
>> line at the start:
>>
>>       if (seconds<0) return seconds_to_time(-seconds);
>
> That may be easier but it does not produce the same result.  Are you
> saying Tim's intended result is the wrong one?

I addressed that in a follow-up to my post. I think both methods are 
wrong: mine because the result does not indicate if the time was 
negative; Tim's because I didn't like how a negative h:m:s value was 
represented.

seconds_to_time(-4000) gave -1:-06:-40 (when I tested the above 
function). I don't think having each of hour, minutes and seconds be 
negative was correct. (You only need one sign indicator, and don't want 
to deal with the possibility of maybe having -5:12:-30.)

It requires that 'struct time' is refined to have a way of representing 
negative time. Then if that is a sign flag for example, then my idea of 
keeping the main function body the same still holds. However, getting 
the sign field set up is fiddly enough that there wouldn't be much 
difference in overall clutter.

Here is one example where seconds_to_time is split, with the sign 
handled elsewhere:

      struct time
      useconds_to_time( Seconds seconds, int sign ){  // unsigned i/p
          return  (struct time){
              .hour    =   seconds   / 60  / 60,
              .minutes =   seconds   / 60  % 60,
              .seconds =   seconds         % 60,
              .sign    =   sign
          };
      }

      struct time
      seconds_to_time( Seconds seconds ){
          if (seconds<0)
              return useconds_to_time(-seconds,1);
          else
              return useconds_to_time(seconds,0);
      }

-- 
Bartc

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 02:36 -0700
  Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-09 10:48 +0100
    Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 03:06 -0700
  Re: Program to compute elapsed time, learning structures and functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 11:01 +0100
    Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 03:07 -0700
  Re: Program to compute elapsed time, learning structures and functions Richard Heathfield <rjh@cpax.org.uk> - 2015-10-09 13:26 +0100
    Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 06:49 -0700
      Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-09 16:20 +0100
        Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 09:27 -0700
        Re: Program to compute elapsed time, learning structures and functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 19:29 +0100
        Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-09 14:06 -0700
          Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-09 22:24 +0100
            Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-09 15:51 -0700
      Re: Program to compute elapsed time, learning structures and functions Richard Heathfield <rjh@cpax.org.uk> - 2015-10-09 18:26 +0100
        Re: Program to compute elapsed time, learning structures and functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 19:24 +0100
          Re: Program to compute elapsed time, learning structures and functions Richard Heathfield <rjh@cpax.org.uk> - 2015-10-09 19:50 +0100
            Re: Program to compute elapsed time, learning structures and functions Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-09 15:56 -0400
      Re: Program to compute elapsed time, learning structures and functions James Kuyper <jameskuyper@verizon.net> - 2015-10-09 15:31 -0400
      Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-10 06:57 -0700
        Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-10 17:25 +0100
          Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-10 19:12 +0100
          Re: Program to compute elapsed time, learning structures and functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-10 20:56 +0100
            Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-10 21:52 +0100
          Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-10 17:57 -0700
            Re: Program to compute elapsed time, learning structures and functions raltbos@xs4all.nl (Richard Bos) - 2015-10-11 10:09 +0000
            Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-11 11:26 +0100
              Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-11 14:12 -0700
            Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-15 10:51 -0700
              Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-15 11:25 -0700
                Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-16 05:34 -0700
                Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-16 08:52 -0700
          Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-15 10:46 -0700
            Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-15 19:06 +0100
              Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-16 05:24 -0700
          Re: Program to compute elapsed time, learning structures and functions glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-17 00:10 +0000
      Re: Program to compute elapsed time, learning structures and functions Barry Schwarz <schwarzb@dqel.com> - 2015-10-18 09:52 -0700
  Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 06:52 -0700
    Re: Program to compute elapsed time, learning structures and functions Barry Schwarz <schwarzb@dqel.com> - 2015-10-18 10:21 -0700
      Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 00:56 -0800
        Re: Program to compute elapsed time, learning structures and functions Barry Schwarz <schwarzb@dqel.com> - 2015-11-05 01:55 -0800
          Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 02:55 -0800
          Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 03:01 -0800
            Re: Program to compute elapsed time, learning structures and functions Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-05 03:41 -0800
              Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 06:55 -0800
          Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 03:03 -0800
            Re: Program to compute elapsed time, learning structures and functions Richard Heathfield <rjh@cpax.org.uk> - 2015-11-05 13:27 +0000
              Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 06:54 -0800
        Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-11-05 12:42 -0800
          Re: Program to compute elapsed time, learning structures and functions Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-05 15:50 -0800

csiph-web