Mensajes sobre el tema: base conversion

Calculator blog


Musings and comments about our common interest

 

Publicado el por

Base formulae project (3)

prime10I have reflected a little bit about the two programs. Both are valid and work - for all bases less than 10 ! It will fail for any base higher than 10, since it doesn't recognize other non-number characters. As such, I realize that the program needs to be limited to a maximum base number. According to the discussion with my son, they may have to work on a base-20 problem (about a tribe in Oceania that uses all their fingers to count, feet included), so I will have to create 2 subroutines to convert from 0 to i, to 0 to 19; and viceversa.

I will make the changes to the programs tomorrow.

Precisely today there is a thread in hpmuseum.org about base changes - limited to base 10 too, and in RPN. (Did I mention that the formulas can be used in RPN mode too?)

 

 

Leer mensaje completo
Publicado el por

Base formulae project (2)

As promised yesterday - a couple of short programs, that can be also used as functions, that perform the conversion from decimal to any base (TOBASE(NUM,BASE)), and from any base to decimal (FROMBASE(NUM,BASE)).

This is a very fast job: the system does not check in FROMBASE that the original number is actually expressed in that base. if you perform FROMBASE(999,8) it will not check that 999 is an impossible number in octal, and it will give back a result (although wrong). These have been written in the HP Prime emulator, since it is so much easier to write there - but asap I'll check it on the real machine (as soon as I am able to find the damn cable to it)

EXPORT TOBASE(NUM,BAS)

BEGIN

LOCAL WIP:=NUM,DEST:=0,DIGIT;

FOR N FROM 0 TO (LOG(NUM)+1) DO

DIGIT:=(WIP MOD BAS);

WIP:=IP(WIP/BAS);

DEST:=DEST + DIGIT*10^N;

END;

RETURN DEST;

END;

 

EXPORT FROMBASE(NUM,BAS)

BEGIN

LOCAL DEC:=0, WIP:=NUM;

FOR N FROM 0 TO (LOG(NUM)+1) DO

WIP:=WIP/10;

DEC:=DEC+BAS^N*IP(10*fp(WIP))

END;

END;

Leer mensaje completo