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
11
votes
6 answers

Computing the floor of log₂(x) using only bitwise operators in C

For homework, using C, I'm supposed to make a program that finds the log base 2 of a number greater than 0 using only the operators ! ~ & ^ | + << >>. I know that I'm supposed to shift right a number of times, but I don't know how to keep track of…
Brett Cox
  • 183
  • 1
  • 2
  • 12
11
votes
1 answer

Wrong logarithm of BigInteger when size of BigInteger exceeds ¼ gigabyte

When I have a BigInteger whose size exceeds 2 gigabits (that's ¼ gigabyte; I found this threshold by trial and error), the logarithm method gives a wrong answer. This simple code illustrates: byte[] bb; bb = new byte[150000001]; bb[150000000]…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
10
votes
2 answers

jQuery UI Slider logarithmic scale

I'm using this jQuery UI code use for a logarithmic slider: var minVal = 10; var maxVal = 100; $("#slider").slider({ range: true, min: minVal, max: maxVal / 2, values: [minVal, maxVal], slide: function(event, ui) { …
Taras Bulgakov
  • 543
  • 3
  • 11
  • 21
10
votes
5 answers

How to know when Big O is Logarithmic?

My question arises from the post "Plain English Explanation of Big O". I don't know the exact meaning for logarithmic complexity. I know that I can make a regression between the time and the number of operations and calculate the X-squared value,…
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
10
votes
2 answers

constexpr log10 Function for Integers

So I need log10 functionality to find the number of characters required to store a given integer. But I'd like to get it at compile time to determine the length of char arrays statically based on these integer constants defined in my code.…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
10
votes
3 answers

Big-O of log versus square root

Generally speaking, is the following always true? log(n) = O(na/(a+1))? s.t. a is any constant positive integer, perhaps very large. If not, what is the largest value of a for which this statement will hold true?
CaptainForge
  • 1,365
  • 7
  • 21
  • 46
10
votes
1 answer

Want to plot Pandas Dataframe as Multiple Histograms with log10 scale x-axis

I have floating point data in a Pandas dataframe. Each column represents a variable (they have string names) and each row a set of values (the rows have integer names which are not important). >>> print data 0 kppawr23 kppaspyd 1 …
Simon Woodward
  • 1,946
  • 1
  • 16
  • 24
10
votes
4 answers

Haskell logbase error

I am trying to calculate the length of an Integer in Haskell, using the fact that the length is equal to truncate (log10(x)+1). Using Integers I created: len :: Integer -> Integer len i = toInteger (truncate (logBase 10 (fromIntegral i)) +…
Pphoenix
  • 1,423
  • 1
  • 15
  • 37
9
votes
3 answers

What kind of logarithm functions / methods are available in objective-c / cocoa-touch?

I've tried searching for logarithm + objective-c, but all I get is math test pages from teachers, or explanations what a logarithm is ;) I've got some measurements that are like 83912.41234 and others are 32.94232. I need to press down this huge…
Thanks
  • 40,109
  • 71
  • 208
  • 322
9
votes
5 answers

Logarithm in Verilog

I've a statement in verilog looking like integer level = log(N) (Where N is a parameter and level is to be determined) But I understand I cannot do complex math statements in verilog, so I'm wondering if there is an alternative solution to the above…
Max Eastman
  • 91
  • 1
  • 1
  • 2
9
votes
2 answers

Efficient implementation of log2(__m256d) in AVX2

SVML's __m256d _mm256_log2_pd (__m256d a) is not available on other compilers than Intel, and they say its performance is handicapped on AMD processors. There are some implementations on the internet referred in AVX log intrinsics (_mm256_log_ps)…
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
9
votes
2 answers

Is Swift dictionary of indexed for performance? Even for exotic types (UUID)?

I want to construct some arrays that will remain in order to get fast searches. If I use something like this: let dictionary: [Int:Int] = [:] for i in 0 ..< 10000000 { dictionary[i] = 0 } Would the query: dictionary[n] == nil be performed in…
Nisba
  • 3,210
  • 2
  • 27
  • 46
9
votes
6 answers

python divide by zero encountered in log - logistic regression

I'm trying to implement a multiclass logistic regression classifier that distinguishes between k different classes. This is my code. import numpy as np from scipy.special import expit def cost(X,y,theta,regTerm): (m,n) = X.shape J =…
9
votes
4 answers

How to get lg2 of a number that is 2^k

What is the best solution for getting the base 2 logarithm of a number that I know is a power of two (2^k). (Of course I know only the value 2^k not k itself.) One way I thought of doing is by subtracting 1 and then doing a bitcount: lg2(n) =…
9
votes
9 answers

Minimum number of bits to represent a given `int`

In C++, what's the fastest way to find out how many bits are needed to store a given int? I can try dividing the number with 2 many times but divisions are pretty slow. Is there any fast way? Edit: Thanks a lot for the answers guys. When I say an…
Luka
  • 1,761
  • 2
  • 19
  • 30