Math.Log() Method in C#


The Math.Log() method in C# is used to return the logarithm of a specified number.

Syntax

public static double Log(double num)
public static double Log(double num, double base)

Above, num is the specified number whose logarithm is to be calculated. Here, the base is the base of the logarithm.

Let us now see an example to implement the Math.Log() method −

Example

using System;
public class Demo {
   public static void Main(){
      double val1 = 2.15;
      double val2 = -2.15;
      Console.WriteLine(Math.Log(val1));
      Console.WriteLine(Math.Log(val2));
   }
}

Output

This will produce the following output −

0.765467842139571
NaN

Example

Let us see another example to implement the Math.Log() method −

using System;
public class Demo {
   public static void Main(){
      double val1 = Double.PositiveInfinity; ;
      double val2 = Double.NegativeInfinity;
      Console.WriteLine(Math.Log(val1));
      Console.WriteLine(Math.Log(val2));
   }
}

Output

This will produce the following output −

∞
NaN

Updated on: 04-Nov-2019

438 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements