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

Difficulty understanding the relation between the representation of a number and the time complexity of searching for (0, N - 1)?

Please refer here to the direct resource that I don't understand. Let's take the number X = 245436, we can represent the following as X = 2 * 10^5 + 4 * 10^4 + 5 * 10^3 + 4 * 10^2 + 3 * 10^1 + 6 * 10^0 using decimal expansion. So, the minimum amount…
thatguyjono
  • 506
  • 2
  • 7
  • 16
-1
votes
2 answers

Adding a column with logarithm in Dataframe Pyspark

I have a dataframe in which I'm trying to add a column which will basically be taking the logarithm of an existing column in the same dataframe. I am trying this : df = df.withColumn("logvalue", log(df["prediction_column"]) ) I have already…
arnab_0017
  • 41
  • 1
  • 1
  • 4
-1
votes
1 answer

Finding number of digits in javascript

I'm trying to get the number of digits of a number in javascript, but I'm running into some edge cases with the equations I've found online. Here's what I'm using, from this site. getNumDigits(val){ return val === 0 ? 1 :…
mheavers
  • 29,530
  • 58
  • 194
  • 315
-1
votes
1 answer

how to put the minimum value equal 0 in axes x in Chart C#

good afternoon! I would like to know if it is possible to put the value zero in scale in chart The chart is in logarithmic with base 10 and value maximum is 100 and minimum 0.001, I want to put o minimum value in zero. Here's an image of the graph…
-1
votes
2 answers

Logarithmic code in R programming with 3 variables

I have written a small program to develop logarithmic code and I have taken 3 variables, x, y and p, where x is base, y is log value and p is default value (power of 'x'). Now I am not getting error when I executed it but it didn't display any…
sk jainmiah
  • 53
  • 2
  • 2
  • 10
-1
votes
2 answers

.Net Chart Logarithmic Axis Scaling, unexpected scaling issues

I have an example chart with logarithmic scaling x axis and y axis as such, Ok now if I set the min X value to say 48 I get This is super unreadable, what I would prefer is to keep the 1, 10, 100, base 10 log scale, but essentially shift the…
-1
votes
1 answer

Discrete data to logarithmic scale

A function returns discrete data between 0 and 128. I want to transform that data to a scale in that way that a value of … 0 would result in 0 128 would result in 128 The values in between should be weighted in that way where lower number are…
Armin Hierstetter
  • 1,078
  • 2
  • 12
  • 27
-1
votes
1 answer

Understanding power of number 2

Assume if the node has N children in a tree and height be H, is total number of nodes equals N pow(H) or the logic applies only for 2 because of log base 2?
user2531608
  • 141
  • 2
  • 7
-1
votes
1 answer

Frequency - linear bins to logarithmic screenspace

I'm working on project, where I need to visualize spectral analysis to set some precise parameters. Now I'm with conversion of bins to screen space, because in linear space, magnitudes in lower frequencies are squashed together. Here's my code in…
sphere42
  • 156
  • 1
  • 11
-1
votes
1 answer

Logarithmic track bar for zoom values of 1% to 1000%

I have a trackbar associated with a picture box where I am drawing an image based on the selected zoom factor. The range is from 1% to 1,000% so the lower you slide it, the faster it appears to zoom out. This is expected but not desired. Is there a…
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
-1
votes
2 answers

Why do I need to set the max value to 101 in order for my basic guessing program to guess 100?

I found that in order to allow the guesser to guess 100, I need to set the maximum value to 100. I am confused by this, since if something is maximum 100, doesn't that include 100? Is there something wrong with my code? Can anyone explain? Noob here…
-1
votes
3 answers

c++ trying to figure out logarithms calculations

So if I am understanding c++ and logarithms correctly. Something like this should give me the base that I am looking for? I am having some issues, but think that this way is a correct start. #include using namespace std; int…
luckyging3r
  • 3,047
  • 3
  • 18
  • 37
-1
votes
3 answers

big integer calculation in c++ using logarithms

I am reading The Algorithm Design Manual by Steven S. Skiena and came across the topic of logarithms. It just hit me that instead of using python for big ints in competitive programming, I could just use the log(.) function in !(at least wherever I…
-1
votes
1 answer

primes, logarithms, summations and loops

I'm trying to make a program to compute all primes smaller than a given number, and the natural logarithm of the product of all those primes. So because we're working with logarithms I could also just add the natural logarithm of each prime smaller…
pokemonfan
  • 209
  • 1
  • 2
  • 9
-1
votes
3 answers

Algorithm: Finding pattern between n and i?

I have the following simple algorithm: int i = 2; int n = {Domain is all integers >= 2} while (I < N) I = I * I; execute(command); end-while I understand how many time the command with execute(ex. n=16 will execute 3 times, n=256, 4 times, etc.) I…
user1726845
  • 109
  • 3
  • 10