## Why does math.log result in ValueError: math domain error?

Your code is doing a log of a number that is less than or equal to zero. That’s mathematically undefined, so Python’s log function raises an exception. Here’s an example: >>> from math import log >>> log(-1) Traceback (most recent call last): File “<pyshell#59>”, line 1, in <module> log(-1) ValueError: math domain error Without knowing … Read more

## Plot logarithmic axes

You can use the Axes.set_yscale method. That allows you to change the scale after the Axes object is created. That would also allow you to build a control to let the user pick the scale if you needed to. The relevant line to add is: ax.set_yscale(‘log’) You can use ‘linear’ to switch back to a … Read more

## Custom logarithmic axis scaling in matplotlib

The scale consists of two Transform classes, each of which needs to provide a transform_non_affine method. One class needs to transform from data to display coordinates, which would be log(a+1), the other is the inverse and needs to transform from display to data coordinates, which would in this case be exp(a)-1. Those methods need to … Read more

## Logarithm for BigInteger

If you want to support arbitrarily big integers, it’s not safe to just do Math.log(bigInteger.doubleValue()); because this would fail if the argument exceeds the double range (about 2^1024 or 10^308, i.e. more than 300 decimal digits ). Here’s my own class that provides the methods double logBigInteger(BigInteger val); double logBigDecimal(BigDecimal val); BigDecimal expBig(double exponent); BigDecimal … 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

## How do you calculate log base 2 in Java for integers?

This is the function that I use for this calculation: public static int binlog( int bits ) // returns 0 for bits=0 { int log = 0; if( ( bits & 0xffff0000 ) != 0 ) { bits >>>= 16; log = 16; } if( bits >= 256 ) { bits >>>= 8; log += … Read more

## ValueError: math domain error

Your code is doing a log of a number that is less than or equal to zero. That’s mathematically undefined, so Python’s log function raises an exception. Here’s an example: >>> from math import log >>> log(-1) Traceback (most recent call last): File “<pyshell#59>”, line 1, in <module> log(-1) ValueError: math domain error Without knowing … Read more

## How can I specify the base for Math.log() in JavaScript?

“Change of Base” Formula / Identity The numerical value for logarithm to the base 10 can be calculated with the following identity. Since Math.log(x) in JavaScript returns the natural logarithm of x (same as ln(x)), for base 10 you can divide by Math.log(10) (same as ln(10)): function log10(val) { return Math.log(val) / Math.LN10; } Math.LN10 … Read more

## What would cause an algorithm to have O(log n) complexity?

I have to agree that it’s pretty weird the first time you see an O(log n) algorithm… where on earth does that logarithm come from? However, it turns out that there’s several different ways that you can get a log term to show up in big-O notation. Here are a few: Repeatedly dividing by a … Read more

## Histogram with Logarithmic Scale and custom breaks

A histogram is a poor-man’s density estimate. Note that in your call to hist() using default arguments, you get frequencies not probabilities — add ,prob=TRUE to the call if you want probabilities. As for the log axis problem, don’t use ‘x’ if you do not want the x-axis transformed: plot(mydata_hist\$count, log=”y”, type=”h”, lwd=10, lend=2) gets … Read more