Haskell Program to calculate the base 10 logarithm of the given value


This tutorial will help us in calculating the base 10 logarithm of the given value. A logarithm is a mathematical function that calculates the power to which a number (called the base) must be raised to produce a given value. The base 10 logarithm, is a logarithm in which the base is 10.

Method 1: Using Internal Functions

In this method, we are going to use build-in log and log base function to calculate bas 10 log of a given number.

Algorithm

  • Step 1 − The Prelude library is imported to use log functions.

  • Step 2 − The log10 function is defined using logBase function as, log10 x = logBase 10 x.

  • Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 4 − The variable named, “value” is being initialized. It will hold a value whose base 10 logarithm is to be calculated.

  • Step 5 − The resultant base 10 logarithm value is printed to the console using ‘print’ function on calling the log10 function.

Example 1

In this example, we are going to see that how we can calculate the base 10 logarithm of the given value using logBase function.

import Prelude hiding (log)

log10 :: (Floating a) => a -> a
log10 x = logBase 10 x

main :: IO ()
main = do
   let value = 100
   print (log10 value)

Output

2.0

Example 2

In this example, we are going to see that how we can calculate the base 10 logarithm of the given value using built-in log function.

log10 :: (Floating a) => a -> a
log10 x = log x / log 10

main :: IO ()
main = do
   let value = 100
   print (log10 value)

Output

2.0

Method 2: Using Recursion

In this method, the recursion is used to calculate the base 10 logarithm of a given value. The log10 function takes a value of any type that is a member of the Floating type class, and returns the base 10 logarithm of that value, also of the same type. The function checks if the given value is 0 or 1, in which cases it returns the appropriate result. Otherwise, it divides the value by 10 and recursively calls the log10 function. Each recursive call increases the logarithm by 1, thus returning the final result.

Algorithm

  • Step 1 − The log10 function is defined using recursive conditions as, log10 x

| x == 0    = error "log10 of 0 is not defined"
| x == 1    = 0
| x < 1     = -log10 (1/x)
| otherwise = log10 (x/10) + 1.
  • Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 3 − The variable named, “value” is being initialized. It will hold a value whose base 10 logarithm is to be calculated.

  • Step 4 − The resultant base 10 logarithm value is printed to the console using ‘print’ function on calling the log10 function.

Example

In this example, we are going to see that how we can calculate the base 10 logarithm of the given value using recursion.

log10 :: (RealFloat a) => a -> a
log10 x
   | x == 0    = error "log10 of 0 is not defined"
   | x == 1    = 0
   | x < 1     = -log10 (1/x)
   | otherwise = log10 (x/10) + 1

main :: IO ()
main = do
   let value = 100
   print (log10 value)

Output

2.0

Conclusion

The base 10 logarithm of a value in Haskell, can be calculated by using the logBase function, by using built-in log function or by using recursion.

Updated on: 01-Mar-2023

269 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements