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

Implementation of Log function but for numbers represented as a string C

Is there a way to implement the log function witch works for string numbers without converting the string to a int? example of string number: char *stringNumber = "432" the string represents only integers(whole Numbers) but they can be near…
El45n
  • 1
  • 4
-3
votes
1 answer

How can I create a y = log10(x) plot in python in range x = 1 to 1000?

I'm trying to plot y = log10(x) - squareroot(x) in Jupyter notebook with x in range 1 to 1000, any ideas?
elliea
  • 1
  • 1
-3
votes
2 answers

What does the logarithm code look like in C++?

I can't search what does the logarithm code look like in C++? What the code of the logarithm function looks like in C++ in the library cmath? Exactly the code. I don't need to figure out how I can get the logarithm. I want to know how this algorithm…
OHtuzh
  • 21
  • 3
-3
votes
3 answers

Using electron in a formula in java

I'm trying to write a Java program that can take values and put them into a formula involving electron. How can I calculate e^x in Java?
-3
votes
2 answers

Big O time efficiency for a quadratic function in JavaScript

I am trying to increase the efficiency of a function. It is currently quadratic and I would like to make it logarithmic. The third to last line of the current function is confusing me somewhat as well and I would like some clarification. function…
aluko17
  • 21
  • 3
-3
votes
1 answer

Says X must be a value in logarithmic function?

I have the following code: import math q = input("Is there a square root in the function? (y,n) ") if q == "y": base = input("base? ") x_value = input("x? ") print (math.log(math.sqrt(base),x_value)) else: base_2 = input("base? ") …
Noah Brown
  • 123
  • 1
  • 3
  • 8
-3
votes
1 answer

Is 2^(log n) = O(log(n))?

Are these two equal? I read somewhere that O(2lg n) = O(n). Going by this observation, I'm guessing the answer would be no, but I'm not entirely sure. I'd appreciate any help.
kurikuone
  • 17
  • 1
  • 2
-3
votes
1 answer

C++ math expression from math formula

I try to convert this math formula in C++ expression But I'm doing something wrong (log(1.0+a*(getpixel(j,k)))/log10( y ))/(log(2.0)/log10( y ))
-4
votes
1 answer

How to take logarithmic scale in graph in JavaScript

I am new to JavaScript. Here I am plotting a graph. Since the values are very high the graph looks mess. So I would like to take logarithmic scale in x axis and y axis. Could you please advice me how to do that in JavaScript? Thanks in advance.
Raghavi
  • 321
  • 2
  • 4
  • 19
-4
votes
2 answers

How to write this mathematical formula in C++?

I have a simple formula and I want to use it in C++ application. Not sure how to rewrite in C++.
jack
  • 1
  • 4
-4
votes
2 answers

How do i calculate 26 + log (base 2)4 in java

26 + log (base 2)4. How to calculate this in java. np= (26 + log(2))*4; System.out.println(np); I have done this but it is showing garbage value. The answer should be 28.
Durvesh Shah
  • 43
  • 1
  • 6
-4
votes
1 answer

Determine the Big O Notation:

5n^5/2 + n^2/5 I tried eliminating the lower order terms and coefficients, not producing a correct answer. Not sure if I should use logs?
-5
votes
2 answers

How can I make a function that takes the log of a number and returns it with the answer rounded to 4 decimal places?

The prompt we were initially given says to create a function that receives a number as an argument and returns the log of that number rounded to 4 decimals places. I need the function to take the min_num and max_num as the arguments This is my…
-5
votes
1 answer

How to include logarithm function in calculation?

I am using react-native to build my mobile app and one of the feature include using a formula to calculate a result. The formula includes log function as seen below: How can I include log function into my calculation code?
Zhen
  • 12,361
  • 38
  • 122
  • 199
-5
votes
1 answer

How to calculate the multiplicator in a sum like sum += sum*(mulu^n)

I would like to calculate the spending factor in a rule where we spend Nth time the previous payment done Here is an example of spending. firstPaymentAmount=10 SpendingFactor=5 PaymentCount=4 payment1: 10 payment2: 50 (= 10 x…
Jean F.
  • 127
  • 8
1 2 3
68
69