Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Math.Log10() Method in C#
The Math.Log10() method in C# calculates the base-10 logarithm of a specified number. This method is particularly useful in mathematical calculations, scientific computations, and when working with exponential data that needs to be converted to a logarithmic scale.
Syntax
Following is the syntax for the Math.Log10() method −
public static double Log10(double d);
Parameters
d − A double-precision floating-point number whose base-10 logarithm is to be found.
Return Value
The Log10() method returns different values based on the input parameter −
| Input Parameter (d) | Return Value |
|---|---|
| Positive number | The base-10 logarithm of d |
| Zero | NegativeInfinity |
| Negative number | NaN (Not a Number) |
| NaN | NaN |
| PositiveInfinity | PositiveInfinity |
Using Math.Log10() with Special Values
The following example demonstrates how Math.Log10() handles special floating-point values −
using System;
public class Demo {
public static void Main() {
double val1 = Double.PositiveInfinity;
double val2 = Double.NegativeInfinity;
double val3 = Double.NaN;
Console.WriteLine("Log10(PositiveInfinity) = " + Math.Log10(val1));
Console.WriteLine("Log10(NegativeInfinity) = " + Math.Log10(val2));
Console.WriteLine("Log10(NaN) = " + Math.Log10(val3));
}
}
The output of the above code is −
Log10(PositiveInfinity) = Infinity Log10(NegativeInfinity) = NaN Log10(NaN) = NaN
Using Math.Log10() with Regular Numbers
This example shows the behavior of Math.Log10() with common numerical values −
using System;
public class Demo {
public static void Main() {
double val1 = 0;
double val2 = 1;
double val3 = 10;
double val4 = 100;
double val5 = -5;
Console.WriteLine("Log10(0) = " + Math.Log10(val1));
Console.WriteLine("Log10(1) = " + Math.Log10(val2));
Console.WriteLine("Log10(10) = " + Math.Log10(val3));
Console.WriteLine("Log10(100) = " + Math.Log10(val4));
Console.WriteLine("Log10(-5) = " + Math.Log10(val5));
}
}
The output of the above code is −
Log10(0) = -Infinity Log10(1) = 0 Log10(10) = 1 Log10(100) = 2 Log10(-5) = NaN
Practical Application Example
Here's a practical example that demonstrates using Math.Log10() to calculate the number of digits in an integer −
using System;
public class Demo {
public static void Main() {
int[] numbers = {12, 345, 6789, 12345};
Console.WriteLine("Number\tDigits");
Console.WriteLine("---------------");
foreach (int num in numbers) {
int digits = (int)Math.Floor(Math.Log10(num)) + 1;
Console.WriteLine(num + "\t" + digits);
}
}
}
The output of the above code is −
Number Digits --------------- 12 2 345 3 6789 4 12345 5
Conclusion
The Math.Log10() method is essential for calculating base-10 logarithms in C#. It handles special cases like zero, negative numbers, and infinity appropriately, making it reliable for mathematical computations and scientific applications where logarithmic calculations are required.
