Math.log2 applied to powers of 2
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.)
On Thu, Sep 18, 2014 at 10:56 AM, Tab Atkins Jr. <jackalmage at gmail.com>
wrote:
On Thu, Sep 18, 2014 at 3:17 AM, Claude Pache <claude.pache at gmail.com> wrote:
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.)
It would also be useful, though perhaps not feasible, if they are guaranteed to be monotonic everywhere and strictly monotonic near those exact values; that is,
for all x < 2^k, log2(x) < k for all x > 2^k, log2(x) > k
and similarly for log10. If this property held, then naïve "number of digits" tests expressed using logarithms would always give the right answers. (This probably conflicts with generally desirable rounding properties, however.)
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.
If you can, please file a bug against v8: code.google.com/p/v8/issues/entry
I consider these to be bugs in the implementation.
On 9/18/14, 2:12 PM, Kevin Reid wrote:
for all x < 2^k, log2(x) < k for all x > 2^k, log2(x) > k
I'm not sure this is viable.
Consider, for example k == 960, x = 2^960 + 2^908.
The exact value of log2(x) would be
960 + log2(1 + 2^-52) = 960 + ln(1+2^-52)/ln(2) < 960 + 1/ln(2) * 2^-52 < 960 + 2^-51
where the two inequalities follow from ln(1+x) < x for small x and ln(2)
0.5.
But 960 + 2^-51, represented as an IEEE double, is 960, because we only have about 43-44 digits of mantissa precision left for the non-integral part of this number...
In fact, the smallest representable double larger than 960 is 960 + 2^-43, as far as I can tell. So to get the property you want, we'd need to have log2(x) equal to that for 2^960 < x < 2^960 + 2^917 at the very least, which is quite a bunch of doubles, for many of which the exact log2 value is definitely closer to 960 than to 960+2^-43.
(This probably conflicts with generally desirable rounding properties, however.)
If by that you mean the argument above, I clearly agree. ;)
On Thu, Sep 18, 2014 at 11:40 AM, Raymond Toy <toy.raymond at gmail.com> wrote:
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.
If you can, please file a bug against v8: code.google.com/p/v8/issues/entry
I consider these to be bugs in the implementation.
Done, thanks: code.google.com/p/v8/issues/detail?id=3579
Just tried in the console of Chrome (with "Experimental JavaScript features" flag enabled).
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.