HP Prime: The Percent Function
The Percent Function
The HP Prime has three percent functions. They are not easily found (through the catalog or in program edit mode, Cmds-More menu). The functions are:
%
%CHANGE
%TOTAL
% Function
%(x, y) returns x * y /100. This returns either x% of y or y% of x. Due to the communicative multiplication property, you can use the arguments in either order.
%CHANGE
This the percent change function where the arguments are %CHANGE(old, new). Formula: %CHANGE(x, y) = (y - x) / x * 100%
%TOTAL
This calculates the % total given a part and a whole. Syntax: %TOTAL(whole, part). Formula: %TOTAL(x, y) = y / x * 100%
The program POPTOWN uses the percent functions. It is a simple game where you invite a certain population to live in a town, and the birth (growth) and death rates are determined by random.
HP Prime Prime Program: POPTOWN
RESULTS(y,n,g,d,p)
BEGIN
// subroutine
PRINT();
PRINT("Year: "+STRING(y));
PRINT("Population: "+STRING(n));
IF y>0 THEN
PRINT("Growth: "+STRING(g)+"%");
PRINT("Death: "+STRING(d)+"%");
PRINT("Δ%: "+%CHANGE(p,n));
END;
WAIT(0);
END;
// main program
EXPORT POPTOWN()
BEGIN
MSGBOX("You start with 10
residents, inviting up to 50
new residents each year. Can
growth beat death?");
// 2020-10-24 EWS
// Growth and Death
LOCAL n,y,w,g,d,c,p;
LOCAL v; // number of invites
n:=10;
RESULTS(y,n,g,d,p);
// The game
FOR y FROM 1 TO 5 DO
INPUT({{c,{0,10,20,30,40,50}}},
"Year "+STRING(y),"Invite:");
// all invites move in at the
// beginning of each turn
p:=n;
w:=10*(c-1)+n;
v:=10*(c-1)+v;
g:=RANDINT(5,20);
d:=RANDINT(1,25);
n:=w+IP(%(w,g))-IP(%(w,d));
// empty town scenario
IF n≤0 THEN
MSGBOX("No one survived.");
n:=0;
END;
RESULTS(y,n,g,d,p);
END;
// End the game results
PRINT("Final Results");
PRINT("Population Change: "+
STRING(n-10));
PRINT("Invited: "+STRING(v));
PRINT("Percent Invite: "+
STRING(ROUND(%TOTAL(n,v),2))
+"%");
END;
Eddie
All original content copyright, © 2011-2020. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.