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
5
votes
3 answers

How do I calculate a logarithm in iOS?

I want to calculate a logarithm in iOS. Can Objective-C do this?
Kuldeep Singh
  • 1,214
  • 4
  • 18
  • 45
5
votes
1 answer

How to calculate the inverse of the log normal cumulative distribution function in python?

How do I calculate the inverse of the log normal cumulative distribution function in python? I'm trying to translate some functions from Excel that uses the function [LOGINV][1] For example LOGINV(0,005;2;0,5) yields 2,0382373 where 0,005 is the…
GeoPy
  • 1,556
  • 3
  • 17
  • 21
5
votes
1 answer

geom_raster interpolation with log scale

I'm a bit stuck plotting a raster with a log scale. Consider this plot for example: ggplot(faithfuld, aes(waiting, eruptions)) + geom_raster(aes(fill = density)) But how to use a log scale with this geom? None of the usual methods are very…
Ben
  • 41,615
  • 18
  • 132
  • 227
5
votes
1 answer

WPF and Oxyplot: graph with CategoryAxis and LogarithmicAxis

I am currently playing with Oxyplot and the demo samples (that one can download there https://github.com/oxyplot/oxyplot/archive/develop.zip), more particulary the sample 'ColumnSeriesDemo'. Here is what it loks like when executed: Code: public…
Louitbol
  • 170
  • 1
  • 16
5
votes
2 answers

Compute logarithmic expression without floating point arithmetics or log

I need to compute the mathematical expression floor(ln(u)/ln(1-p)) for 0 < u < 1 and 0 < p < 1 in C on an embedded processor with no floating point arithmetics and no ln function. The result is a positive integer. I know about the limit cases (p=0),…
mqtthiqs
  • 445
  • 3
  • 9
5
votes
4 answers

Difference between Logarithmic and Uniform cost criteria

I'v got some problem to understand the difference between Logarithmic(Lcc) and Uniform(Ucc) cost criteria and also how to use it in calculations. Could someone please explain the difference between the two and perhaps show how to calculate the…
Marthin
  • 6,413
  • 15
  • 58
  • 95
5
votes
3 answers

Log values by SFrame column

Please, can anybody tell me, how I can take logarithm from every value in SFrame, graphlab (or DataFrame, pandas) column, without to iterate through the whole length of the SFrame column? I specially interest on similar functionality, like by…
Guforu
  • 3,835
  • 8
  • 33
  • 52
5
votes
2 answers

R lattice xyplot doesn't match grid to axes' ticks (not multiplot)

I'm trying to produce a plot with lattice's xyplot by the following code: set.seed(123) #### make it…
PavoDive
  • 6,322
  • 2
  • 29
  • 55
5
votes
2 answers

Java logarithm by log(num, base)(Closed)

I am making a program with android SDK (Java), but I have GUIed my program to which you get a number, then the base. The base can be an any number. Do I have a way other than to switch-case it infinitely? (I have closed this thread. I have found…
user2700021
5
votes
1 answer

Why am I getting this error when trying to use log from math.h in C?

I need to use logs in a program for an assignment. I ran this test program on my machine to see how the log function works (and if it would), and I get the following error during build. Code /* log example */ #include /* printf…
user2608931
5
votes
2 answers

Matplotlib logarithmic scale with zero value

I have a very large and sparse dataset of spam twitter accounts and it requires me to scale the x axis in order to be able to visualize the distribution (histogram, kde etc) and cdf of the various variables (tweets_count, number of…
amaatouq
  • 2,297
  • 5
  • 29
  • 50
5
votes
1 answer

How to dynamically change axis from linear to logarithmic in HighChart

I want to dynamically change axis from linear to logarithmic and vice versa in HighCharts. Please see this fiddle example. yAxis: { //linear type: 'logarithmic', minorTickInterval: 0.1 }, There is a type: String in api For initial…
user503853
5
votes
6 answers

how to check if a number is a power of base b?

In python, how can you check if a number n is an exact power of base b? Note: it needs to be generalized to any base which is given as a parameter. Here is what I got: Assume n and base are integers > 0. import math def is_power(n,base): return…
omega
  • 40,311
  • 81
  • 251
  • 474
5
votes
8 answers

Why log(1000)/log(10) isn't the same as log10(1000)?

Today, I came across quite strange problem. I needed to calculate string length of a number, so I came up with this solution // say the number is 1000 (int)(log(1000)/log(10)) + 1 This is based on mathematical formula log10x = lognx/logn10…
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
5
votes
1 answer

Fortran natural logarithm error

New to Fortran (just started today), having trouble with the natural logarithm: PROGRAM log IMPLICIT NONE REAL :: x PRINT *, "Enter a number:" READ *, x x = log (x) PRINT *, "The natural log of x is:", x END PROGRAM log The compiler keeps…
James Jenkinson
  • 1,563
  • 2
  • 16
  • 33