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

Round up to nearest power of 10

I'm trying to figure out how to round up a number (greater than 0) to the nearest power of 10. Examples: roundUp(23.4) = 100 roundUp(2.34) = 10 roundUp(.234) = 1 roundUp(0.0234) = 0.1 roundUp(0.00234) = 0.01 For numbers greater than 1, I believe…
accelerate
  • 1,215
  • 3
  • 16
  • 30
9
votes
2 answers

Calculate the log base n with Shift left or Shift right

I have a little problem. Who knows how we can calculate the log base n with Shift_L or Shift_R? for example: for n=2 we had this solution: int log(int n){ int res = 0; while((n>>=1)) res++; return res; }
8
votes
1 answer

Avoiding overflow in log(cosh(x))

My simulation needs to implement np.log(np.cosh(x)) This overflows for large x, i. e. I'm getting the RuntimeWarning: overflow encountered in cosh warning. In principle, as logarithm decreases the number in question, in some range of x, cosh should…
WojciechR
  • 323
  • 1
  • 6
8
votes
1 answer

Calculation of xlogx with numpy

I want to calculate x ln x with arbitrarily small positive x, or x = 0 without underflow or division by zero. How do I go about doing it? I have googled "python numpy xlnx OR xlogx" with no meaningful result. x = 0 a = x * np.log(x) b =…
HK Tong
  • 83
  • 1
  • 10
8
votes
1 answer

How to apply funtion to single Column of large dataset using Dask?

If apply funtion to calculate logaritm at single column of large dataset using Dask, How can I do that? df_train.apply(lambda x: np.log1p(x), axis=1 , meta={'column_name':'float32'}).compute() The dataset is very large (125 Millions of rows), How…
ambigus9
  • 1,417
  • 3
  • 19
  • 37
8
votes
2 answers

Calculating Base-n logarithm in Ruby

This one seems like an easy one, but I'm having trouble calculating log (Base 5) in Ruby. Clearly the standard base-10 log works fine: >> value = Math::log(234504) => 12.3652279242923 But in my project I need to use Base 5. According to the ruby…
Scott Allen
  • 113
  • 2
  • 7
8
votes
1 answer

Logarithm of zero in Python

I am trying to do a logarithm of zero in Python. from math import log log(0) And this raises me an exception. ValueError: math domain error I understand that a log of a negative number is mathematically undefined, and that's why Python's log can…
Santiago Gil
  • 1,292
  • 7
  • 21
  • 52
8
votes
4 answers

What is the best data structure for storing a set of four (or more) values?

Say I have the following variables and its corresponding values which represents a record. name = 'abc' age = 23 weight = 60 height = 174 Please note that the value could be of different types (string, integer, float,…
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
8
votes
2 answers

Fitting logarithmic curve in R

If I have a set of points in R that are linear I can do the following to plot the points, fit a line to them, then display the line: x=c(61,610,1037,2074,3050,4087,5002,6100,7015) y=c(0.401244, 0.844381, 1.18922, 1.93864, 2.76673, 3.52449, 4.21855,…
user52291
  • 161
  • 2
  • 2
  • 4
7
votes
2 answers

'Float' object has no attribute 'log'

I have a time series with price information in column price. When I tried to create a new column ln_price by taking the ln of column price I got an error: AttributeError: 'float' object has no attribute 'log' Can someone help me understand why…
Tony
  • 221
  • 1
  • 4
  • 11
7
votes
2 answers

Does the base for logarithmic calculations in Python influence the speed?

I have to use a lot of logarithmic calculations in one program. In terms of the logarithmic base, the procedure is not specific. I was wondering, if any base n (2? 10? e?) is faster in the Python 3.5 math module than others, because maybe under the…
Mr. T
  • 11,960
  • 10
  • 32
  • 54
7
votes
2 answers

An efficient method for calculating log base 2 of a number between 1 and 2

I am working on a fixed-point platform (floating-point arithmetic not supported). I represent any rational number q as the floor value of q * (1 << precision). I need an efficient method for calculating log base 2 of x, where 1 < x < 2. Here is what…
goodvibration
  • 5,980
  • 4
  • 28
  • 61
7
votes
3 answers

Matlab: Solving a logarithmic equation

I have the following equation that I want to solve with respect to a: x = (a-b-c+d)/log((a-b)/(c-d)) where x, b, c, and d are known. I used Wolfram Alpha to solve the equation, and the result is: a = b-x*W(-((c-d)*exp(d/x-c/x))/x) where W is the…
ROLF
  • 284
  • 2
  • 14
7
votes
1 answer

Segment annotation on log10 scale works differently for the end and the beginning of the segment?

I found a rather confusing feature in ggplot while trying to annotate segments on log10 scale. Following code produces the plot below: library(ggplot2) dat <- data.frame(x = x <- 1:1000, y = log(x)) ggplot(dat, aes(x = x, y = y)) + geom_line(size =…
Mikko
  • 7,530
  • 8
  • 55
  • 92
7
votes
3 answers

Calculating the length needed to represent an integer in an arbitrary base

I have the length of a representation of an integer in an arbitrary base. Say the length is 15, and the base is 36. I'd then like to work out how long a representation of said integer would be in another arbitrary base. i.e, converting to base 2…
Max
  • 2,760
  • 1
  • 28
  • 47