Math.log2 applied to powers of 2

Tab Atkins Jr. jackalmage at gmail.com
Thu Sep 18 10:56:59 PDT 2014


On Thu, Sep 18, 2014 at 3:17 AM, Claude Pache <claude.pache at gmail.com> wrote:
> Just tried in the console of Chrome (with "Experimental JavaScript features"
> flag enabled).
>
>     > Math.log2(8)
>     < 2.9999999999999996
>
> Firefox gives me the correct answer (3).
>
> Question: Should Math.log2 give exact results for powers of 2?
>
> The same issue holds for Math.log10 (might be applicable for nonnegative
> powers only): Math.log10(1e15) != 15 in Chrome.

I have no idea of the computation complexity underlying a log
implementation, so given that: yes, it should totally give exact
results for powers of 2, and log10 should do the same for (positive)
powers of 10.  (Negative powers of 10 can't actually be represented by
a JS number, so there's no need to talk about them.)

~TJ


More information about the es-discuss mailing list