Logarithm of a BigInt

In case you don’t want to return a BigInt, then the following might work for you too: function log10(bigint) { if (bigint < 0) return NaN; const s = bigint.toString(10); return s.length + Math.log10(“0.” + s.substring(0, 15)) } function log(bigint) { return log10(bigint) * Math.log(10); } function natlog(bigint) { if (bigint < 0) return NaN; … Read more

What is the complexity of the log function?

This really depends on the domain of what values you want to compute a logarithm of. For IEEE doubles, many processors can take logarithms in a single assembly instruction; x86 has the FYL2X and FYL2XP1 instructions, for example. Although typically instructions like these will only take the logarithm in some fixed base, they can be … Read more

numerically stable way to multiply log probability matrices in numpy

logsumexp works by evaluating the right-hand side of the equation log(∑ exp[a]) = max(a) + log(∑ exp[a – max(a)]) I.e., it pulls out the max before starting to sum, to prevent overflow in exp. The same can be applied before doing vector dot products: log(exp[a] ⋅ exp[b]) = log(∑ exp[a] × exp[b]) = log(∑ exp[a … Read more

Logarithm Algorithm

Use this identity: logb(n) = loge(n) / loge(b) Where log can be a logarithm function in any base, n is the number and b is the base. For example, in Java this will find the base-2 logarithm of 256: Math.log(256) / Math.log(2) => 8.0 Math.log() uses base e, by the way. And there’s also Math.log10(), … Read more

Logarithm of a BigDecimal

Java Number Cruncher: The Java Programmer’s Guide to Numerical Computing provides a solution using Newton’s Method. Source code from the book is available here. The following has been taken from chapter 12.5 Big Decimal Functions (p330 & p331): /** * Compute the natural logarithm of x to a given scale, x > 0. */ public … Read more

Convert Linear scale to Logarithmic

Notation As is the convention both in mathematics and programming, the “log” function is taken to be base-e. The “exp” function is the exponential function. Remember that these functions are inverses we take the functions as: exp : ℝ → ℝ+, and log : ℝ+ → ℝ. Solution You’re just solving a simple equation here: … Read more

Logarithmic y-axis bins in python

try plt.yscale(‘log’, nonposy=’clip’) http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.yscale The issue is with the bottom of bars being at y=0 and the default is to mask out in-valid points (log(0) -> undefined) when doing the log transformation (there was discussion of changing this, but I don’t remember which way it went) so when it tries to draw the rectangles for … Read more