Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Log base 2

Tags:

c#

c#-4.0

Let's have the following code

(float)Math.Log(3.83031869)

The output i got is

1.342948

But when i calculated the Log2 for same number using many online calculators I got

 1.93746

any illustration please ? Thanks in advance.

like image 830
FSm Avatar asked Nov 30 '12 23:11

FSm


People also ask

What is the value of log base 2?

Log base 2, also known as the binary logarithm, is the logarithm to the base 2. The binary logarithm of x is the power to which the number 2 must be raised to obtain the value x. For example, the binary logarithm of 1 is 0, the binary logarithm of 2 is 1 and the binary logarithm of 4 is 2.


3 Answers

Math.Log(num) returns the log of base e

Math.Log(num, base) is probably what you are looking for.

like image 139
JG in SD Avatar answered Oct 10 '22 02:10

JG in SD


When calling the Log method with only a single argument, you get the Log base e. If you provide the second argument of 2.0, you get the result you expect:

//Testing in LinqPad
void Main()
{
    Math.Log(3.83031869).Dump();
    Math.Log(3.83031869, 2.0).Dump();
}

Results

1.34294800860817
1.93746443219072
like image 41
Chris Dunaway Avatar answered Oct 10 '22 03:10

Chris Dunaway


As can be seen in MSDN http://msdn.microsoft.com/en-us/library/x80ywz41.aspx

The Math.Log function computes the log with base e.

See http://msdn.microsoft.com/en-us/library/hd50b6h5.aspx for what you need.

like image 22
Itay Karo Avatar answered Oct 10 '22 02:10

Itay Karo