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


In Haskell, the 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 2 logarithm, also known as binary logarithm, is a logarithm in which the base is 2. For example, the base 2 logarithm of 8 is 3, because 2 to the power of 3 equals 8 (2^3 = 8).

Method 1: Using logBase function

In this method, the log function takes a value of type Double and returns the base 2 logarithm of that value, which is also of type Double. The result is then printed to the console.

Algorithm

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

  • Step 2 − The log function is defined using logBase function as, log x = logBase 2 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 2 logarithm is to be calculated.

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

Example

In this example, we are going to see that how we can calculate the base 2 logarithm of the given value. This can be done by using logBase function.

import Prelude hiding (log)

log :: Double -> Double
log x = logBase 2 x

main :: IO ()
main = do
   let value = 64
   print (log value)

Output

6.0

Method 2: Using finiteBitSize and countLeadingZeros function

In this method, the log2 function is used from the Data.Bits module which provides bit manipulation functions. The log2 function takes an Int and returns the base 2 logarithm of that value, also of type Int.

Algorithm

  • Step 1 − The ‘Data.Bits’ library is imported to use bit manipulation functions.

  • Step 2 − The log2 function is defined as, log2 x = finiteBitSize x - 1 - countLeadingZeros 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 2 logarithm is to be calculated.

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

Example

In this example, we are going to see that how we can calculate the base 2 logarithm of the given value. This can be done by using finiteBitSize and countLeadingZeros function.

import Data.Bits

log2 :: Int -> Int
log2 x = finiteBitSize x - 1 - countLeadingZeros x

main :: IO ()
main = do
   let value = 64
   print (log2 value)

Output

6

Method 3: Using recursion

In this method, the recursion is used to calculate the base 2 logarithm of a given value. The log2 function takes a value of type Double and returns the base 2 logarithm of that value, also of type Double. 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 2 and recursively calls the log2 function. Each recursive call increases the logarithm by 1, thus returning the final result.

Algorithm

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

    | x == 0 = error "log2 of 0 is not defined"

    | x == 1 = 0

    | x < 1 = -log2 (1/x)

    | otherwise = log2 (x/2) + 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 2 logarithm is to be calculated.

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

Example

In this example, we are going to see that how we can calculate the base 2 logarithm of the given value. This can be done by using recursion.

log2 :: Double -> Double
log2 x
   | x == 0    = error "log2 of 0 is not defined"
   | x == 1    = 0
   | x < 1     = -log2 (1/x)
   | otherwise = log2 (x/2) + 1

main :: IO ()
main = do
   let value = 64
   print (log2 value)

Output

6.0

Conclusion

The base 2 logarithm of a value in Haskell, can be calculated by using the logBase function, by using finiteBitSize and countLeadingZeros function or by using recursion.

Updated on: 01-Mar-2023

569 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements