Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #72251 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-10-09 02:36 -0700 |
| Last post | 2015-11-05 15:50 -0800 |
| Articles | 20 on this page of 49 — 12 participants |
Back to article view | Back to comp.lang.c
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
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-10-10 19:12 +0100 |
| Message-ID | <mvbka6$qsf$1@dont-email.me> |
| In reply to | #72396 |
On 10/10/2015 17:25, Bartc wrote:
> On 10/10/2015 14:57, Tim Rentsch wrote:
[Dealing with negative number of seconds]
>> 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);
This will return a positive time when seconds are negative; the sign of
the time is lost.
But I think your method will return negative hours, negative minutes and
negative seconds; that doesn't sound right either.
There should be just one sign indicator.
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-10-10 20:56 +0100 |
| Message-ID | <871td2ijkw.fsf@bsb.me.uk> |
| In reply to | #72396 |
Bartc <bc@freeuk.com> writes:
> On 10/10/2015 14:57, Tim Rentsch wrote:
>
>> Here is another perspective for your consideration. My advice is
>> to disregard the advice of people who say not to use compound
>> literals. The main value of using compound literals is not
>> whether the code is shorter or longer (though in most cases it
>> will be shorter), but that it expresses what is needed directly
>> rather than indirectly.
>
> The problem is that, as they are implemented in C, they tend to have
> rather more clutter than is desired, which reduces the expressiveness.
>
>> struct time
>> seconds_to_time( Seconds seconds ){
>> return (struct time){
>> .hour = seconds / 60 / 60,
>> .minutes = seconds / 60 % 60,
>> .seconds = seconds % 60
>> };
>> }
>
> 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. You need
to state the type in both versions, but with the compound literal there
is no need to invent a name for a temporary object.
(Aside: I'm perplexed why statically-typed languages with type inference
did not gain more ground. It seems to me to the obvious way to static
type-checking without so much clutter.)
> Or even like this (less dependent on how struct time is
> defined and with fewer divide and mod operations):
>
> struct time seconds_to_tims(Seconds seconds) {
> struct time T;
> T.hours = seconds/3600;
> T.minutes = (seconds-T.hours*3600)/60;
> T.seconds = seconds-T.hours*3600-T.minutes*60
> return T;
> }
There are fewer / and % symbols in the source, but the number of divide
and mod operations is up to the compiler, and gcc on my machine does not
agree that yours re-write has fewer. Of course it might end up as you
say on some other system, but the point is that this is a micro-
optimisation that should not impede code clarity (at least not until
there is a good reason).
>> 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?
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-10-10 21:52 +0100 |
| Message-ID | <mvbtm5$sgf$1@dont-email.me> |
| In reply to | #72411 |
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
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-10-10 17:57 -0700 |
| Message-ID | <lnoag6cjd0.fsf@kst-u.example.com> |
| In reply to | #72396 |
Bartc <bc@freeuk.com> writes:
> On 10/10/2015 14:57, Tim Rentsch wrote:
>> Here is another perspective for your consideration. My advice is
>> to disregard the advice of people who say not to use compound
>> literals. The main value of using compound literals is not
>> whether the code is shorter or longer (though in most cases it
>> will be shorter), but that it expresses what is needed directly
>> rather than indirectly.
>
> The problem is that, as they are implemented in C, they tend to have
> rather more clutter than is desired, which reduces the expressiveness.
>
>> struct time
>> seconds_to_time( Seconds seconds ){
>> return (struct time){
>> .hour = seconds / 60 / 60,
>> .minutes = seconds / 60 % 60,
>> .seconds = seconds % 60
>> };
>> }
>
> 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;
> }
Or, IMHO more clearly:
struct time seconds_to_tims(Seconds seconds) {
const struct time T = {
.hour = seconds/60/60,
.minutes = seconds/60%60,
.seconds = seconds%60
};
return T;
}
Incidentally, the member names should be all plural or all singular.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | raltbos@xs4all.nl (Richard Bos) |
|---|---|
| Date | 2015-10-11 10:09 +0000 |
| Message-ID | <561a350e.4188015@news.xs4all.nl> |
| In reply to | #72446 |
Keith Thompson <kst-u@mib.org> wrote:
> struct time seconds_to_tims(Seconds seconds) {
> const struct time T = {
> .hour = seconds/60/60,
> .minutes = seconds/60%60,
> .seconds = seconds%60
> };
> return T;
> }
>
> Incidentally, the member names should be all plural or all singular.
Hm. In theory, yes, quite agree. Certainly for other structures.
However, in daily life, when talking about a time, IME people _do_ talk
about the hour singular, then minutes and seconds plural.
Richard
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-10-11 11:26 +0100 |
| Message-ID | <mvddd5$sjr$1@dont-email.me> |
| In reply to | #72446 |
On 11/10/2015 01:57, Keith Thompson wrote:
> Bartc <bc@freeuk.com> writes:
>> What would that last function look like without compound literals? Perhaps:
>>
>> struct time seconds_to_tims(Seconds seconds) {
(Did I really write 'tims'?)
>> struct time T = {seconds/60/60, seconds/60%60, seconds%60};
>> return T;
>> }
> Or, IMHO more clearly:
>
> struct time seconds_to_tims(Seconds seconds) {
> const struct time T = {
> .hour = seconds/60/60,
> .minutes = seconds/60%60,
> .seconds = seconds%60
> };
> return T;
> }
What's the purpose of the 'const' here? Because I can't see it.
> Incidentally, the member names should be all plural or all singular.
It was like that in the OP.
(I don't like 'Seconds seconds', or 'struct time', but I go along with
it because it's someone else's style.
For the seconds, it should just have type 'int' or the next size up.
There should really be a more succinct and elegant way of denoting the
latter than 'long long int' or 'int64_t'. 'long int' might do if it
actually worked.)
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-10-11 14:12 -0700 |
| Message-ID | <lnd1wlcdoq.fsf@kst-u.example.com> |
| In reply to | #72463 |
Bartc <bc@freeuk.com> writes:
> On 11/10/2015 01:57, Keith Thompson wrote:
[..]
>> Or, IMHO more clearly:
>>
>> struct time seconds_to_tims(Seconds seconds) {
>> const struct time T = {
>> .hour = seconds/60/60,
>> .minutes = seconds/60%60,
>> .seconds = seconds%60
>> };
>> return T;
>> }
>
> What's the purpose of the 'const' here? Because I can't see it.
It indicates, both to the compiler and to the reader, that the object
won't be modified after it's been initialized. It's not much of an
advantage with such a small scope, but I prefer to omit "const" only
when an object will actually be modified (except that I don't bother
defining parameters as "const").
[...]
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2015-10-15 10:51 -0700 |
| Message-ID | <kfn8u74j9zj.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #72446 |
Keith Thompson <kst-u@mib.org> writes:
> Bartc <bc@freeuk.com> writes:
>> On 10/10/2015 14:57, Tim Rentsch wrote:
>>> Here is another perspective for your consideration. My advice is
>>> to disregard the advice of people who say not to use compound
>>> literals. The main value of using compound literals is not
>>> whether the code is shorter or longer (though in most cases it
>>> will be shorter), but that it expresses what is needed directly
>>> rather than indirectly.
>>
>> The problem is that, as they are implemented in C, they tend to have
>> rather more clutter than is desired, which reduces the expressiveness.
>>
>>> struct time
>>> seconds_to_time( Seconds seconds ){
>>> return (struct time){
>>> .hour = seconds / 60 / 60,
>>> .minutes = seconds / 60 % 60,
>>> .seconds = seconds % 60
>>> };
>>> }
>>
>> 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;
>> }
>
> Or, IMHO more clearly:
>
> struct time seconds_to_tims(Seconds seconds) {
> const struct time T = {
> .hour = seconds/60/60,
> .minutes = seconds/60%60,
> .seconds = seconds%60
> };
> return T;
> }
I was going to ask why you used a temporary, but now I see that
there was a temporary in the message you were responding to. Is
there some reason for using a temporary rather than just writing
a compound literal in the return statement? Normally I would not
use a temporary in a case like this.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-10-15 11:25 -0700 |
| Message-ID | <lnh9ls6lbo.fsf@kst-u.example.com> |
| In reply to | #72968 |
Tim Rentsch <txr@alumni.caltech.edu> writes:
> Keith Thompson <kst-u@mib.org> writes:
>> 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;
>>> }
>>
>> Or, IMHO more clearly:
>>
>> struct time seconds_to_tims(Seconds seconds) {
>> const struct time T = {
>> .hour = seconds/60/60,
>> .minutes = seconds/60%60,
>> .seconds = seconds%60
>> };
>> return T;
>> }
>
> I was going to ask why you used a temporary, but now I see that
> there was a temporary in the message you were responding to. Is
> there some reason for using a temporary rather than just writing
> a compound literal in the return statement? Normally I would not
> use a temporary in a case like this.
No, there's no particular reason to use a temporary if compound
literals are available. (If they're not, you need a temporary
object so you can use an initializer).
I might *temporarily* use a temporary if I want to print the value
before returning it.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2015-10-16 05:34 -0700 |
| Message-ID | <kfn6127hu01.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #72974 |
Keith Thompson <kst-u@mib.org> writes:
> Tim Rentsch <txr@alumni.caltech.edu> writes:
>> Keith Thompson <kst-u@mib.org> writes:
>>> 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;
>>>> }
>>>
>>> Or, IMHO more clearly:
>>>
>>> struct time seconds_to_tims(Seconds seconds) {
>>> const struct time T = {
>>> .hour = seconds/60/60,
>>> .minutes = seconds/60%60,
>>> .seconds = seconds%60
>>> };
>>> return T;
>>> }
>>
>> I was going to ask why you used a temporary, but now I see that
>> there was a temporary in the message you were responding to. Is
>> there some reason for using a temporary rather than just writing
>> a compound literal in the return statement? Normally I would not
>> use a temporary in a case like this.
>
> No, there's no particular reason to use a temporary if compound
> literals are available.
Okay, glad that got cleared up.
> (If they're not, you need a temporary object so you can use an
> initializer).
Note that if compound literals are not available then using an
initializer doesn't work either in this case, because of C90's
more restrictive rules for aggregate initializers. These days
I think most people take the less restrictive C99 rule pretty
much for granted in such circumstances.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-10-16 08:52 -0700 |
| Message-ID | <lnr3ku6cb1.fsf@kst-u.example.com> |
| In reply to | #73026 |
Tim Rentsch <txr@alumni.caltech.edu> writes:
> Keith Thompson <kst-u@mib.org> writes:
[...]
>> No, there's no particular reason to use a temporary if compound
>> literals are available.
>
> Okay, glad that got cleared up.
>
>> (If they're not, you need a temporary object so you can use an
>> initializer).
>
> Note that if compound literals are not available then using an
> initializer doesn't work either in this case, because of C90's
> more restrictive rules for aggregate initializers. These days
> I think most people take the less restrictive C99 rule pretty
> much for granted in such circumstances.
Ah, you're right. I forgot that C90 permits only constant
expressions in initializers.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2015-10-15 10:46 -0700 |
| Message-ID | <kfnd1wgja8s.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #72396 |
Bartc <bc@freeuk.com> writes:
> On 10/10/2015 14:57, Tim Rentsch wrote:
>
>> Here is another perspective for your consideration. My advice is
>> to disregard the advice of people who say not to use compound
>> literals. The main value of using compound literals is not
>> whether the code is shorter or longer (though in most cases it
>> will be shorter), but that it expresses what is needed directly
>> rather than indirectly.
>
> The problem is that, as they are implemented in C, they tend to have
> rather more clutter than is desired, which reduces the expressiveness.
>
>> struct time
>> seconds_to_time( Seconds seconds ){
>> return (struct time){
>> .hour = seconds / 60 / 60,
>> .minutes = seconds / 60 % 60,
>> .seconds = seconds % 60
>> };
>> }
>
> 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. Or even like this (less dependent on how struct time is
> defined and with fewer divide and mod operations):
>
> struct time seconds_to_tims(Seconds seconds) {
> struct time T;
> T.hours = seconds/3600;
> T.minutes = (seconds-T.hours*3600)/60;
> T.seconds = seconds-T.hours*3600-T.minutes*60
> return T;
> }
>
>
>> 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);
The response from Ben Bacarisse said essentially all of
what I was going to say.
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-10-15 19:06 +0100 |
| Message-ID | <mvoprj$jfl$1@dont-email.me> |
| In reply to | #72967 |
On 15/10/2015 18:46, Tim Rentsch wrote:
> Bartc <bc@freeuk.com> writes:
>>> 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);
>
> The response from Ben Bacarisse said essentially all of
> what I was going to say.
Since you're here, did you intend that converting -4000 seconds gives
negative hours, negative minutes, and negative seconds?
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2015-10-16 05:24 -0700 |
| Message-ID | <kfna8rjhuha.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #72971 |
Bartc <bc@freeuk.com> writes:
> On 15/10/2015 18:46, Tim Rentsch wrote:
>> Bartc <bc@freeuk.com> writes:
>>
>>>> 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);
>>
>> The response from Ben Bacarisse said essentially all of
>> what I was going to say.
>
> Since you're here, did you intend that converting -4000 seconds gives
> negative hours, negative minutes, and negative seconds?
Given how the code was written, I am at a loss to understand
what might prompt someone to think otherwise.
[toc] | [prev] | [next] | [standalone]
| From | glen herrmannsfeldt <gah@ugcs.caltech.edu> |
|---|---|
| Date | 2015-10-17 00:10 +0000 |
| Message-ID | <mvs3mg$rps$2@speranza.aioe.org> |
| In reply to | #72396 |
Bartc <bc@freeuk.com> wrote:
> On 10/10/2015 14:57, Tim Rentsch wrote:
(snip)
>> 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);
In PL/I, which allows structure expressions, it could be:
if (seconds<0) return -seconds_to_time(-seconds);
I don't think that works in C.
-- glen
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-10-18 09:52 -0700 |
| Message-ID | <cbj72blhll1qepasnpg1419cc62f8mf7hf@4ax.com> |
| In reply to | #72262 |
On Fri, 9 Oct 2015 06:49:40 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:
>On Friday, October 9, 2015 at 4:26:35 PM UTC+4, Richard Heathfield wrote:
>> On 09/10/15 10:36, Alla _ wrote:
>>
>> > Here is the error message I get when I try
>> > to compile the program. I don't see why I get
>> > this error message.
>> >
>> > ex8-3.c: In function 'elapsed_time':
>> > ex8-3.c:48: error: expected expression before '{' token
>> <snip>
>>
>> > result = {.hour = time_2.hour - time_1.hour, .minutes = time_2.minutes - time_1.minutes,
.seconds = time_2.seconds - time_1.seconds};
>>
>> It's complaining about .hour
>>
>> Is there any particular reason why you can't write
>>
>> result.hour = time_2.hour - time_1.hour;
>> result.minutes = time_2.minutes - time_1.minutes;
>> result.seconds = time_2.seconds - time_1.seconds;
>>
>> ?
>>
>> It will compile, it's clearer, it requires less typing in a good editor,
>> and it makes the bug more obvious and far easier to fix.
>>
>
>Thank you! No, of course, there is no obligation for me to use that approach; I was trying different
>approaches to initialization, which are described in the textbook.
You should know by now that this is an assignment, not an
initialization. If your book calls it initialization, get another one
where the author knows something about C.
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-10-09 06:52 -0700 |
| Message-ID | <a2834562-310c-4589-a2a7-90c703e1a6b6@googlegroups.com> |
| In reply to | #72251 |
it compiles now, but, indeed, produces a bizarre result :) looking into the code itself now :)
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-10-18 10:21 -0700 |
| Message-ID | <gij72btr8a1o4iisg238kfnkbhob3i39j1@4ax.com> |
| In reply to | #72263 |
On Fri, 9 Oct 2015 06:52:42 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:
>it compiles now, but, indeed, produces a bizarre
>result :) looking into the code itself now :)
Of course it does. You should take a pencil and paper and "play
computer" with some sample input to see where your algorithm breaks
down.
You tell the user to enter time as hhmmss but your read it in as
ssmmhh.
You test minutes to determine if the seconds wrap but that is
irrelevant. The difference between time2=hhmm15 and time1=hhMM10 will
be always end in 5 seconds regardless of whether mm < MM, mm == MM, or
mm > MM. In your code, if mm < MM, you will get a value of 65 for
seconds. The test on seconds is the only relevant one. And of course
that test is meaningless when trying to deal with minutes.
If the clock has wrapped to the next day, simply add 24 to the
negative hours you already calculated.
Why, after computing all the values, do you attempt to compute
them again? And with formulas that do not take into account any of
the conditions you tested for the first time?
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-11-05 00:56 -0800 |
| Message-ID | <96d1b3f5-3ab0-4b14-a0bc-8f7e6e07fa0a@googlegroups.com> |
| In reply to | #73215 |
Long time after I created this thread, I have corrected the
program, and it works now. Thank you for your help!
// Program to computed elapsed time between two times
#include <stdio.h>
struct time
{
int hour;
int minutes;
int seconds;
};
struct time elapsed_time (struct time time_1, struct time time_2);
int main (void)
{
struct time time_one, time_two, elapsed;
printf("Please, enter two times (hh:mm:ss):\n");
scanf ("%i:%i:%i %i:%i:%i", &time_one.hour, &time_one.minutes, &time_one.seconds, &time_two.hour, &time_two.minutes, &time_two.seconds);
elapsed = elapsed_time (time_one, time_two);
printf("The elapsed time is %.2i:%.2i:%.2i\n", elapsed.hour, elapsed.minutes, elapsed.seconds);
return 0;
}
// Function to compute elapsed time
struct time elapsed_time (struct time time_1, struct time time_2)
{
struct time result;
if (time_2.seconds < time_1.seconds)
{
result.seconds = time_2.seconds - time_1.seconds + 60;
time_2.minutes--;
if (time_2.minutes < time_1.minutes)
{
result.minutes = time_2.minutes - time_1.minutes + 60;
time_2.hour --;
}
else
result.minutes = time_2.minutes - time_1.minutes;
// Time_2 is the next day, over 00:00:00
if (time_2.hour < time_1.hour)
result.hour = 24 - time_1.hour + time_2.hour;
else
result.hour = time_2.hour - time_1.hour;
}
else
{
result.seconds = time_2.seconds - time_1.seconds;
if (time_2.minutes < time_1.minutes)
{
result.minutes = time_2.minutes - time_1.minutes + 60;
time_2.hour --;
}
else
result.minutes = time_2.minutes - time_1.minutes;
// Time_2 is the next day, over 00:00:00
if (time_2.hour < time_1.hour)
result.hour = 24 - time_1.hour + time_2.hour;
else
result.hour = time_2.hour - time_1.hour;
}
return result;
}
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-11-05 01:55 -0800 |
| Message-ID | <rv7m3btng0grkp33264jvmkc7v8brv8es7@4ax.com> |
| In reply to | #74933 |
On Thu, 5 Nov 2015 00:56:43 -0800 (PST), Alla _
<modelling.data@gmail.com> wrote:
>Long time after I created this thread, I have corrected the
>program, and it works now. Thank you for your help!
>
>// Program to computed elapsed time between two times
>#include <stdio.h>
>
>struct time
>{
> int hour;
> int minutes;
> int seconds;
>};
>
>struct time elapsed_time (struct time time_1, struct time time_2);
>
>int main (void)
>{
> struct time time_one, time_two, elapsed;
>
> printf("Please, enter two times (hh:mm:ss):\n");
> scanf ("%i:%i:%i %i:%i:%i", &time_one.hour, &time_one.minutes, &time_one.seconds, &time_two.hour, &time_two.minutes, &time_two.seconds);
There is a difference between %i and %d for input. When dealing with
time values, it would not be unusual for values below 10 to be entered
with a leading 0. Using %i causes these values to be treated as
octal. If you enter the value 08 or 09, the conversion will terminate
in error prematurely. Apparently your testing did not include any of
these values.
Unless you really need the octal conversion (obviously undesirable in
this case), you should stick with %d.
> elapsed = elapsed_time (time_one, time_two);
>
> printf("The elapsed time is %.2i:%.2i:%.2i\n", elapsed.hour, elapsed.minutes, elapsed.seconds);
There is no distinction on output.
> return 0;
>}
>
>// Function to compute elapsed time
>
>struct time elapsed_time (struct time time_1, struct time time_2)
It is not a big deal in this case, but passing and returning
structures by value is not all that common. Most of the time, you
pass the address of the structures (const qualified when appropriate).
>{
> struct time result;
>
> if (time_2.seconds < time_1.seconds)
> {
> result.seconds = time_2.seconds - time_1.seconds + 60;
> time_2.minutes--;
>
> if (time_2.minutes < time_1.minutes)
> {
> result.minutes = time_2.minutes - time_1.minutes + 60;
> time_2.hour --;
> }
> else
>
> result.minutes = time_2.minutes - time_1.minutes;
Consider just this if-else block for minutes. You have the same
partial formula "time_2.minutes - time_1.minutes" twice. Verifying
that both are the same is needless work. If time2<time1, then
time2-time1 will be negative. This lets you code the block as the
much shorter and easier to read
results.minutes = time2.minutes - time1.minutes;
if (results.minutes < 0)
{
results.minutes += 60;
time2.hour--;
}
> // Time_2 is the next day, over 00:00:00
> if (time_2.hour < time_1.hour)
>
> result.hour = 24 - time_1.hour + time_2.hour;
>
> else
>
> result.hour = time_2.hour - time_1.hour;
> }
>
> else
> {
> result.seconds = time_2.seconds - time_1.seconds;
>
> if (time_2.minutes < time_1.minutes)
You have way too much redundant code. You test minutes twice and
hours twice. Each need be tested only once.
When testing seconds, you should deal exclusively with seconds plus
the decrement of minutes when needed. When testing minutes, you
should deal exclusively with minutes plus the decrement of hours if
needed.
> {
> result.minutes = time_2.minutes - time_1.minutes + 60;
> time_2.hour --;
> }
> else
>
> result.minutes = time_2.minutes - time_1.minutes;
>
> // Time_2 is the next day, over 00:00:00
> if (time_2.hour < time_1.hour)
>
> result.hour = 24 - time_1.hour + time_2.hour;
>
> else
>
> result.hour = time_2.hour - time_1.hour;
> }
>
> return result;
>}
>
>
>
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
Back to top | Article view | comp.lang.c
csiph-web