Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Learning functions; roots of the quadratic; two return values Date: Wed, 07 Oct 2015 09:12:33 -0700 Organization: None to speak of Lines: 34 Message-ID: References: <623fef72-989a-4973-9d04-b1a867a00703@googlegroups.com> <5611ccfd$0$3060$426a34cc@news.free.fr> <5613071A.2070609@verizon.net> <20151007051930.3973122b@maxa-pc> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="8328"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4AkNxeJEm3+PAm/7wMjTr" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:AOxi0JxShThai4IXWzbZ8j+H804= sha1:DqhnrZFros8zEQDmX6jUDOXozZg= Xref: csiph.com comp.lang.c:72139 Melzzzzz writes: > On Tue, 6 Oct 2015 23:18:54 +0000 (UTC) > glen herrmannsfeldt 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 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"