Z STD MATHS
01-16-2017, 07:26 PM
Post: #1
 StephenG1CMZ Senior Member Posts: 870 Joined: May 2015
Z STD MATHS
Z STD MATHS: Some standard maths routines

Stephen Lewkowicz (G1CMZ)
ANDROID HP Prime App broken offline on some mobiles
01-16-2017, 07:28 PM
Post: #2
 StephenG1CMZ Senior Member Posts: 870 Joined: May 2015
RE: Z STD MATHS
V0.001
Base Conversion
Precision
Etc

Code:

// Z STD MATHS V0.001 StephenG1CMZ

//BASECONV
LOCAL DIGITS:="0123456789ABCDEF";

EXPORT BASEST_D(NNST,BS)
//BASE STRING TO DECIMAL
//NNST: POSITIVE STRING EG 0F
//BS: INTERPRETED IN BASE BS
BEGIN
LOCAL II,JJ,DG;
LOCAL CC:=0;
LOCAL TT:=0;
LOCAL ERRFLG:=−1;
FOR II FROM SIZE(NNST)  DOWNTO 1  DO
DG:=−1;
FOR JJ FROM 1 TO MIN(SIZE(DIGITS),BS  ) DO
IF MID(NNST,II,1)==MID(DIGITS,JJ,1) THEN
DG:=JJ-1;
END;
END;
IF DG<0 THEN //DIGIT NOT IN BASE
//OCTAL(A): ERROR
//SPACE FOR READABILITY WITHIN NUM: NO:^10 POSN ERR
ERRFLG:=−1;
END;
TT:=TT+DG*BS^CC;
CC:=CC+1;
END;
RETURN IFTE(ERRFLG,ERRFLG,TT); //POS DECIMAL INTEGER
//NEG: BAD DIGIT NaN EG 8 OCTAL OR SPACE))
END;

EXPORT DEC_BASEST(NN,BS)
BEGIN
LOCAL GS:=":";//:INDICATION OF BASE
//BUT NEEDS REMOVING BEFORE INPUT
IF SIZE(DIGITS)<BS THEN
//NaN
ELSE
IF NN==0 THEN
GS:="0"+GS;
ELSE
WHILE NN>0  AND BS>1 DO
//PP=IP(FP(NN/BS)*BS);//DEBUG
GS:=MID(DIGITS,IP(FP(NN/BS)*BS)+1,1)+GS;
NN:=IP(NN/BS);
END;
END;
END;
RETURN GS;
END;

EXPORT DIGITSNEEDED(NN)
//DIGITS NEEDED FOR IP
//EXCLUDES SIGN POINT SEPARATOR
BEGIN
IP(LOG(MAX(1,ABS(NN))))+1;
END;

EXPORT DIGITSNEEDED_BS(NN,BS)
//AS DIGITSNEEDED BUT
//NN IS A BASE10 NUM TO BE SHOWN AS IN
//BS
BEGIN
LOCAL RR;
//IF BS 1 DIVBY0 TBD
RR:=LOG(MAX(1,ABS(NN)))/LOG(BS);
RR:=IP(RR)+1;
RETURN RR;//COUNT OF SYMBOLS IN BASE
//EG (16,16): RETURNS 1
//MULTIPLY BY SYMBOL SIZE IF F IS 16

END;

EXPORT ZBASECONV()
BEGIN
LOCAL RR;

//RR:=BASEST_D("8",8);

MSGBOX(DIGITSNEEDED(1000));
MSGBOX("RR"+RR);
END;
//END BASECONV
//Precision 2017 StephenG1CMZ
GetPrecisionb()
//Discover our precision in bits
//PC: Expect float:23 double 53
BEGIN //MEEUS
LOCAL XX:=1;
LOCAL JJ:=0;
WHILE XX+1≠XX DO
XX:=2*XX;
JJ:=JJ+1;
END;
JJ:=JJ-1;

RETURN JJ; //MEASURED BITS OF PRECISION (INT)
END;

EXPORT PrecisionBase(bitsP,BaseN)
BEGIN
//LOCAL KK:=0.30103;LOCAL KK:=LOG(2);
LOCAL KK:=LN(2)/LN(BaseN);
LOCAL RESULT:=bitsP*KK;
//RESULT IS REAL,DECIMAL (OR OTHER BASE) PRECISION
RETURN ROUND(RESULT,11);
END;

PrPrecision(ST,bitsP,BaseN)
BEGIN
PRINT(ST+bitsP+" bits: "+PrecisionBase(bitsP,BaseN)+" digits");//NUM OF DEC(OR BASE) DIGITS ACCURACY
END;

EXPORT PRECISION()
BEGIN
PRINT();
PRINT("Precision:");
PrPrecision("BINARY:          ",1,2);
PrPrecision("OCTAL:           ",3,8);
PrPrecision("HEX:                ",4,16);
PrPrecision("HP Prime:     ",GetPrecisionb(),10);
PrPrecision("IEEE float:     ",24,10);
PrPrecision("IEEE double: ",53,10);
PrPrecision("TEST 3:        ",DIM("ABC")*4,16);//HOW MANY DIGITS
WAIT;
END;
//End Precision

Z_SGN(XX)
BEGIN
RETURN ((XX>0)-(XX<0));
END;

EXPORT REDUCE(XX,RNG)
//REDUCE RANGE TO 0≤AA<RNG
//RNG:0:NO REDUCE

BEGIN
LOCAL AA,BB;
IF RNG==0 THEN
RETURN XX;
END;
BB:=XX/RNG;
AA:=RNG*(BB-IP(BB));
IF AA<0 THEN
AA:=RNG+AA;
END;
RETURN AA; //0≤AA<RNG
END;

EXPORT SUM_LP(LST)
//SUM A LIST SMALLER 1ST TO KEEP PRECISION

//A SIMPLER IMP GIVEN SORT2 TECH:
//1 MAKE A SIGN LST)-1+2 CAN BE DONE
//2 MAKE AN ABS LST) IN 1 PASS
//3 SORT ABS LST, TRACKING WITH SIGN LST
//4 SUM EACH SORTED ABS*SIGN
//COST: 2 PASSES+1SORT.SIMPLE LOGIC
BEGIN
LOCAL II;
LOCAL LSGN:={};//LIST
LOCAL LPOS:={};//LIST
LOCAL SUMG:=0;
//MAKE A LIST OF SIGNS AND ABS
FOR II FROM 1 TO SIZE(LST) DO
LSGN(II):=Z_SGN(LST(II));
LPOS(II):=ABS(LST(II));
END;
//SORT LISTS LPOS WITH LSGN IN ORDER OF ABS VALUE
//TBD
//UNIL IMPLEMENTED: WORKS. NO GAIN.
//SUM SORTED LIST())
FOR II FROM  1 TO SIZE(LST) DO
SUMG:=SUMG+LSGN(II)*LPOS(II);
END;
RETURN SUMG;
END;

EXPORT STDMATHS()
BEGIN
LOCAL LL:={1,2,3};
LL:=RANDOM(1000,0,0.1);
//LL(0):=1ᴇ3;
//SUM_LP(LL);
REDUCE(180+360,−360);
END;

Stephen Lewkowicz (G1CMZ)
ANDROID HP Prime App broken offline on some mobiles
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)