Showing posts with label programming tips. Show all posts
Showing posts with label programming tips. Show all posts

Saturday, February 13, 2021

HP Prime CHOOSE for Repeated Use - Present Value

 HP Prime CHOOSE for Repeated Use - Present Value


Introduction:  Using the CHOOSE Command to Allow for Repeated Calculations


The use of REPEAT and CHOOSE will allow the programmer to set up a choose from a set of calculations and allows for additional calculations until the user exits the program.  


Here is a sample syntax, with comments followed by double slashes (//):


// Set up local variables:  a choice variable, a list of options, the size of the list

LOCAL ch, lst, s;


// Use a list of options.  Be sure to have the last option as "Exit", "Quit", etc.

lst:={"opiton 1", "option 2", ... , "Exit"};

s:=SIZE(lst);


// Set a choice variable to a starting value. Use a "dummy" value to begin with, like -1.  The choice variable will take on a positive integer value after the CHOOSE command has been executed.

ch:=-1;


// The REPEAT loop

REPEAT

CHOOSE(ch,"title",lst);


IF ch==1 THEN

[choice 1 - calculation]

END;


IF ch==2 THEN

[choice 2 - calculation]

END;


...


IF ch==n-1 THEN

[choice n-1 - calculation]

END;


UNTIL ch==s;   // This is the Exit option


Let's look at an example program.



HP Prime Program: PVACT


*  This is an example program that uses CHOOSE to allow for repeated calculations.   The application is for several types of present value for several types of annuities.


*  As a bonus, you can save some settings, such as format and number of digits, to variables before switching settings that the program requires.  This allows for the programmer to restore settings at the end of the program.  


EXPORT PVACT()

BEGIN

// EWS 2021-01-10

// CHOOSE demonstration

// loop until user exits


// Grab the settings

LOCAL h1,h2;

h1:=HFormat;

h2:=HDigits;


// ch choice varaible

LOCAL ch,n,p,r,m,w,v;

LOCAL lst,s,g;


// Set a "dummy" initial choice

// for ch

ch:=−1;


// Set a list of choices

lst:={"Constant Annuity",

"Growing Annuity",

"Perpetual Annuity",

"Exit"};

s:=SIZE(lst);


// Set decimal fix to 2 places

HFormat:=1;

HDigits:=2;


// Use a Repeat loop to allow

// more than one calculation

REPEAT


CHOOSE(ch,"Present Value",lst);

 

// Constant Annuity - Choice 1

IF ch==1 THEN

INPUT({n,r,m},"Constant Annuity",

{"n?  ","i%? ","PMT? "},

{"number of payments",

"periodic interest rate",

"payment"});

v:=Finance.TvmPV(n,r,−m,0,1);

PRINT();

PRINT("n = "+n);

PRINT("i% = "+r);

PRINT("PMT = "+m);

PRINT("PV = "+v);

// show the screen

// WAIT is needed in a loop

WAIT();

END;


// Growing Annuity - choice 2

IF ch==2 THEN

INPUT({n,r,g,m},"Growing Annuity",

{"n?  ","i%? ","g%? ","PMT? "},

{"number of payments",

"periodic interest rate",

"payment growth rate",

"base payment"});

w:=(1+0.01*g)/(1+0.01*r);

v:=m/(1+0.01*r)*(1-w^n)/(1-w);

PRINT();

PRINT("n = "+n);

PRINT("i% = "+r);

PRINT("g% = "+g);

PRINT("Base Payment = "+m);

PRINT("PV = "+v);

// show the screen

// WAIT is needed in a loop

WAIT();

END;


// Perpetual Annuity - choice 3

IF ch==3 THEN

INPUT({r,m},"Perpetual Annuity",

{"i%? ","PMT? "},

{"interest rate",

"payment"});

v:=m/(0.01*r);

PRINT();

PRINT("r% = "+r);

PRINT("PMT = "+m);

PRINT("PV = "+v);

// show the screen

// WAIT is needed in a loop

WAIT();

END;


// if Exit is selected

UNTIL ch==s;


// Restore the setting

HFormat:=h1;

HDigits:=h2;

END;



Eddie


All original content copyright, © 2011-2021.  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. 


Monday, February 29, 2016

Quick Tricks: Casio Programming Calculators and webcal.freetzi.com

Quick Tricks:  Casio Programming Calculators and webcal.freetzi.com

 I spent the last week learning how the Casio fx-3650p can be programmed to execute amazing calculations.  The reason why I say amazing is that on the surface, the fx-3650p is limited, both in programming space (360 bytes allocated on 4 program slots) and the number of variables (7: A, B, C, D, X, Y, M).  Please check out the WebCal Page Counting Machine, run by David Chuek: 


The website is in Chinese (I believe) and I am grateful for Google Translate because I am not proficient in Chinese. 

On this page advanced programs for the fx-3650p including:

* Gamma Function, Elliptic Functions (for x<1), Beta, Zeta, and other special functions
* Determinant of a 3 x 3 Matrix
* Squaring a square matrix
* 4 and 8 point Fast Fourier Transform
* Geometry:  Distance from a point to a line, area from a set of vertices
* Prime Factorization
* Engineering Programs: Snell’s Laws, Prism, Ohm’s law
* Days between Dates, Day of the Week

If you have a Casio programming calculator, check this awesome website out.

While I was learning to code the programs myself, I notice there were several tricks that were used to save space.  I want to test the tricks for the Casio fx-5800p and Casio fx-9860GII (Linear Input Mode).   The case for the fx-9860GII should be the same for other modern Casio graphing calculators (9750g, Prizm). 

Quickly Setting X = 1 and Y = 0

Syntax:  Pol(1,0)
Calculators:   fx-3650p only

Note:  Pol(1,0) sets I to 1 and J to 0 for the fx-5800p.

The Power of Implied Multiplication

Most Casio programming calculators have only single-letter variables, which allows for implied multiplication.  For example, typing AB multiples the values stored in A and B.  In the examples listed below, I use variables A and B, but it applies to all the other variables. 

Implied Multiplication and Square Root

Syntax:  √AB
What it calculates:  √(A*B)
Calculators: fx-3650p, fx-5800p, fx-9860GII

Example:  A = 5, B = 10;  √AB returns 7.071067812  (√50)

Implied Multiplication and Powers

Syntax:  n^AB
What it Calculates:  (n^A)*B
Calculators: fx-3650p, fx-9860GII (linear input only)

Example:  2^AB where A = 5, B = 10;  2^AB returns 320   (2^5*10)

The Jump Function Shortcuts

If the Expression is Nonzero

Syntax:  (var/expression) ⇒ (execute this statement of var/expression ≠  0) : (next command)
The next command must exist or an error occurs

Calculators:  fx-3650p, fx-5800p, fx-9860GII

Example: Ask for A and B.  Divide A by B, unless B is 0. 
Prog2
? → A: ? → B: B ⇒ A ÷ B: Ans

A = 13.5, B = 2.7, result is 5
A = 13.5, B = 0, result is whatever is last stored in Ans

Using the Jump Function to create an AND condition

Connecting two or more jump functions (⇒) to create an AND condition. 

Syntax – 2 Conditions:
(condition 1) ⇒ (condition 2) ⇒ (do if conditions 1 and 2 are true) : (next command)

Syntax – 3 Conditions:
(cond 1) ⇒ (cond 2) ⇒ (cond 3) ⇒ (do if all three conditions are 3) : (next command)
  
Example: Calculate the eccentricity of an ellipse, assuming A ≥ B.  If anything improper is entered, -1 is returned.

Prog3
? → A: ? → B: -1 → C: A ≥ B ⇒ A > 0 ⇒ B > 0 ⇒ √(1 – B2/A2 → C: C

A = 11.5, B = 3.6, result 0.949738796
A = 6, B = 8, result -1
A = -2, B = 1, result -1

Independent Memory

M+:  Calculate the expression, add to M and store the result to M
M-:  Calculate the expression, subtract from M and store the result to M

Syntax:  (expression) M+,  (expression) M-
Calculators: fx-3650p, fx-5800p. 
Note:  This should work on any Casio calculator with M+ and M- available on the keyboard.

Example: 
Store 7 in M.  
3^2 + 4 M+ returns 13 in the display, with M = 20
2^3 – 1 M- returns in the display, with M = 13

Absolute Value

Does you calculator not have an absolute value function?  Try this trick:

Syntax: √A2
What it calculates:  |A|
Calculators:  fx-3650p, fx-5800p, fx-9860GII

Examples:  A = 55, B = -55
√A2 returns 55
√B2 returns 55

Extracting the Integer Part of a Number when the Function Int/Intg is not available

Two ways to do it:
* Syntax:  Fix 0:  Rnd:  Norm 1
* Syntax:  Switch the fx-3650p to Base Mode, 3 for Decimal, then enter your program

The Last Answer (Ans) skips over a prompt

Syntax:  (calculation):  ? → var:  f(Ans)
Calculators: fx-3650p, fx-5800p, fx-9860GII

Example:  Calculate 5^1.5, ask for A, and add it to 5^1.5
Prog1
5^1.5 : ? → A:  Ans + A
Let A = 2, the result is 13.18033989
Let A = 9.75, the result is 20.93033989


Happy Leap Year Day!   Talk to you next time,

Eddie


This blog is property of Edward Shore.  2016


Python: Combination Functions (Micropython)

Python: Combination Functions (Micropython) This is a set of combination functions. Programmed with a Casio fx-CG 100 but should ...