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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Exponentiation operator step by step in the zkl 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 zkl programming language

Source code in the zkl programming language

fcn pow(n,exp){
   reg v;
   if(n.isType(1)){ // Int
      if (exp<0) return(if(n*n!=1) 0 else (if(exp.isOdd) n else 1));
      v=1; 
   }else{
      if(exp<0){ n=1.0/n; exp=-exp; }
      v=1.0;
   }
   while(exp>0){
      if(exp.isOdd) v*=n;
      n*=n;
      exp/=2;
   }
   v
}

println("2^6 = %d".fmt(pow(2,6)));
println("2^-6 = %d".fmt(pow(2,-6)));
println("2.71^6 = %f".fmt(pow(2.71,6)));
println("2.71^-6 = %f".fmt(pow(2.71,-6)));

  

You may also check:How to resolve the algorithm Find the missing permutation step by step in the Burlesque programming language
You may also check:How to resolve the algorithm First-class functions/Use numbers analogously step by step in the Ada programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Egel programming language
You may also check:How to resolve the algorithm MD5/Implementation step by step in the D programming language
You may also check:How to resolve the algorithm Nonoblock step by step in the Kotlin programming language