How to resolve the algorithm Exponentiation operator step by step in the Elixir programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Exponentiation operator step by step in the Elixir programming language

Table of Contents

Problem Statement

Most programming languages have a built-in implementation of exponentiation.

Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an overloaded form should be provided for both   intint   and   floatint   variants.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Exponentiation operator step by step in the Elixir programming language

Source code in the elixir programming language

defmodule My do
  def exp(x,y) when is_integer(x) and is_integer(y) and y>=0 do
    IO.write("int>   ")         # debug test
    exp_int(x,y)
  end
  def exp(x,y) when is_integer(y) do
    IO.write("float> ")         # debug test
    exp_float(x,y)
  end
  def exp(x,y), do: (IO.write("       "); :math.pow(x,y))
  
  defp exp_int(_,0), do: 1
  defp exp_int(x,y), do: Enum.reduce(1..y, 1, fn _,acc -> x * acc end)
  
  defp exp_float(_,y) when y==0, do: 1.0
  defp exp_float(x,y) when y<0, do: 1/exp_float(x,-y)
  defp exp_float(x,y), do: Enum.reduce(1..y, 1, fn _,acc -> x * acc end)
end

list = [{2,0}, {2,3}, {2,-2},
        {2.0,0}, {2.0,3}, {2.0,-2},
        {0.5,0}, {0.5,3}, {0.5,-2},
        {-2,2}, {-2,3}, {-2.0,2}, {-2.0,3},
        ]
IO.puts "                    ___My.exp___  __:math.pow_"
Enum.each(list, fn {x,y} ->
  sxy = "#{x} ** #{y}"
  sexp = inspect My.exp(x,y)
  spow = inspect :math.pow(x,y)         # For the comparison
  :io.fwrite("~10s = ~12s, ~12s~n", [sxy, sexp, spow])
end)


  

You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Draco programming language
You may also check:How to resolve the algorithm Faulhaber's triangle step by step in the Haskell programming language
You may also check:How to resolve the algorithm Sleep step by step in the Raku programming language
You may also check:How to resolve the algorithm Maze solving step by step in the Tcl programming language
You may also check:How to resolve the algorithm Pisano period step by step in the REXX programming language