Published on by JGD
# 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;

Comments: 0

More about:
HP prime, base conversion
Only registered users may post comments.

Sign in and post comment
Register now