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
7
votes
3 answers

Branch-free implementation of f(x) := if x == 0 then 0 else (x * log(x))

I have this C function: double f(int x) { if (x <= 0) return 0.0; else return x * log(x); } which I am calling in a tight loop, and would like to get rid of the branch to see if it improves performance. I cannot use…
finnw
  • 47,861
  • 24
  • 143
  • 221
7
votes
4 answers

T(n)=2T(n−−√)+logn

I am trying to find the time complexity for the recurrence: T(n) = 2T(n1/2) + log n I am pretty close to the solution, however, I have run into a roadblock. I need to solve: n(1/2k) = 1 for k to simplify my substitution pattern. I am not…
Waqas
  • 311
  • 1
  • 3
  • 10
6
votes
2 answers

How can you perform varying base logarithmic functions in Javascript?

This problem is being asked with a node.js server in mind, but I stated the question as "javascript" because I will likely use this same logic for a client-side script, as well. Here's the problem: given a set of x values, y needs to scale in a…
Aejay
  • 909
  • 9
  • 19
6
votes
3 answers

Why does the integer representation of a floating point number offer a piecewise linear approximation to the logarithm?

If you were reading news about developments in graphics in the 1990s, you might have followed Jim Blinn's column in IEEE Computer Graphics & Applications, "Jim Blinn's corner." In the summer of 1997 you would have opened your issue to a column…
Adam Hyland
  • 878
  • 1
  • 9
  • 21
6
votes
2 answers

Numeric function for log of sum in Python

Given log(a) and log(b), I want to compute log(a+b) (in a numerically stable way). I wrote a little function for this: def log_add(logA,logB): if logA == log(0): return logB if logA
user
  • 7,123
  • 7
  • 48
  • 90
6
votes
1 answer

Optimized integer logarithm base2 for BigInt

This is the naive algorithm that calculates integer log2(n), function ilog2(n) { // n is a positive non-zero BigInt const C1 = BigInt(1) const C2 = BigInt(2) for(var count=0; n>C1; count++) n = n/C2 return count } //…
Peter Krauss
  • 13,174
  • 24
  • 167
  • 304
6
votes
4 answers

Calculating the largest int less than the base 2 log of N

I have been reading Algorithms, 4th Edition, and it defines a question as follows: Write a static method lg() that takes an int value N as an argument and returns the largest int not larger than the base-2 logarithm of N in Java. Do not use…
user6233283
6
votes
3 answers

Python - how to convert a list to log(list)

I have a list of values and would like to convert it to the log of that list or pass the log of a list to a function. I'm more familiar with R and you can usually throw some () around anything. When I attempt this in Python I get the…
user974887
  • 2,309
  • 3
  • 17
  • 18
6
votes
3 answers

Quick integer logarithm for special case

I have integer values ranging from 32-8191 which I want to map to a roughly logarithmic scale. If I were using base 2, I could just count the leading zero bits and map them into 8 slots, but this is too course-grained; I need 32 slots (and more…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
6
votes
2 answers

Custom logarithmic axis scaling in matplotlib

I'm trying to scale the x axis of a plot with math.log(1+x) instead of the usual 'log' scale option, and I've looked over some of the custom scaling examples but I can't get mine to work! Here's my MWE: import matplotlib.pyplot as plt import numpy…
Arnold
  • 863
  • 5
  • 13
  • 21
6
votes
2 answers

Use UISider to generate a logarithmic scale of values

Is there a way to got the values outputted by a UISlider in an iPhone app to increase exponentially? For example the first third generate 1 -10, the second third generate 11 to 100, and the final third generate 101 to 1000?
lavelle
  • 1,446
  • 1
  • 17
  • 30
6
votes
2 answers

Logging logarithmically 1, 10, 100, 1000, etc

Is there a more efficient way of doing the following, something just feels wrong about it? I'm looking for the most time efficient way of logging logarithmically. public bool Read() { long count = Interlocked.Increment(ref _count); …
Aaron Stainback
  • 3,489
  • 2
  • 31
  • 32
6
votes
3 answers

Confused about "c lg n" in Introduction to Algorithms book - what is c?

I am reading the book "Introduction to Algorithms" and I am confused by this part: We also assume a limit on the size of each word of data . For example when working with >inputs of size n , we typically assume that integers are represented by c lg…
user3731608
  • 69
  • 1
  • 2
6
votes
4 answers

Problem with arithmetic using logarithms to avoid numerical underflow (take 2)

I have two lists of fractions; say A = [ 1/212, 5/212, 3/212, ... ] and B = [ 4/143, 7/143, 2/143, ... ]. If we define A' = a[0] * a[1] * a[2] * ... and B' = b[0] * b[1] * b[2] * ... I want to calculate a normalised value of A' and B' ie…
mat kelcey
  • 3,077
  • 2
  • 30
  • 35
6
votes
3 answers

Time complexity for dependant nested for loop?

Can you explain me how to find time complexity for this? sum=0; for(k=1;k<=n;k*=2) for(j=1;j<=k;j++) sum++; So, i know the outer loop has time complexity of O(logn), but since the iterations of the inner loop depends on the value of the…
user2228135
  • 239
  • 6
  • 17