1. Question About Z-Level Number Thingie

Howdy, guys 'n' gals,

I was wondering if somebody could point me in the right direction...

I want to be able to use what I am temporarily calling "Z-Level" numbering,
where it's like hexadecimal but it uses the whole alphabet (up to 'Z', you
see). Whereas hexadecimal stops at F=15, z-decimal stops at Z = 36 (starting
with 0=0 and workin' yer way up).

I would like to have a generic routine where you can plug in a value and
tell it what numbering scheme you want to use (anything from 1 (binary) to
36 (z-level) initially) and it
will return that numbering scheme's number.

For example,

rVal = number(15, 16) would set rVal to the hexadecimal equivalent of "F",
because I want '15' returned in base-16.

Or, we could have a separate function for each number base. For instance:

rVal = hex(15) would set rVal to "F".
rVal = zex(36) would set rVal to "Z".
rVal = zex(37) would set rVal to "10". (Right?)

Anyway, if anybody could provide some hints and tips in this direction, I'd
appreciate it!

TIA,
< |<

new topic     » topic index » view message » categorize

2. Re: Question About Z-Level Number Thingie

Big number arithmetic is already available in the Euphoria Archives.
It does just what you are purposing.

        Lucius L. Hilley III
        lhilley at cdc.net
+----------+--------------+--------------+
| Hollow   | ICQ: 9638898 | AIM: LLHIII  |
|  Horse   +--------------+--------------+
| Software | http://www.cdc.net/~lhilley |
+----------+-----------------------------+

> ---------------------- Information from the mail
header -----------------------
> Sender:       Euphoria Programming for MS-DOS
<EUPHORIA at LISTSERV.MUOHIO.EDU>
> Poster:       "C. K. Lester" <cklester at TICNET.COM>
> Subject:      Question About Z-Level Number Thingie
> --------------------------------------------------------------------------
-----
>
> Howdy, guys 'n' gals,
>
> I was wondering if somebody could point me in the right direction...
>
> I want to be able to use what I am temporarily calling "Z-Level"
numbering,
> where it's like hexadecimal but it uses the whole alphabet (up to 'Z', you
> see). Whereas hexadecimal stops at F=15, z-decimal stops at Z = 36
(starting
> with 0=0 and workin' yer way up).
>
> I would like to have a generic routine where you can plug in a value and
> tell it what numbering scheme you want to use (anything from 1 (binary) to
> 36 (z-level) initially) and it
> will return that numbering scheme's number.
>
> For example,
>
> rVal = number(15, 16) would set rVal to the hexadecimal equivalent of "F",
> because I want '15' returned in base-16.
>
> Or, we could have a separate function for each number base. For instance:
>
> rVal = hex(15) would set rVal to "F".
> rVal = zex(36) would set rVal to "Z".
> rVal = zex(37) would set rVal to "10". (Right?)
>
> Anyway, if anybody could provide some hints and tips in this direction,
I'd
> appreciate it!
>
> TIA,
> < |<
>

new topic     » goto parent     » topic index » view message » categorize

3. Re: Question About Z-Level Number Thingie

Thanks, Lucius! I'll have a look see...

> -----Original Message-----
> From: Euphoria Programming for MS-DOS
> [mailto:EUPHORIA at LISTSERV.MUOHIO.EDU]On Behalf Of Lucius L. Hilley III
> Subject: Re: Question About Z-Level Number Thingie
>
>     Big number arithmetic is already available in the Euphoria Archives.
> It does just what you are purposing.
>

new topic     » goto parent     » topic index » view message » categorize

4. Re: Question About Z-Level Number Thingie

C. K. Lester wrote:
>I want to be able to use what I am temporarily calling "Z-Level"
numbering,
>where it's like hexadecimal but it uses the whole alphabet (up to 'Z', y=
ou
>see). Whereas hexadecimal stops at F=3D15, z-decimal stops at Z =3D 36
(starting
>with 0=3D0 and workin' yer way up).
>

Actually, Z would be 35  tongue

I don't know if Lucius' suggestion helped you, but here is a short functi=
on
which converts a decimal integer to any base between 2 and 36...

sequence char, out_str
char =3D "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
out_str =3D ""

function convert_base(integer num, integer base)
    if num >=3D base then
        out_str =3D convert_base(floor(num/base), base) & char[remainder(=
num,
base)+1]
    else
        out_str &=3D char[num+1]
    end if
    return out_str
end function

- Colin

new topic     » goto parent     » topic index » view message » categorize

5. Re: Question About Z-Level Number Thingie

Dude, you rock!

Thanks.

I guess I should test the code before I sing your praises, but I have faith.
smile

Thanks, again!

< |<

-------------------
Your original post:

> I don't know if Lucius' suggestion helped you, but here is a
> short function
> which converts a decimal integer to any base between 2 and 36...
>
> sequence char, out_str
> char = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
> out_str = ""
>
> function convert_base(integer num, integer base)
>     if num >= base then
>         out_str = convert_base(floor(num/base), base) &
> char[remainder(num,
> base)+1]
>     else
>         out_str &= char[num+1]
>     end if
>     return out_str
> end function
>
> - Colin
>

new topic     » goto parent     » topic index » view message » categorize

6. Re: Question About Z-Level Number Thingie

Colin Taylor wrote:

>I don't know if Lucius' suggestion helped you, but here is a short function
>which converts a decimal integer to any base between 2 and 36...
>
>sequence char, out_str
>char = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
>out_str = ""
>
>function convert_base(integer num, integer base)
>    if num >= base then
>        out_str = convert_base(floor(num/base), base)
>                & char[remainder(num,base)+1]
>    else
>        out_str &= char[num+1]
>    end if
>    return out_str
>end function

Not bad! Being in something of a picky mood, though, I took it upon myself
to rewrite the above routine, and also to write its partner in crime. This
code is given below, along with a "sanity test" to insure that everything
converts back & forth properly.

Further suggestions/modifications/improvements are encouraged!


-- written by Gabriel Boehme
-- based on code by Colin Taylor

include misc.e

constant NUM_CHAR = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

type base_type(integer x)
   return x >= 2 and x <= length(NUM_CHAR)
end type

function int_to_base_n(integer int, base_type base)
sequence base_n
   base_n = ""
   while int >= base do
      base_n = prepend(base_n, NUM_CHAR[remainder(int,base)+1])
      int = floor(int/base)
   end while
   base_n = prepend(base_n, NUM_CHAR[int+1])
   return base_n
end function

function base_n_to_int(sequence base_n, base_type base)
integer int
   int = 0
   base_n = reverse(base_n)
   for pos = 1 to length(base_n) do
      int += (find(base_n[pos], NUM_CHAR)-1) * power(base, pos-1)
   end for
   return int
end function


-- sanity test
sequence string
integer result

for base = 2 to 36 do
   for number = 0 to 100 do
      string = int_to_base_n(number, base)
      printf(1, "%s\n", {string})
      result = base_n_to_int(string, base)
      if number != result then
         printf(1, "%d != %d", {number, result})
         abort(0)
      end if
   end for
end for

puts(1, "\nYay!\n")


Hep yadda,
Gabriel Boehme

----------
[...] we have a suffocating sense of luxury and no sense at all of liberty.
All the pleasure-hunters seem to be themselves hunted. All the children of
fortune seem to be chained to the wheel. There is very little that really
even pretends to be happiness in all this sort of harassed hedonism.

G.K. Chesterton
----------

new topic     » goto parent     » topic index » view message » categorize

7. Re: Question About Z-Level Number Thingie

Gabriel Boehme wrote:
>Not bad! Being in something of a picky mood, though, I took it upon myse=
lf
>to rewrite the above routine, and also to write its partner in crime. Th=
is
>code is given below, along with a "sanity test" to insure that everythin=
g
>converts back & forth properly.
>
>Further suggestions/modifications/improvements are encouraged!
>

Very nice, and probably faster than mine, too.  How about making it handl=
e
negative numbers?  That would include two's complement notation for binar=
y
numbers, of course...  I'll leave it to you, since my pickiness is all
faded away by around 10:30 AM.

- Colin

new topic     » goto parent     » topic index » view message » categorize

8. Re: Question About Z-Level Number Thingie

Gabriel Boehme wrote:
>Further suggestions/modifications/improvements are encouraged!

Here is a slight improvement of my original routine (recursion retained)
and a slight improvement of your second routine (doesn't need the reverse=
()
function).  It passes the sanity test, but still doesn't like negative
numbers.

- Colin


constant CHAR =3D "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

function convert_base(integer num, integer base)
    sequence out_str
    out_str =3D ""
    if num >=3D base then
        out_str =3D convert_base(floor(num/base), base)
    end if
    return out_str & CHAR[remainder(num, base)+1]
end function

function read_base(sequence num, integer base)
    integer out_num, len
    len =3D length(num)
    out_num =3D 0
    for i =3D 1 to len do
        out_num +=3D (find(num[i], CHAR)-1)*power(base, len-i)
    end for
    return out_num
end function

new topic     » goto parent     » topic index » view message » categorize

9. Re: Question About Z-Level Number Thingie

Colin Taylor wrote:

>Here is a slight improvement of my original routine (recursion retained)
>and a slight improvement of your second routine (doesn't need the reverse()
>function).  It passes the sanity test, but still doesn't like negative
>numbers.
>
>constant CHAR = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
>
>function convert_base(integer num, integer base)
>    sequence out_str
>    out_str = ""
>    if num >= base then
>        out_str = convert_base(floor(num/base), base)
>    end if
>    return out_str & CHAR[remainder(num, base)+1]
>end function

<PICKYMODE=PETPEEVE>

Maybe this is just me, but I don't see why the convert_base() function has
to be recursive. Every time it calls itself, it has to re-allocate space for
num, base and out_str. I guess I don't see how that's "better" -- especially
since base retains the same value on each recursive call, which is rather
inefficient. Generally, I'm pretty sure it's more efficient to code a
routine with "while" loop, rather than trying to make it recursive. (If I'm
wrong, though, let me know!)

</PICKYMODE>

And as for handling negative values, well, I guess I'm just going to "cheat"
on this one. The two's complement stuff would only work for base-2 derived
number systems, so I'm just adding a sign character to the value. That way,
it'll work no matter what number system we're using.

>function read_base(sequence num, integer base)
>    integer out_num, len
>    len = length(num)
>    out_num = 0
>    for i = 1 to len do
>        out_num += (find(num[i], CHAR)-1)*power(base, len-i)
>    end for
>    return out_num
>end function

Cool, thanks for getting that reverse() call outta there! smile


-- my revised version, covering the full range of Euphoria integers positive
and negative...

constant NUM_CHAR = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

type base_type(integer x)
   return x >= 2 and x <= length(NUM_CHAR)
end type

function int_to_base_n(integer int, base_type base)
integer sign
sequence base_n

   if int < 0 then
      sign = '-'
      int = -int
   else
      sign = '+'
   end if

   base_n = ""
   while int >= base do
      base_n = prepend(base_n, NUM_CHAR[remainder(int,base)+1])
      int = floor(int/base)
   end while

   return sign & prepend(base_n, NUM_CHAR[int+1])
end function

function base_n_to_int(sequence base_n, base_type base)
integer len, int
   len = length(base_n)

   int = 0
   for pos = 2 to len do
      int += (find(base_n[pos], NUM_CHAR)-1) * power(base, len-pos)
   end for

   if base_n[1] = '+' then
      return int
   elsif base_n[1] = '-' then
      return -int
   end if
end function


-- sanity test
sequence string
integer result

for base = 2 to 36 do
   printf(1, "[Base %d]\n", base)
   for number = -100 to 100 do
      string = int_to_base_n(number, base)
      printf(1, "%s\n", {string})
      result = base_n_to_int(string, base)
      if number != result then
         printf(1, "%d != %d", {number, result})
         abort(0)
      end if
   end for
   puts(1, '\n')
end for

puts(1, "\nYay!\n")


Now, if somebody could figure out how to handle floating-point values with
this thing...

Hep yadda,
Gabriel Boehme

----------
We'll never get rich by hard work; but, we'll never get rich without it.

Robert Fripp
----------

new topic     » goto parent     » topic index » view message » categorize

10. Re: Question About Z-Level Number Thingie

Gabriel Boehme wrote (quite a while ago):
>Maybe this is just me, but I don't see why the convert_base() function =

>has to be recursive. Every time it calls itself, it has to re-allocate
space =

>for num, base and out_str. I guess I don't see how that's "better"

That depends whether "better" means "more efficient"  or just "more fun t=
o
program".  Sometimes I go with the latter definition.

- Colin

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu