Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #71692 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-10-03 10:43 -0700 |
| Last post | 2015-10-06 07:34 +0200 |
| Articles | 20 on this page of 104 — 24 participants |
Back to article view | Back to comp.lang.c
Learning functions; roots of the quadratic; two return values Alla _ <modelling.data@gmail.com> - 2015-10-03 10:43 -0700
Re: Learning functions; roots of the quadratic; two return values Andrew Cooper <amc96@cam.ac.uk> - 2015-10-03 19:00 +0100
Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-03 19:34 +0100
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-03 21:29 +0200
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-03 21:36 +0200
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-03 21:43 +0200
Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-03 20:46 +0100
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-03 22:46 +0200
Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-03 23:18 +0100
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-04 01:08 +0200
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-04 01:36 +0200
Re: Learning functions; roots of the quadratic; two return values Ian Collins <ian-news@hotmail.com> - 2015-10-04 12:52 +1300
Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-04 15:42 +0100
Re: Learning functions; roots of the quadratic; two return values jacobnavia <jacob@jacob.remcomp.fr> - 2015-10-03 23:05 +0200
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-03 23:41 +0200
Re: Learning functions; roots of the quadratic; two return values jacobnavia <jacob@jacob.remcomp.fr> - 2015-10-04 00:14 +0200
Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-03 23:07 +0100
Re: Learning functions; roots of the quadratic; two return values jacobnavia <jacob@jacob.remcomp.fr> - 2015-10-04 00:32 +0200
Re: Learning functions; roots of the quadratic; two return values Keith Thompson <kst-u@mib.org> - 2015-10-04 12:56 -0700
Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-04 23:41 +0100
Re: Learning functions; roots of the quadratic; two return values Keith Thompson <kst-u@mib.org> - 2015-10-04 18:24 -0700
Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-04 16:36 -0400
Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-05 00:01 +0100
Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-04 19:24 -0400
Re: Learning functions; roots of the quadratic; two return values Ian Collins <ian-news@hotmail.com> - 2015-10-04 10:18 +1300
Re: Learning functions; roots of the quadratic; two return values jacobnavia <jacob@jacob.remcomp.fr> - 2015-10-03 23:27 +0200
Re: Learning functions; roots of the quadratic; two return values Ian Collins <ian-news@hotmail.com> - 2015-10-04 11:08 +1300
Re: Learning functions; roots of the quadratic; two return values Richard Damon <Richard@Damon-Family.org> - 2015-10-03 18:38 -0400
Re: Learning functions; roots of the quadratic; two return values Ian Collins <ian-news@hotmail.com> - 2015-10-04 11:47 +1300
Re: Learning functions; roots of the quadratic; two return values raltbos@xs4all.nl (Richard Bos) - 2015-10-04 14:07 +0000
Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-04 15:48 +0100
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-04 18:59 +0200
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-04 20:06 +0200
Re: Learning functions; roots of the quadratic; two return values Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-04 15:21 -0700
Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 05:25 +0000
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-03 21:48 +0200
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-04 00:31 +0200
Re: Learning functions; roots of the quadratic; two return values alla.rashitova@gmail.com - 2015-10-05 08:40 -0700
Re: Learning functions; roots of the quadratic; two return values Richard Heathfield <rjh@cpax.org.uk> - 2015-10-05 17:10 +0100
Re: Learning functions; roots of the quadratic; two return values Barry Schwarz <schwarzb@dqel.com> - 2015-10-05 10:41 -0700
Re: Learning functions; roots of the quadratic; two return values "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-10-03 12:04 -0700
Re: Learning functions; roots of the quadratic; two return values Philip Lantz <prl@canterey.us> - 2015-10-03 12:48 -0700
Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-03 20:55 +0100
Re: Learning functions; roots of the quadratic; two return values "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-10-03 13:27 -0700
Re: Learning functions; roots of the quadratic; two return values jacobnavia <jacob@jacob.remcomp.fr> - 2015-10-03 23:08 +0200
Re: Learning functions; roots of the quadratic; two return values Richard Heathfield <rjh@cpax.org.uk> - 2015-10-03 22:52 +0100
Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-04 20:46 +0200
Re: Learning functions; roots of the quadratic; two return values Keith Thompson <kst-u@mib.org> - 2015-10-04 15:47 -0700
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-05 03:06 +0200
Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-05 05:31 +0200
Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-05 11:58 +0200
Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-05 12:00 +0200
Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-05 12:06 +0200
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-05 12:39 +0200
Re: Learning functions; roots of the quadratic; two return values Bartc <bc@freeuk.com> - 2015-10-05 12:20 +0100
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-05 13:50 +0200
Re: Learning functions; roots of the quadratic; two return values Richard Heathfield <rjh@cpax.org.uk> - 2015-10-05 12:57 +0100
Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 05:40 +0000
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-05 08:30 +0200
Re: Learning functions; roots of the quadratic; two return values Philip Lantz <prl@canterey.us> - 2015-10-05 10:03 -0700
Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 22:25 +0000
Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-05 19:26 -0400
Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-06 03:44 +0000
Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-06 10:55 -0400
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-06 23:07 +0200
Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-06 18:35 -0400
Re: Learning functions; roots of the quadratic; two return values raltbos@xs4all.nl (Richard Bos) - 2015-10-14 11:08 +0000
Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-14 11:11 -0400
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-14 22:22 +0200
Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-14 17:17 -0400
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-14 23:45 +0200
Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-15 00:11 +0000
Re: Learning functions; roots of the quadratic; two return values Bartc <bc@freeuk.com> - 2015-10-15 01:37 +0100
Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-15 05:52 +0000
Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-14 21:54 -0400
Re: Learning functions; roots of the quadratic; two return values asetofsymbols@gmail.com - 2015-10-14 22:40 -0700
Re: Learning functions; roots of the quadratic; two return values asetofsymbols@gmail.com - 2015-10-15 05:00 -0700
Re: Learning functions; roots of the quadratic; two return values supercat@casperkitty.com - 2015-10-15 11:58 -0700
Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-15 06:04 +0000
Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-06 23:18 +0000
Re: Learning functions; roots of the quadratic; two return values supercat@casperkitty.com - 2015-10-06 16:31 -0700
Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-07 00:38 +0000
Re: Learning functions; roots of the quadratic; two return values supercat@casperkitty.com - 2015-10-06 21:19 -0700
Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-06 22:15 -0400
Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-07 08:45 +0000
Re: Learning functions; roots of the quadratic; two return values Melzzzzz <mel@zzzzz.com> - 2015-10-07 05:19 +0200
Re: Learning functions; roots of the quadratic; two return values Keith Thompson <kst-u@mib.org> - 2015-10-07 09:12 -0700
Re: Learning functions; roots of the quadratic; two return values Melzzzzz <mel@zzzzz.com> - 2015-10-08 00:53 +0200
Re: Learning functions; roots of the quadratic; two return values Keith Thompson <kst-u@mib.org> - 2015-10-07 16:23 -0700
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-06 02:16 +0200
Re: Learning functions; roots of the quadratic; two return values supercat@casperkitty.com - 2015-10-06 15:43 -0700
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-07 02:48 +0200
Re: Learning functions; roots of the quadratic; two return values Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-14 14:00 -0700
Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-14 23:23 +0200
Re: Learning functions; roots of the quadratic; two return values Geoff <geoff@invalid.invalid> - 2015-10-03 12:50 -0700
Re: Learning functions; roots of the quadratic; two return values Bartc <bc@freeuk.com> - 2015-10-03 21:16 +0100
Re: Learning functions; roots of the quadratic; two return values alla.rashitova@gmail.com - 2015-10-05 09:02 -0700
Re: Learning functions; roots of the quadratic; two return values Richard Heathfield <rjh@cpax.org.uk> - 2015-10-05 17:13 +0100
Re: Learning functions; roots of the quadratic; two return values Barry Schwarz <schwarzb@dqel.com> - 2015-10-03 15:37 -0700
Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-05 23:44 +0200
Re: Learning functions; roots of the quadratic; two return values Geoff <geoff@invalid.invalid> - 2015-10-05 18:37 -0700
Re: Learning functions; roots of the quadratic; two return values Barry Schwarz <schwarzb@dqel.com> - 2015-10-05 19:24 -0700
Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-06 07:36 +0200
Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-06 07:34 +0200
Page 5 of 6 — ← Prev page 1 2 3 4 [5] 6 Next page →
| From | supercat@casperkitty.com |
|---|---|
| Date | 2015-10-06 16:31 -0700 |
| Message-ID | <ae21409f-fb21-4bb6-96a7-caf96efb03e5@googlegroups.com> |
| In reply to | #72095 |
On Tuesday, October 6, 2015 at 6:19:05 PM UTC-5, glen herrmannsfeldt wrote: > I might in C write: > > d=(complicated, but still fits on one line expression); > d *= d; > > I don't find the alternatives more readable, even when I > agree that it uses one variable for two different things. I tend to prefer assigning a "throwaway" variable name for the first line, but wish more languages would support a concept I've seen in a number of assemblers which would regard variables starting with some character (e.g. @) as temporaries which would become undefined (allowing their names to be reused) each time the assembler encountered certain directives. In some cases, one can accomplish something similar in C by creating a new scoping block, but there's no way to initialize a new variable with values stored in temporaries, since any scoping block which bounds the scope of the temporaries would likewise bound the scope of the variable one is trying to create. Having a separate purely-linear concept of scope which was separate from normal variable scoping could be quite helpful (while there wouldn't be any particular hard-and-fast rule as to how many "throwaway" variables one could have in scope at once, a desire to nest lifetimes would suggest that the outer variables aren't really "throwaways".
[toc] | [prev] | [next] | [standalone]
| From | glen herrmannsfeldt <gah@ugcs.caltech.edu> |
|---|---|
| Date | 2015-10-07 00:38 +0000 |
| Message-ID | <mv1pir$273$1@speranza.aioe.org> |
| In reply to | #72097 |
supercat@casperkitty.com wrote: (snip on temporary variable names) > I tend to prefer assigning a "throwaway" variable name for the first > line, but wish more languages would support a concept I've seen in a > number of assemblers which would regard variables starting with some > character (e.g. @) as temporaries which would become undefined (allowing > their names to be reused) each time the assembler encountered certain > directives. I have known assemblers that allowed local labels for branch targets, and I suppose they could be used this way, too. I remember some DEC assemblers that allow such that go away with the next regular symbol. I forget now if the form is 1$ or $1, but they are some special character and one digit. > In some cases, one can accomplish something similar in C by creating a new > scoping block, but there's no way to initialize a new variable with values > stored in temporaries, since any scoping block which bounds the scope of > the temporaries would likewise bound the scope of the variable one is > trying to create. I suppose, but it is starting to make the problem worse. Adding nesting levels just for temporary variables seems excessive. In Fortran 66, expressions are not allowed in the I/O list of WRITE statements, or for DO statements. Both of those caused need for otherwise unneeded temporaries. I sometimes wonder about C's for, and something like: for(i=1; i<sqrt(n); i++) ... where the sqrt, as written, is evaluated each time. Fortran and PL/I evaluate the loop limit and increment once, and use that value each time. Some might use a temporary variable for the C loop limit, instead of a complicated expression. -- glen
[toc] | [prev] | [next] | [standalone]
| From | supercat@casperkitty.com |
|---|---|
| Date | 2015-10-06 21:19 -0700 |
| Message-ID | <44a22acb-93ef-44ab-8967-a33eb50ce3c6@googlegroups.com> |
| In reply to | #72105 |
On Tuesday, October 6, 2015 at 7:39:01 PM UTC-5, glen herrmannsfeldt wrote: > I have known assemblers that allowed local labels for branch > targets, and I suppose they could be used this way, too. The main usage was for branch targets, but some allowed such labels to be used with "equ" as well. If I recall, at least one compiler cleared all such labels whenever a branch label was followed by a double colon. In any case, the main goal is to be able to use some throw-away variables in the calculation of longer-lived variable, without having to extend the scope of the throw-away variables to that of the real one. Even if a compiler could optimize the lifetimes of the throw-away variables, it would be better to have them go away semantically as well, to ensure that e.g. a typo or copy/paste mistake won't cause their value to be accidentally used later when something else is intended. > I suppose, but it is starting to make the problem worse. Adding > nesting levels just for temporary variables seems excessive. The idea would be to avoid creating nesting levels for identifiers whose purpose is primarily common sub-expression elimination. > In Fortran 66, expressions are not allowed in the I/O list of > WRITE statements, or for DO statements. Both of those caused > need for otherwise unneeded temporaries. > > I sometimes wonder about C's for, and something like: C's "for" statement was a simple syntax hack which allowed some common patterns to be written with two fewer lines, but didn't otherwise really add much in the way of expressiveness. Languages with a more rigid for- loop structure could avoid the need to use extra variables to store ending counts. On the other hand, if I were in charge of the language I would use "for" loops to add a semantic distinction, by having an option that would--even when compiling with options to rein in overflow semantics-- explicitly allow "for" loops to exit early in case of an overflow that would occur in the execution of the loop. IMHO there was considerable value in having somewhat-constrained overflow behavior, but there is also considerable value in allowing compilers the flexibility to adjust loop ending conditions using transforms that would be valid if integer-type variables worked like mathematical integers. In cases where integer overflow could only occur when a program receives invalid inputs, but where input may come from untrusted and possibly malicious sources, a program which is required not to expose security risks when given invalid input could not meet such a requirement of overflow could cause unconstrained Undefined Behavior, but might still be able to meet such requirements if it caused a loop to end early.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-10-06 22:15 -0400 |
| Message-ID | <mv1v4l$g9g$1@dont-email.me> |
| In reply to | #72095 |
On 10/06/2015 07:18 PM, glen herrmannsfeldt wrote:
...
> I don't remember how this started, so I will guess what came earlier.
>
>
> delta=b*b-4*a*c;
> if(delta<0) exit(123);
> delta=sqrt(delta);
> x1=(-b-delta)/(2*a);
> x2=(-b+delta)/(2*a);
>
> this give me a chance to discuss both optimization and naming.
>
> It does seem that the greek letter delta is often used for
> the determinant in math, as mathematicians still don't know
> that variable names can have more than one letter.
>
> (As I understand it, it was Fortran that figured that out. Thanks.)
>
> So, why the line:
>
> delta=sqrt(delta); ?
>
> We can't make it sqrt(b*b-4*a*c) as that doesn't give us a
> chance to test before the sqrt. Another possibility:
>
> delta=b*b-4*a*c;
> if(delta<0) exit(123);
> x1=(-b-sqrt(delta))/(2*a);
> x2=(-b+sqrt(delta))/(2*a);
This was more like what I was thinking of:
delta = b*b-4*a*c;
if(delta < 0)
{
// Roots are complex - handle as appropriate for the application.
// Which, in my opinion, should NEVER be exit(123).
}
else
{
double sqrtdelt = sqrt(delta);
x1 = (-b-sqrtdelt))/(2*a);
x2 = (-b+sqrtdelt))/(2*z);
}
The idea that you must store sqrt(delta) in the same variable in order
to avoid making two calls to sqrt() seems quite bizarre to me. If the
complier isn't smart enough to optimize away the second sqrt(), it will
take up far more additional space on the stack than sqrtdelt does -
particularly since I would expect any modern compiler to generate
essentially the same code for my version as for the original, storing
sqrtdelt in the same memory location that used to be assigned to delta.
--
James Kuyper
[toc] | [prev] | [next] | [standalone]
| From | glen herrmannsfeldt <gah@ugcs.caltech.edu> |
|---|---|
| Date | 2015-10-07 08:45 +0000 |
| Message-ID | <mv2m3j$o1d$1@speranza.aioe.org> |
| In reply to | #72108 |
James Kuyper <jameskuyper@verizon.net> wrote:
(snip, I wrote)
>> delta=b*b-4*a*c;
>> if(delta<0) exit(123);
>> delta=sqrt(delta);
>> x1=(-b-delta)/(2*a);
>> x2=(-b+delta)/(2*a);
>> this give me a chance to discuss both optimization and naming.
(snip)
> This was more like what I was thinking of:
> delta = b*b-4*a*c;
> if(delta < 0)
> {
> // Roots are complex - handle as appropriate for the application.
> // Which, in my opinion, should NEVER be exit(123).
> }
As it wasn't important to the point in question, I decided not
to complicate the code.
But reasonably often I write a program that will only be used
once, and still want to test for things that could happen,
but don't need a big explanation. Reasonably often, I used exit().
It isn't completely obvious that the readability of the other
statements isn't different, but I think close enough.
-- glen
[toc] | [prev] | [next] | [standalone]
| From | Melzzzzz <mel@zzzzz.com> |
|---|---|
| Date | 2015-10-07 05:19 +0200 |
| Message-ID | <20151007051930.3973122b@maxa-pc> |
| In reply to | #72095 |
On Tue, 6 Oct 2015 23:18:54 +0000 (UTC) glen herrmannsfeldt <gah@ugcs.caltech.edu> wrote: > > delta=b*b-4*a*c; > if(delta<0) exit(123); > x1=(-b-sqrt(delta))/(2*a); > x2=(-b+sqrt(delta))/(2*a); > > note that this avoids the double use of delta, and is otherwise > likely more readable. But it has two calls to sqrt(), and > everyone knows that sqrt() is a slow function. Because of this, > we should hand optimize to the form above, either with one or > two variables. (not seriously) > > But okay, we should trust the compiler to figure out that only > one sqrt() is needed. And sqrt() really isn't all that slow if > we are only doing millions of sqrt(), not billions or trillions. How can compiler figure out that? In C++ there is constexpr annotation which helps compiler in such cases and other languages have similar (eg D pure), but not C.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-10-07 09:12 -0700 |
| Message-ID | <lnmvvuk68e.fsf@kst-u.example.com> |
| In reply to | #72110 |
Melzzzzz <mel@zzzzz.com> writes:
> On Tue, 6 Oct 2015 23:18:54 +0000 (UTC)
> glen herrmannsfeldt <gah@ugcs.caltech.edu> wrote:
>
>>
>> delta=b*b-4*a*c;
>> if(delta<0) exit(123);
>> x1=(-b-sqrt(delta))/(2*a);
>> x2=(-b+sqrt(delta))/(2*a);
>>
>> note that this avoids the double use of delta, and is otherwise
>> likely more readable. But it has two calls to sqrt(), and
>> everyone knows that sqrt() is a slow function. Because of this,
>> we should hand optimize to the form above, either with one or
>> two variables. (not seriously)
>>
>> But okay, we should trust the compiler to figure out that only
>> one sqrt() is needed. And sqrt() really isn't all that slow if
>> we are only doing millions of sqrt(), not billions or trillions.
>
> How can compiler figure out that? In C++ there is constexpr annotation
> which helps compiler in such cases and other languages have similar (eg
> D pure), but not C.
The behavior of the sqrt() function is defined by the standard. A
compiler can take advantage of the fact that calling sqrt() twice with
the same argument will yield the same result both times, and will not
have side effects.
--
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 | Melzzzzz <mel@zzzzz.com> |
|---|---|
| Date | 2015-10-08 00:53 +0200 |
| Message-ID | <20151008005326.47e5ac1f@maxa-pc> |
| In reply to | #72139 |
On Wed, 07 Oct 2015 09:12:33 -0700 Keith Thompson <kst-u@mib.org> wrote: > Melzzzzz <mel@zzzzz.com> writes: > > On Tue, 6 Oct 2015 23:18:54 +0000 (UTC) > > glen herrmannsfeldt <gah@ugcs.caltech.edu> wrote: > > > >> > >> delta=b*b-4*a*c; > >> if(delta<0) exit(123); > >> x1=(-b-sqrt(delta))/(2*a); > >> x2=(-b+sqrt(delta))/(2*a); > >> > >> note that this avoids the double use of delta, and is otherwise > >> likely more readable. But it has two calls to sqrt(), and > >> everyone knows that sqrt() is a slow function. Because of this, > >> we should hand optimize to the form above, either with one or > >> two variables. (not seriously) > >> > >> But okay, we should trust the compiler to figure out that only > >> one sqrt() is needed. And sqrt() really isn't all that slow if > >> we are only doing millions of sqrt(), not billions or trillions. > > > > How can compiler figure out that? In C++ there is constexpr > > annotation which helps compiler in such cases and other languages > > have similar (eg D pure), but not C. > > The behavior of the sqrt() function is defined by the standard. A > compiler can take advantage of the fact that calling sqrt() twice with > the same argument will yield the same result both times, and will not > have side effects. > Yes, I see that compiler (gcc) takes advantage of knowing contents of sqrt, but there is some weird constraint that make it generate calls for several sqrts anyway. That is it does not calls function first, rather it executes sqrt instruction directly, but then it checks result with itself (ucomisd) and if parity flag is set (unordered) it calls sqrt then checks result again and if parity calls sqrt again. This code is not present if -ffast-math or -Ofast flag is present.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-10-07 16:23 -0700 |
| Message-ID | <lnwpuyi7pr.fsf@kst-u.example.com> |
| In reply to | #72160 |
Melzzzzz <mel@zzzzz.com> writes:
> On Wed, 07 Oct 2015 09:12:33 -0700
> Keith Thompson <kst-u@mib.org> wrote:
[...]
>> The behavior of the sqrt() function is defined by the standard. A
>> compiler can take advantage of the fact that calling sqrt() twice with
>> the same argument will yield the same result both times, and will not
>> have side effects.
>
> Yes, I see that compiler (gcc) takes advantage of knowing contents of
> sqrt, but there is some weird constraint that make it generate calls
> for several sqrts anyway. That is it does not calls function first,
> rather it executes sqrt instruction directly, but then it checks
> result with itself (ucomisd) and if parity flag is set (unordered) it
> calls sqrt then checks result again and if parity calls sqrt again.
> This code is not present if -ffast-math or -Ofast flag is present.
I have no idea. The standard requires a call to the standard sqrt()
function to behave in a certain way. A compiler is free to do whatever
it likes to achieve that effect.
--
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 | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-06 02:16 +0200 |
| Message-ID | <561312cd$0$3050$426a74cc@news.free.fr> |
| In reply to | #72009 |
On 10/06/15 00:25, glen herrmannsfeldt wrote: > temp2=sqrt(temp1); > > now there are two variables so the program is much more readable. Except for the fact that "temp" is a completely meaningless name relatively to either the content or the purpose of the variables. Its only meaning is about their lifetime, an info we don't care about. Names like delta or discriminant, sqrtDelta or sqrtDiscriminant, are the kinds of names a reader wants to find here.
[toc] | [prev] | [next] | [standalone]
| From | supercat@casperkitty.com |
|---|---|
| Date | 2015-10-06 15:43 -0700 |
| Message-ID | <97848e18-b909-4eca-9b1f-15d97424b9ce@googlegroups.com> |
| In reply to | #72016 |
On Monday, October 5, 2015 at 7:16:22 PM UTC-5, regis wrote: > Except for the fact that "temp" is a completely meaningless name > relatively to either the content or the purpose of the variables. > Its only meaning is about their lifetime, an info we don't care about. In many cases, the clearest description of a temporary variable will be the value which is assigned to it. If one is creating a variable for the purpose of factoring out a common subexpression, having a short meaningless name which is readily identifiable as matching the name of the variable in the previous line will often IMHO be preferable to having a longer and more descriptive name.
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-07 02:48 +0200 |
| Message-ID | <56146bd1$0$3065$426a74cc@news.free.fr> |
| In reply to | #72093 |
On 10/07/15 00:43, supercat@casperkitty.com wrote: > In many cases, the clearest description of a temporary variable will be > the value which is assigned to it. > If one is creating a variable for the purpose of factoring out a common > subexpression, having a short meaningless name which is readily identifiable > as matching the name of the variable in the previous line will often IMHO > be preferable to having a longer and more descriptive name. You really go a long way to try to convince yourself that delta is a better name than sqrtDelta to convey the meaning of sqrt(delta)... Using delta for sqrt(delta) here is not only non-descriptive here, it's deceptive. There is zero good reason to overwrite delta here and change the meaning of the identifier. As for length, sqrtDelta is still two chars shorter than the expression sqrt(delta) it stands for and there's zero good reasons not to use it. The burden of making a message clear is up to its author, not up to its reader. Requiring both mental juggling and mental mapping from the reader, is just shifting this burden onto the reader. I'm not going to rewrite Robert C Martin's "Clean Code" here anyway...
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-10-14 14:00 -0700 |
| Message-ID | <82807743-fe62-4d22-909d-afe22df95853@googlegroups.com> |
| In reply to | #72106 |
On Wednesday, October 7, 2015 at 1:48:31 AM UTC+1, regis wrote: > > You really go a long way to try to convince yourself > that delta is a better name than sqrtDelta > to convey the meaning of sqrt(delta)... > sqrtDelta is a bit long for use in an expression. There's also the issue that is it sqrt(Delta) or sqrt(delta), and does the code have to remain correct if we multiply it by -1? Then if we're taking the square root of a number, generally the result means something.
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-14 23:23 +0200 |
| Message-ID | <561ec7e6$0$3003$426a74cc@news.free.fr> |
| In reply to | #72880 |
On 10/14/15 23:00, Malcolm McLean wrote: > On Wednesday, October 7, 2015 at 1:48:31 AM UTC+1, regis wrote: >> >> You really go a long way to try to convince yourself >> that delta is a better name than sqrtDelta >> to convey the meaning of sqrt(delta)... >> > sqrtDelta is a bit long for use in an expression. > There's also the issue that is it sqrt(Delta) or sqrt(delta), If you use camelCase for variable names, you probably never capitalize the first letter, so Delta simply does not exist, only delta exists. If you have allow both variable names delta and Delta, you probably use underscore as a word separator, in order to allow sqrt_delta and sqrt_Delta. Anyway, I was talking about the meaning of words here, not about style guides.
[toc] | [prev] | [next] | [standalone]
| From | Geoff <geoff@invalid.invalid> |
|---|---|
| Date | 2015-10-03 12:50 -0700 |
| Message-ID | <ip901b58rfi1bg51131te0k4cn2lv52mtq@4ax.com> |
| In reply to | #71692 |
On Sat, 3 Oct 2015 10:43:18 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:
>// Function to find roots of a quadratic
>float solve_quadratic (float a, float b, float c)
>{
> float x1, x2;
> float discriminant = pow(b,2) - 4 * a * c;
>
> if (discriminant < 0)
> {
> printf("Roots are imaginary\n");
> return -1;
> }
> else
> {
> x1 = (-b + approx_sqrt(discriminant)) / (2 * a);
> x2 = (-b - approx_sqrt(discriminant)) / (2 * a);
> }
>
> return x1, x2;
>}
Functions can directly return only one value. The comma operator is
not doing what you expect.
I suggest the following:
// Function to find roots of a quadratic
// Returns 1 if it has real roots, 0 if imaginary roots
int solve_quadratic (float a, float b, float c, float *r1, float *r2)
{
float discriminant = pow(b,2) - 4 * a * c;
if (discriminant < 0)
{
return 0;
}
else
{
*r1 = (-b + approx_sqrt(discriminant)) / (2 * a);
*r2 = (-b - approx_sqrt(discriminant)) / (2 * a);
}
return 1;
}
Declare float r1, r2; in main.
Change your function declaration and the function call to match.
Your call in main might look something like this:
if (solve_quadratic(a,b,c,&r1,&r2))
printf("Roots of this quadratic are: %f and %f\n", r1, r2);
else
printf("Roots are imaginary\n");
Output:
Please, enter three constants of the quadratic:
1 0 -4
Roots of this quadratic are: 2.000000 and -2.000000
Please, enter three constants of the quadratic:
1 2 3
Roots are imaginary
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-10-03 21:16 +0100 |
| Message-ID | <mupcue$otu$1@dont-email.me> |
| In reply to | #71692 |
On 03/10/2015 18:43, Alla _ wrote:
> // Function to compute an approximate square root - Newton Raphson method
> float approx_sqrt (float x)
> {
> float guess = 1;
>
> while (abs_value (pow(guess,2) / x) > 1.001 || abs_value (pow(guess,2) / x) < 0.999)
> guess = (x / guess + guess) / 2.0;
>
> return guess;
> }
Why compute an approximate square root? C has a function sqrt() to do it
directly (and probably a lot faster).
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | alla.rashitova@gmail.com |
|---|---|
| Date | 2015-10-05 09:02 -0700 |
| Message-ID | <52211346-19f5-4296-8183-1d27aa5fa5e1@googlegroups.com> |
| In reply to | #71727 |
On Sunday, October 4, 2015 at 12:16:15 AM UTC+4, Bart wrote:
> On 03/10/2015 18:43, Alla _ wrote:
>
> > // Function to compute an approximate square root - Newton Raphson method
> > float approx_sqrt (float x)
> > {
> > float guess = 1;
> >
> > while (abs_value (pow(guess,2) / x) > 1.001 || abs_value (pow(guess,2) / x) < 0.999)
> > guess = (x / guess + guess) / 2.0;
> >
> > return guess;
> > }
>
> Why compute an approximate square root? C has a function sqrt() to do it
> directly (and probably a lot faster).
>
> --
> Bartc
It is based on a task from the textbook; as one of
the examples on how to use functions, the author
used this approximate square root function for a few
exercises.
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-10-05 17:13 +0100 |
| Message-ID | <muu7f1$a2l$2@dont-email.me> |
| In reply to | #71978 |
On 05/10/15 17:02, alla.rashitova@gmail.com wrote: > On Sunday, October 4, 2015 at 12:16:15 AM UTC+4, Bart wrote: >> >> Why compute an approximate square root? C has a function sqrt() to do it >> directly (and probably a lot faster). > > It is based on a task from the textbook; as one of > the examples on how to use functions, the author > used this approximate square root function for a few > exercises. That's a good reason to do it. Another good reason is that it's important for at least some people to learn how to write such functions. If nobody learns how to do it, after 70 years or so there'll be nobody left on the planet that knows. -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-10-03 15:37 -0700 |
| Message-ID | <jhl01b9b4vlf0eclre6ebs0h3m46si8clo@4ax.com> |
| In reply to | #71692 |
On Sat, 3 Oct 2015 10:43:18 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:
>Hello!
>
>Below is a program which should have a few functions:
>
>1) the function that finds two roots of a quadratic
>2) the function to turn a value into an absolute value
>3) to find approximate square root (which uses the above
>absolute-value function) (please, note that I am following
>the material from the book, and I am not yet ready for
>sophisticated functions to find a square root)
>
>Quadratic has two roots, hence my square root function
>should return two values x1 and x2. Please, take a look
You need to go back and study the solution to a quadratic. It only
requires a single value of a square root.
Furthermore, the two possible square roots of a positive number always
have the same absolute value. They differ only in sign. You hardly
need your square root function to return both since you can generate
the second with a unary minus.
>at the program: what is my mistake, and why I don't get
>any answer? I assume one of the reasons is that I don't
>have any %-tags in my printf function, but when I add
>two %f, I get an error message from a compiler, because
>then I have only "one" argument, i.e. solve_quadratic(a,b,c).
>
>#include <stdio.h>
>#include <math.h>
>
>float abs_value (float x);
>float approx_sqrt (float x);
>float solve_quadratic (float a, float b, float c);
>
>// The main function prompts the user for 3 constant values to fill a quadratic
>// ax^2 + bx + c
The mathematical term for a, b, and c is "coefficient". Thinking of
them as constant can lead to false assumptions and poor programming.
>int main(void)
>{
> float a, b, c;
>
> printf("Please, enter three constants of the quadratic: \n");
>
> if (scanf ("%f %f %f", &a, &b, &c) == 3)
> printf("Square roots of this quadratic are: %f and %f\n", solve_quadratic(a,b,c));
Quadratics don't have square roots (algebraically they can but that is
not what you are computing). You are computing the roots (sometimes
called the solutions) of the quadratic.
> return 0;
>}
>
>// Function to take an absolute value of x that is used further in square root function
>float abs_value (float x)
>{
> if (x < 0)
> x = - x;
> return x;
>}
>
>// Function to compute an approximate square root - Newton Raphson method
>float approx_sqrt (float x)
>{
> float guess = 1;
>
> while (abs_value (pow(guess,2) / x) > 1.001 || abs_value (pow(guess,2) / x) < 0.999)
> guess = (x / guess + guess) / 2.0;
>
> return guess;
>}
>
>// Function to find roots of a quadratic
>float solve_quadratic (float a, float b, float c)
>{
> float x1, x2;
> float discriminant = pow(b,2) - 4 * a * c;
>
> if (discriminant < 0)
> {
> printf("Roots are imaginary\n");
> return -1;
> }
> else
> {
> x1 = (-b + approx_sqrt(discriminant)) / (2 * a);
> x2 = (-b - approx_sqrt(discriminant)) / (2 * a);
It doesn't pay to call approx_sqrt twice when it must return the same
value both times.
> }
>
> return x1, x2;
Others have addressed this.
>}
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Rosario19 <Ros@invalid.invalid> |
|---|---|
| Date | 2015-10-05 23:44 +0200 |
| Message-ID | <c8r51bluu1rp9us6fk348lg4elm61skgnq@4ax.com> |
| In reply to | #71692 |
On Sat, 3 Oct 2015 10:43:18 -0700 (PDT), Alla _ wrote:
>Hello!
>
>Below is a program which should have a few functions:
>
>1) the function that finds two roots of a quadratic
>2) the function to turn a value into an absolute value
>3) to find approximate square root (which uses the above
>absolute-value function) (please, note that I am following
>the material from the book, and I am not yet ready for
>sophisticated functions to find a square root)
i'm not agree with point 3 when it said one has to call one function
for absolute value...
>Quadratic has two roots,
not always what about a=0? what about delta<0? what about a=b=c=0?
what about a=b=0 and c!=0?
>hence my square root function
>should return two values x1 and x2. Please, take a look
>at the program: what is my mistake, and why I don't get
>any answer? I assume one of the reasons is that I don't
>have any %-tags in my printf function, but when I add
>two %f, I get an error message from a compiler, because
>then I have only "one" argument, i.e. solve_quadratic(a,b,c).
>
>#include <stdio.h>
>#include <math.h>
>
>float abs_value (float x);
>float approx_sqrt (float x);
>float solve_quadratic (float a, float b, float c);
>
>// The main function prompts the user for 3 constant values to fill a quadratic
>// ax^2 + bx + c
>int main(void)
>{
> float a, b, c;
>
> printf("Please, enter three constants of the quadratic: \n");
>
> if (scanf ("%f %f %f", &a, &b, &c) == 3)
> printf("Square roots of this quadratic are: %f and %f\n", solve_quadratic(a,b,c));
>
> return 0;
>}
>
>// Function to take an absolute value of x that is used further in square root function
>float abs_value (float x)
>{
> if (x < 0)
> x = - x;
> return x;
>}
>
>// Function to compute an approximate square root - Newton Raphson method
>float approx_sqrt (float x)
>{
> float guess = 1;
>
> while (abs_value (pow(guess,2) / x) > 1.001 || abs_value (pow(guess,2) / x) < 0.999)
> guess = (x / guess + guess) / 2.0;
this for me it is wrong... so you for stop sqrt use pow the inverse?
> return guess;
>}
>
>// Function to find roots of a quadratic
>float solve_quadratic (float a, float b, float c)
>{
> float x1, x2;
> float discriminant = pow(b,2) - 4 * a * c;
>
> if (discriminant < 0)
> {
> printf("Roots are imaginary\n");
> return -1;
> }
> else
for me if a==0 this below goes to seg fault
> {
> x1 = (-b + approx_sqrt(discriminant)) / (2 * a);
> x2 = (-b - approx_sqrt(discriminant)) / (2 * a);
> }
>
> return x1, x2;
>}
>
>Thank you!
this is my obfuscate [for you] solution
#include <stdio.h>
#include <stdlib.h>
#include "libreria.h"
// bcc32 -v thisfile.c libreria.lib
void errore(void)
{printf("Errore di input Esco\n"); exit(0);}
f64 mySqrt(f64 a)
{f64 r, t, v;
t=1;
l0: r=t;
t=(r+(a/r))/2.0;
v=t-r;
GG(!(-0.1e-10<v&&v<0.1e-10), l0); // 10 cifre precisione
R t;
}
// "x" has to be one pointer to double that point
// one array of 2 double at last
// ax^2+bx+c=0
// return 0 for 0 solutions
// return 1 for one solution find write in x[0]
// return 2 for 2 solutions find write in x[0] x[1]
// return 3 for infinite solutions
// return -1 for error
// overflow not detect in error case
i32 solveQudratic(f64* x, f64 a, f64 b, f64 c)
{f64 delta, sqDelta;
if(x==0) R -1;
x[0]=0; x[1]=0;
if(-0.1e-10<a&&a<0.1e-10)
{if(-0.1e-10<b&&b<0.1e-10)
{if(-0.1e-10<c&&c<0.1e-10)
R 3;
else R 0;
}
x[0]=-c/b; R 1;
}
delta=(b*b)-(4*a*c);
if(delta<0) R 0;
sqDelta=mySqrt(delta);
x[0] =(-b- sqDelta)/ (2*a);
x[1] =(-b+ sqDelta)/ (2*a);
R 2;
}
int main(void)
{f64 a, b, c, x[2], t;
i32 r;
P("Programma che risolve l'equazione ax^2+bx+c=0\n");
P("Digitare per a=b=c=0 per finire il ciclo\n");
l0: a=GetDouble("Inserisci a: ");
if(a==DBL_MIN) errore();
b=GetDouble("Inserisci b: ");
if(b==DBL_MIN) errore();
c=GetDouble("Inserisci c: ");
if(c==DBL_MIN) errore();
r=solveQudratic(x, a, b, c);
if(r==-1) errore();
if(r==3){P("Infinite soluzioni\n"); G end;}
if(r==0) P("Nessuna soluzione\n");
if(r==1) P("Una sola soluzione: x[0]=%.10f\n", x[0]);
if(r==2) P("Due soluzioni: x[0]=%.10f, x[1]=%.10f\n", x[0], x[1]);
if(r>=1) {t=(a*x[0]*x[0])+b*x[0]+c;
if(!(-0.1e-10<t&&t<0.1e-10))
P("La soluzione x[0] si discosta\n");
else P("La soluzione x[0] ok\n");
}
if(r== 2){t=(a*x[1]*x[1])+b*x[1]+c;
if(!(-0.1e-10<t&&t<0.1e-10))
P("La soluzione x[1] si discosta\n");
else P("La soluzione x[1] ok\n");
}
G l0;
end:
R 0;
}
[toc] | [prev] | [next] | [standalone]
Page 5 of 6 — ← Prev page 1 2 3 4 [5] 6 Next page →
Back to top | Article view | comp.lang.c
csiph-web