Questions tagged [logarithm]

The logarithm of a number is the exponent by which another fixed value, the base, has to be raised to produce that number. It has applications in algebra and complexity theory. It is usually denoted as log in programming languages. Use this tag for any programming questions involving logarithms.

A base-b logarithm of a number represents the exponent that b must be raised to to obtain the original number. Namely, for a number n, it is like asking the question "b to which power equals n?"

Given a number n

  • Express n = bx for some base b, and some exponent x.
  • Then, the base-b logarithm of n is the exponent x. logb(n) = x.

The three most important bases of a logarithm is the base-2 logarithm, prevalent in computer science, the base-10 logarithm similar to counting the number of decimal places, and the natural (base-e) logarithm with unique mathematical properties.

The notations of the logarithm of a number n for different bases is

  • logb(n) for a given base b
  • log(n), which may mean the base-10 logarithm, the natural logarithm, or a generic logarithm
  • ln(n), which is often but not always used for the natural logarithm.

Furthermore, the base of the logarithm can be changed using a simple formula involving a ratio of logarithms. In fact, logarithms of base c is expressed as a constant multiple of a logarithm of base b.

  • logc(n) = logb(n)/logb(c)

Most of the time, the logarithm is an irrational number that cannot be expressed exactly in terms of decimals, let alone floating-point numbers.

Logarithm in programming languages

Unlike simple additive and multiplicative operators, and in some cases, the exponent, using the logarithm for a given programming language is usually used as a particular built-in library method. The logarithm usually accepts floating-point inputs strictly greater than zero and returns the approximate logarithm of the number, usually the natural logarithm.

Furthermore, note that the name log(...) often refers to the natural logarithm in the context of programming languages, rather than ln(...). The base-10 log is usually named log10(...).

Languages which support a built-in base-b logarithm passes through two arguments, and may either be of the form log(x, b) or log(b, x) for a number to be evaluated x and base b.

Usages in various programming languages (base-e)

  • C: log(double x), logf(float x), logl(long double x) (#include <math.h>)
  • C++: std::log(x) (#include <cmath>)
  • Java: java.lang.Math.log(double x)
  • JavaScript: Math.log(x) (do not confuse with console.log)
  • Python: log(x), log(x, b) for base-b logarithm (import log from math)
  • Rust: ln(self: f64) -> f64, (log10 and log2 for bases 10, 2)
  • Fortran: LOG(x)
  • MySQL: LOG(x), LOG(b, x) for base-b logarithm of a number x
  • Excel: LN(x), LOG(x, b) for base-b

Applications of logarithms

The logarithm is the inverse of exponentiation, namely that logb(bn), blogb(n) are identities that equal the original number n.

Logarithms are important in many mathematical fields, especially those involving variables differing by many orders of magnitude, logarithmic axes, and solving formulas involving exponential expressions. It is also a way to interpolate the number of digits (of some base b) of a given number: the number of decimal digits required for a positive integer n is ⌊log10(n)⌋.

The logarithm also represents a major complexity class in algorithmic complexity theory. Logarithmic complexity represents time or space complexity of O(log(n)) for an input of size n. Logarithmic growth is very slow, and is asymptotically slower than any power function nc where c is strictly positive including the linear function. Therefore, logarithmic complexity is very efficient, and is considered to be in polynomial time.

These algorithms are all logarithmic-time:

  • Binary Search
  • BST Insertion (average case)

There is also another major complexity class called linearithmic complexity, which represents O(n * log(n)) complexity. It usually occurs when a logarithmic-complexity process is executed n times. It is asymptotically slower than any power function nc where c > 1.

These algorithms are all in linearithmic, or n-log-n time.

  • Quicksort (average case)
  • Listing all elements in a BST (average case)

Read more

Documentation

Tags

  • : DO NOT use this tag for logarithms. The word "log" is also used to refer to the action of logging. That tag is for logarithms and logarithmic concepts only.
  • : DO NOT use this tag for the natural logarithm. That refers to the link command. Instead, use .
  • , : Inverses of the logarithm function
  • : Refers to the natural exponent, the inverse of log(...).
    Namely, log(exp(x)) == exp(log(x)) == x for x > 0.
  • , , , : The logarithm is an important class of algorithmic complexity. It is very efficient, much more efficient than linear, but is lesser than constant.

External links

1026 questions
6
votes
2 answers

What is the fastest way to find if a large integer is power of ten?

I could just use division and modulus in a loop, but this is slow for really large integers. The number is stored in base two, and may be as large as 2^8192. I only need to know if it is a power of ten, so I figure there may be a shortcut (other…
warren
  • 563
  • 2
  • 13
6
votes
1 answer

Which of the growth rates log(log *n) and log*(log n) is faster?

As n gets large, of the two functions log*(log n) and log(log* n) will will be faster? Here, the log* function is the iterated logarithm, defined here: I suspect these are the same, just written differently, but is there any difference between…
user97452
  • 61
  • 1
  • 3
6
votes
1 answer

Negative axis in a log plot

I'm plotting a log plot using matplotlib. My values go from 1 to 35. fig=plt.figure(figsize=(7,7)) fig.subplots_adjust(top=0.75, right=0.9) ax=fig.add_subplot(111) ax.plot(x, y, marker='o', color='black',…
Marika Blum
  • 907
  • 2
  • 8
  • 7
6
votes
1 answer

Taking logs and adding versus multiplying

If I want to take the product of a list of floating point numbers, what's the worst-case/average-case precision lost by adding their logs and then taking exp of the sum as opposed to just multiplying them. Is there ever a case when this is actually…
dspyz
  • 5,280
  • 2
  • 25
  • 63
6
votes
3 answers

Transforming variable density on log scale with R

I want to plot the density of variable whose range is the following: Min. :-1214813.0 1st Qu.: 1.0 Median : 40.0 Mean : 303.2 3rd Qu.: 166.0 Max. : 1623990.0 The linear plot of the density results in a tall…
Mulone
  • 3,603
  • 9
  • 47
  • 69
6
votes
1 answer

Logarithmic yscale in imshow

Possible Duplicate: Plot logarithmic axes with matplotlib in python I have a 50*1050 matrix in which the dimension 50 represents the frequency and 1050 the time. I tried to plot it using imshow and I get this image: But i want to highlight the…
Mojtaba
  • 249
  • 6
  • 14
6
votes
2 answers

Highcharts - best way to handle and display zero (or negative) values in a line chart series with logarithmic Y axis

In my HighChart line graphs, the series data is being fed from my Ruby on Rails application dynamically. Sometimes the series values are zeros or less which is a problem for HighCharts and it throws the following exception: Highcharts Error…
Bharat
  • 2,409
  • 6
  • 32
  • 57
5
votes
2 answers

How computers calculate logarithm?

I wanted to know how computers calculate logarithms? I don't mean the related functions. For example, Python uses math.log() function. But I want to know what exactly does this function do? And can it be simulated again and more accurately? Is there…
5
votes
5 answers

What is the correct algorthm for a logarthmic distribution curve between two points?

I've read a bunch of tutorials about the proper way to generate a logarithmic distribution of tagcloud weights. Most of them group the tags into steps. This seems somewhat silly to me, so I developed my own algorithm based on what I've read so…
dburke
  • 1,236
  • 2
  • 11
  • 15
5
votes
1 answer

What is the time complexity of math.log2(x) in Python 3?

As the title says, I would like to know what is the time complexity of math.log2(x). I know that it is possible to write such a function in C in O(1) complexity, but I could not find any information about the implementation of this function in…
Saviour
  • 302
  • 2
  • 11
5
votes
5 answers

What does Logn actually mean?

I am just studying for my class in Algorithms and have been looking over QuickSort. I understand the algorithm and how it works, but not how to get the number of comparisons it does, or what logn actually means, at the end of the day. I understand…
5
votes
1 answer

Log2 approximation in fixed-point

I'v already implemented fixed-point log2 function using lookup table and low-order polynomial approximation but not quite happy with accuracy across the entire 32-bit fixed-point range [-1,+1). The input format is s0.31 and the output format is…
Ali
  • 288
  • 3
  • 10
5
votes
1 answer

Robustly and accurately computing natural logarithm of quotient of two floating-point numbers

One obvious problem when computating log (a/b), where a and b are two non-zero positive finite floating-point operands of a given precision (here called the native precision), is that the quotient a/b may not be representable as a floating-point…
njuffa
  • 23,970
  • 4
  • 78
  • 130
5
votes
3 answers

Algorithm behind Math.log - Java

Lately I used the Math.log() to code some program and now I ask myself how does the method log() work. How does the computer calculate the logarithm? Thanks for solution.
tzuxi
  • 117
  • 1
  • 9
5
votes
0 answers

Most efficient way to take log of numpy array but ignore 0's

I'd like to take the natural logarithm of a numpy array but not calculate the logarithm of 0-entries at all. I.e. I'd like to implement the convention log(0)=0 over numpy arrays. import numpy as np import timeit foo = np.random.rand(500) %timeit…
Chris
  • 721
  • 1
  • 10
  • 23