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
1 answer

Java complex Math.log function giving wrong answer

I am running the following function to simulate power flow. Math.log( (1000 * d) / ((1000 * d) - p )) / Math.log(1000/999); I am doing two tests with different values for p for both: p = 1333 test 1: d = 1000000 test 2: d = 200000 Running…
KilleR
  • 71
  • 1
  • 7
-1
votes
3 answers

Program that calculates the value of natural logarithm of 2

I'm trying to write a program that reads in from the user the number of terms to be used in calculating the natural logarithm of 2. EX: Log(2) with 5 terms is 1/1 - 1/2 + 1/3 - 1/4 + 1/5. The denominator is increased by 1 with each subsequent term,…
joe
  • 29
  • 1
  • 4
-1
votes
2 answers

How do you code to determine the logarithm of a value in Ada?

Using Ada (GNAT): I need to determine the power of ten for a given value. The most obvious approach is to use a logarithm; but that fails to compile. with Ada.Numerics.Generic_Elementary_Functions; procedure F(Value : in Float) is The_Log :…
mat_geek
  • 2,481
  • 3
  • 24
  • 29
-1
votes
3 answers

Logarithmic calculation in Objective-C

I am building a game using Sprite Kit and I want to gradually increase the difficulty (starting at 1.0) based on the time since starting the game. Someone suggested that I should use a logarithmic calculation for this but I'm unsure how to…
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
-1
votes
1 answer

How to calculate witness pair for Big O?

Can someone explain how to use witness pairs in the case where you have to show that log n^2 is O(log n) ? Please give an example of how you came up with any particular witness pair.
-1
votes
1 answer

Not able to understand a logarithm conversion

I was going through slides of an algorithm class and came across following. T(n) = 2T(n^(1/2)) + lg n Rename: m = lg n => n = 2^m T (2^m) = 2T(2^(m/2)) + m Rename: S(m) = T(2^m) S(m) = 2S(m/2) + m Can any one explain me how did the last…
TechCrunch
  • 2,924
  • 5
  • 45
  • 79
-1
votes
2 answers

What does taking the logarithm of a variable mean?

Question with regards to taking the logarithm of a variable (Statistics Question) Say you have a bar graph displaying data for an example "Cost of Computer Orders by the Population" and you are trying to analyze the data and find a distribution. The…
Masterminder
  • 1,127
  • 7
  • 21
  • 31
-1
votes
2 answers

Finding number of digits in integer when being passed an int = 0

Using Java btw. But Usually people seem to do this sort of thing with. int numDigits = (int)(log10(num)+1); //can explicitly floor, or casting to int will do that but log10(0) = -INF, which means my length is being set to the largest negative…
Aerlusch
  • 517
  • 3
  • 6
  • 11
-2
votes
1 answer

Calculating log to the base 2 in java gives inaccurate results

Consider this program to determine if a given number is a power of 2. class Solution{ // Function to check if given number n is a power of two. public static boolean isPowerofTwo(long n){ // Your code here float…
Ananya
  • 1
  • 1
-2
votes
2 answers

derive exponential function using log() in C

I am trying to derive exponential function using logarithms. I know from below equation log(22026.4657948067), is 10 and exp(10) is 22026.4657948067 I would like to understand the basic math behind exp() and log(). At the moment, I have log() in C…
Coder
  • 27
  • 7
-2
votes
1 answer

For each of the following pairs of functions f(n) and g(n), either f(n) = O(g(n)) or g(n) = O(f(n)), but not both. Determine which is the case

there is 2 functions: f(n) = n + log n and g(n) = n√n if f(n) = O(g(n)): n + log n <= C * n√n else if g(n) = O(f(n)): n√n <= C(n + log n) stuck to prove that
lukalortk
  • 11
  • 1
  • 4
-2
votes
2 answers

Why floor and ceil functions giving different values when applied on a log function result in c++

Below is the program I've written to find the exponent of 6, but I this its giving wrong output or I may be wrong somewhere, I'm unable to figure out here. #include using namespace std; #define ll long long int main() { ll t; …
aksr
  • 322
  • 1
  • 2
  • 11
-2
votes
1 answer

Same Function written in two different ways in python, one is correct and the other is wrong

I am wondering form the following functions written in different ways in python. The first function is working fine and success. Return a correct data and successfully delete the record from the database. In the second one, return correct data, but…
Khalil
  • 41
  • 1
  • 10
-2
votes
1 answer

What is the best approach for computing logarithm of an integer x base 2 approximated to the greatest integer less than or equal to it?

What is the best amortized time complexity to compute floor(log(x)) amongst the algorithms that find floor(log(x)) for base 2?
-2
votes
1 answer

mapping numbers on a scale

I have an array of numbers up to 20000 and I'm trying to assign a weight to these numbers: The closer a number is to 0 the higher should the weight be. My problem is that I'm trying to make it such that the higher the number is, the smaller should…
user1791240
  • 142
  • 9