Jesper de Jong wrote:It's just a guess, but it could be this: Math.pow() works on floating-point numbers of type double. The data type double has limited precision. You might get rounding errors which will give you a different result than when you'd use integer computations.
Campbell Ritchie wrote:Your use of left shift and - may be what the * 31 bit is optimised to.
Campbell Ritchie wrote:and you showed a similar algorithm with a premature optimisation combining << and -.
Campbell Ritchie wrote:I thought Bloch says it is probably optimised to h << 5 - h. But you cannot be sure that is how it is optimised. Anyway, as Brian Goetz reminds us, “write dumb code”. If there is such an optimisation for * 31, it will be used anyway. If, however, somebody ever invents a better optimisation, then using h << 5 - h will miss that optimisation. So trying to micro‑optimise in code like that is unnecessary in my opinion.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |