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

Python Log, Value Error : math domain error

I have this code: import math n2 = [0,1,2,3] for i in range(1,3): x = math.log2(n2[i]) n2 += x print(n2) When I try it, I get: ValueError: math domain error. Why? What is wrong, and how do I fix it?
susmit410
  • 9
  • 4
-2
votes
3 answers

Python: string comparison with double conditions

Trying to search 2 lists for common strings. 1-st list being a file with text, while the 2-nd is a list of words with logarithmic probability before the actual word – to match, a word not only needs to be in both lists, but also have a certain…
Россарх
  • 147
  • 1
  • 9
-2
votes
1 answer

How does log() differentiate between log base and natural log?

I am new to coding, and am a bit confused by how C++ differentiates between log base (any number) and natural log (base e) if both are represented by the same log() function. Can anyone shed some light on this for me?
-2
votes
1 answer

Iterated logarithm Big-O complexity

I have 2 doubts:- 1) Is (log* n)^n = O((logn)!) ? 2) Which is bigger, log(log* n) or log*(logn) ?
Zephyr
  • 1,521
  • 3
  • 22
  • 42
-2
votes
3 answers

How can I resolve an exponential function for x in R?

I want to analyse a logarithmic growth curve in more detail. Especially I would like to kow the time point when the slope becomes >0 (which is the starting point of growth after a lag phase). Therefore I fitted a logarithmic function to my growth…
Anne
  • 241
  • 1
  • 3
  • 9
-2
votes
1 answer

How to apply base2 logarithm to an RDD of Ints in Spark?

What is the proper way of doing this to apply log2 to my RDD of numbers? Is there a function to help with this?
hli
  • 61
  • 1
  • 5
-2
votes
1 answer

Linear Scale vs. Log Scale

This may be a very simple question, but I haven't been in touch with Algorithms since a while. I have a logarithmic scale of 20,100,500,2500,12500 which relates to 1,2,3,4,5 on the respectively. Now, I want to find out as to where the value 225…
-2
votes
2 answers

Big O of while loop

I have some code here that where x grows like big O(n), however I'm not sure why. It seems more of a logarithmic big O. Could I get some help figuring out why it grows like big O(n)? Thanks! i = k; x = 1.0; while (i > 1 ) { i = (int)…
-2
votes
2 answers

What is the running time of an O(n log n) sort done n times?

Is it O(n^2 log n)? Can you show how it is derived? Is O(n^2 log n) the same as O((n^2) * (log n))?
-2
votes
1 answer

insertion sort beat merge sort n<=8logn , value of n

I am weak in math n<=8logn How to solve this equation to get the value of n, The question was from algorith " For inputs of size n, insertion sort runs in 8n^2 steps while merge sort runs in 64n lg n steps; for which values of n does insertion…
Mansur Ahamed
  • 53
  • 1
  • 2
  • 7
-2
votes
2 answers

How to plot a log curve in R?

I have the following set of data: x = c(8,16,64,128,256) y = c(7030.8, 3624.0, 1045.8, 646.2, 369.0) Which, when plotted, looks like an exponential decay or negative ln function. I'm trying to fit a smooth curve to this data, but I don't know…
user2954167
  • 155
  • 1
  • 3
  • 14
-2
votes
2 answers

What is the complexity of function f(n) with n=f(n).log(f(n))

What is the complexity of function f(n),preferably the Big-O notation, and f(n) satisfies the condition n = f(n).log(f(n)) ,f(n) > 1 .Let assume that log in base 2. I tried to isolate f(n) from the condition but could not get it done. After using…
huynq9
  • 522
  • 8
  • 22
-2
votes
1 answer

How to find function for calculating Y in curve using R

I have a logarithmic curve in x and y data (I know the curve is smooth and have 127 data points). Im trying to use R to find the equation for calculating Y when X = N. I have put my data into R x = c(0, 1, 2, 3, 4... y = c(0 , 31.6 , 33.3 , 35.1 ,…
Ke.
  • 2,484
  • 8
  • 40
  • 78
-2
votes
1 answer

Logarithmic space

Does anyone have a method to generate a logarithmically spaced number array. The method signature would be: public static List logSpace (double start, double end, double numValues) This is similar to matlab function 'logspace'
mcintoda
  • 635
  • 1
  • 8
  • 10
-3
votes
1 answer

counting digts in c++ using log10

#include #include using namespace std; int main() { int n, temp, rem, digits=0, sum=0; cout << "Enter a armstrong number: "; cin >> n; temp = n; digits = (int)log10(n) + 1; while (n != 0) { rem…