B
blmblm
Maybe a style question, though there could be efficiency issues
too .... :
I teach beginning/intermediate programming to university
students, and I've noticed that they often want to use pow() to
compute small integer powers (squares, e.g.) where I would use
explicit multiplication. Is there a "best practice" on this one?
I'm thinking that the tradeoffs are something like the following:
Explicit multiplication might be more efficient -- but a smart
enough compiler might transform pow(x, 2) into x*x.
Using pow() might be clearer, and if the thing to be raised to a
power is an expression, it avoids writing the expression twice,
which makes the code shorter and avoids a source of possible error.
I've almost convinced myself here that the students are right,
but to me explicit multiplication still seems more idiomatic.
Am I completely off-base here, perhaps having developed bad habits
in an era in which compilers were less smart?
too .... :
I teach beginning/intermediate programming to university
students, and I've noticed that they often want to use pow() to
compute small integer powers (squares, e.g.) where I would use
explicit multiplication. Is there a "best practice" on this one?
I'm thinking that the tradeoffs are something like the following:
Explicit multiplication might be more efficient -- but a smart
enough compiler might transform pow(x, 2) into x*x.
Using pow() might be clearer, and if the thing to be raised to a
power is an expression, it avoids writing the expression twice,
which makes the code shorter and avoids a source of possible error.
I've almost convinced myself here that the students are right,
but to me explicit multiplication still seems more idiomatic.
Am I completely off-base here, perhaps having developed bad habits
in an era in which compilers were less smart?