How to resolve the algorithm Negative base numbers step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Negative base numbers step by step in the REXX programming language

Table of Contents

Problem Statement

Negative base numbers are an alternate way to encode numbers without the need for a minus sign. Various negative bases may be used including negadecimal (base -10), negabinary (-2) and negaternary (-3).[1][2]

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Negative base numbers step by step in the REXX programming language

Source code in the rexx programming language

/*REXX pgm converts & displays a base ten integer to a negative base number (up to -10).*/
@=' converted to base ';      numeric digits 300 /*be able to handle ginormous numbers. */
n=  10;  b=  -2;   q= nBase(n, b);   say right(n, 20)   @   right(b, 3)  '────►'  q   ok()
n= 146;  b=  -3;   q= nBase(n, b);   say right(n, 20)   @   right(b, 3)  '────►'  q   ok()
n=  15;  b= -10;   q= nBase(n, b);   say right(n, 20)   @   right(b, 3)  '────►'  q   ok()
n= -15;  b= -10;   q= nBase(n, b);   say right(n, 20)   @   right(b, 3)  '────►'  q   ok()
n=   0;  b=  -5;   q= nBase(n, b);   say right(n, 20)   @   right(b, 3)  '────►'  q   ok()
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
nBase: procedure; parse arg x,r;             $=  /*obtain args; $ is the integer result.*/
       if r<-10 | r>-2 then do; say 'base' r "must be in range: -2 ───► -10"; exit 13; end
                    do  while x\==0              /*keep processing while  X  isn't zero.*/
                    z= x // r;        x= x % r   /*calculate remainder; calculate int ÷.*/
                    if z<0  then do;  z= z - r   /*subtract a negative  R  from  Z ◄──┐ */
                                      x= x + 1   /*bump  X  by one.                   │ */
                                 end             /*                   Funny "add" ►───┘ */
                    $= z || $                    /*prepend new digit (numeral) to result*/
                    end   /*while*/
       return word($ 0, 1)                       /*possibly adjust for a  zero  value.  */
/*──────────────────────────────────────────────────────────────────────────────────────*/
ok:    ?=;  if pBase(q, b)\=n  then ?= ' ◄──error in negative base calculation';  return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
pBase: procedure; parse arg x,r;   p= 0;   $= 0  /*obtain args; $ is the integer result.*/
       if r<-10 | r>-2 then do; say 'base' r "must be in range: -2 ───► -10"; exit 13; end
            do j=length(x)  by -1  for length(x) /*process each of the numerals in  X.  */
            $= $ + substr(x,j,1) *    r ** p     /*add value of a numeral to $ (result).*/
            p= p + 1                             /*bump the power by 1.                 */
            end   /*j*/                          /* [↓]  process the number "bottom-up".*/
       return $


/*REXX pgm converts & displays a base ten integer to a negative base number (up to -71).*/
@=' converted to base ';      numeric digits 300 /*be able to handle ginormous numbers. */
n=      10;  b=  -2;   q= nBase(n, b);   say right(n, 20)  @  right(b,3)  '────►'  q  ok()
n=     146;  b=  -3;   q= nBase(n, b);   say right(n, 20)  @  right(b,3)  '────►'  q  ok()
n=      15;  b= -10;   q= nBase(n, b);   say right(n, 20)  @  right(b,3)  '────►'  q  ok()
n=     -15;  b= -10;   q= nBase(n, b);   say right(n, 20)  @  right(b,3)  '────►'  q  ok()
n=       0;  b=  -5;   q= nBase(n, b);   say right(n, 20)  @  right(b,3)  '────►'  q  ok()
n=-6284695;  b= -62;   q= nBase(n, b);   say right(n, 20)  @  right(b,3)  '────►'  q  ok()
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
_Base: !='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz /*+-!éáµ' /*sym*/
       parse arg $;        m= length(!);           L= length(x);             p= 0
       if r<-m | r>-2 then do;  say 'base' r "must be in range: -2 ───► -"m; exit 13;  end
       return
/*──────────────────────────────────────────────────────────────────────────────────────*/
nBase: procedure; parse arg x,r;   call _Base    /*get args; $  will be integer result. */
                    do  while x\==0              /*keep processing while  X  isn't zero.*/
                    z=x // r;         x= x % r   /*calculate remainder; calculate int ÷.*/
                    if z<0  then do;  z= z - r   /*subtract a negative  R  from  Z ◄──┐ */
                                      x= x + 1   /*bump  X  by one.                   │ */
                                 end             /*                   Funny "add" ►───┘ */
                    $=substr(!, z+1, 1)$         /*prepend the new numeral to the result*/
                    end   /*while*/
       if $==''  then return 0;    return $      /*possibly adjust for a  zero  value.  */
/*──────────────────────────────────────────────────────────────────────────────────────*/
ok:    if pBase(q, b)\=n  then return ' ◄──error in negative base calculation';  return ''
/*──────────────────────────────────────────────────────────────────────────────────────*/
pBase: procedure; parse arg x,r;   call _Base 0  /*get args; $  will be integer result. */
                    do j=L  by -1  for L         /*process each of the numerals in  X.  */
                    v=pos( substr(x,j,1), !) - 1 /*use base  R  for the numeral's value.*/
                    $= $ + v * r**p;    p= p + 1 /*add it to $ (result); bump power by 1*/
                    end   /*j*/                  /* [↑]  process the number "bottom-up".*/
       return $


  

You may also check:How to resolve the algorithm Musical scale step by step in the Pure Data programming language
You may also check:How to resolve the algorithm Zeckendorf number representation step by step in the 11l programming language
You may also check:How to resolve the algorithm Infinity step by step in the Klingphix programming language
You may also check:How to resolve the algorithm McNuggets problem step by step in the C# programming language
You may also check:How to resolve the algorithm Abbreviations, simple step by step in the Forth programming language