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
14
votes
2 answers

How to calculate sums in log-space without underflow?

I am trying to calculate log(a + b) given log(a) and log(b). The problem is, log(a) and log(b) are so negative that when I try to calculate a and b themselves, they underflow and I get log(0), which is undefined. For log(a * b) and log(a / b), this…
14
votes
1 answer

How to do an inverse log transformation in R?

I am familiar with regular log transformations: DF1$RT <- log(DF1$RT) How do I perform an inverse log transformation in R?
Yash
  • 141
  • 1
  • 1
  • 3
14
votes
2 answers

How to convert log probability into simple probability between 0 and 1 values using python

I am using Gaussian mixture model for speaker identification. I use this code to predict the speaker for each voice clip. for path in file_paths: path = path.strip() print (path) sr,audio = read(source + path) vector =…
Sandeep
  • 369
  • 1
  • 5
  • 16
14
votes
6 answers

performance of log10 function returning an int

Today I needed a cheap log10 function, of which I only used the int part. Assuming the result is floored, so the log10 of 999 would be 2. Would it be beneficial writing a function myself? And if so, which way would be the best to go. Assuming the…
laurisvr
  • 2,724
  • 6
  • 25
  • 44
14
votes
4 answers

How to compute log base 2 using bitwise operators?

I need to compute the log base 2 of a number in C but I cannot use the math library. The answer doesn't need to be exact, just to the closest int. I've thought about it and I know I could just use a while loop and keep dividing the number by 2 until…
SKLAK
  • 3,825
  • 9
  • 33
  • 57
14
votes
5 answers

Is there any GMP logarithm function?

Is there any logarithm function implemented in the GMP library?
eddy ed
  • 917
  • 2
  • 10
  • 21
13
votes
2 answers

log2 of an integer that is a power of 2

Is there an efficient way to find the log2 of a number assuming it's a power of 2. I know the obvious ways like having a table or for (log2=0;x!=1;x>>=1,log2++); But I am wondering if there is a more efficient/elegant way.
Tohiko
  • 1,860
  • 2
  • 18
  • 26
13
votes
2 answers

Matplotlib - Boxplot calculated on log10 values but shown in logarithmic scale

I think this is a simple question, but I just still can't seem to think of a simple solution. I have a set of data of molecular abundances, with values ranging many orders of magnitude. I want to represent these abundances with boxplots…
Frank
  • 619
  • 1
  • 6
  • 26
12
votes
1 answer

How to inverse a log2 transformation

I have data in this form: ds y 1 2015-12-31 51737806366 2 2016-01-01 451800500 3 2016-01-04 48503189 4 2016-01-06 221000 5 2016-01-07 542483038 6 2016-01-08 628189789 7 2016-01-09 556762005 8 …
Miguel 2488
  • 1,410
  • 1
  • 20
  • 41
12
votes
1 answer

Working with small probabilities, via logs

Source: Google Code Jam. https://code.google.com/codejam/contest/10224486/dashboard#s=a&a=1 We're asked to calculate Prob(K successes from N trials) where each of the N trials has a known success probability of p_n. Some Analysis and thoughts on the…
Brondahl
  • 7,402
  • 5
  • 45
  • 74
12
votes
1 answer

How are logarithms programmed?

Are they just figured out using the same mechanism as a linear search or is the range narrowed down somehow, similar to a binary search.
Taffer
  • 147
  • 1
  • 1
  • 4
11
votes
1 answer

Space ship simulator guidance computer targeting with concentric indicator squares

I'm working on a 3D space trading game with some people, and one of the things I've been assigned to do is to make a guidance computer 'tunnel' that the ship travels through, with the tunnel made of squares that the user flies through to their…
Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
11
votes
2 answers

Logarithm with SSE, or switch to FPU?

I'm doing some statistics calculations. I need them to be fast, so I rewrote most of it to use SSE. I'm pretty much new to it, so I was wondering what the right approach here is: To my knowledge, there is no log2 or ln function in SSE, at least not…
user1128760
11
votes
4 answers

O(n log log n) time complexity

I have a short program here: Given any n: i = 0; while (i < n) { k = 2; while (k < n) { sum += a[j] * b[k] k = k * k; } i++; } The asymptotic running time of this is O(n log log n). Why is this the case? I get that the…
chrismanderson
  • 4,603
  • 5
  • 31
  • 47
11
votes
5 answers

Double equals 0 problem in C

I was implementing an algorithm to calculate natural logs in C. double taylor_ln(int z) { double sum = 0.0; double tmp = 1.0; int i = 1; while(tmp != 0.0) { tmp = (1.0 / i) * (pow(((z - 1.0) / (z + 1.0)), i)); …
mbreedlove
  • 336
  • 2
  • 5
  • 17