pow function working unexpectedly
herriojr at gmail.com
Tue Apr 11 23:37:01 UTC 2006
Yes, thank you. I wasn't going to report it as a bug after you
pointed out my blatant mistake. Thanks again for the info.
On 4/11/06, Nicolas Blais <nb_root at videotron.ca> wrote:
> On Tuesday 11 April 2006 19:24, Colin Percival wrote:
> > Andy Reitz wrote:
> > > So, clearly, something is optimizing the pow() function away when the
> > > arguments are hard-coded lvalues, instead of varibles.
> > >
> > > Now, what that thing *is*, I don't know.
> > The C compiler precomputes constant expressions; your "pow(2,3)" is
> > being rewritten to "8" by the compiler. Similarly, if you write
> > "1 + 2 / 3 + 4 * 5 - 6", the C compiler will turn this into "15"
> > rather than producing a series of instructions which computes the
> > expression.
> > When you reference variables, this optimization isn't possible, since
> > those variables might be modified before you reach the line where
> > they are used. (Obviously this doesn't happen in your program, but
> > the compiler isn't smart enough to figure that out.)
> > Colin Percival
> +1. The compiler sees constants differently from variables (which reside in
> memory space). With the constant, the optimizer can rewrite the equation
> its answer.
> Anyhow, it can't be seen as a bug because you should include the math
> whenever you're using a function from it (see man pow). Therefore, if you
> would have included the library in the first place, you would have never
> found this 'oddity'.
> FreeBSD 7.0-CURRENT #1: Sat Apr 8 21:33:25 EDT 2006
> root at clk01a:/usr/obj/usr/src/sys/CLK01A
> PGP? : http://www.clkroot.net/security/nb_root.asc
More information about the freebsd-questions