The HP Calculator Specialists
Musings and comments about our common interest
HP has just issued the HP Prime app for iPhone and iPad! It works fast. There are several minor bugs, but that's to expect, since the new appel guidelines imply that the code had to be converted to 64 bit from 32, and that all characters are stored with 32 bits (which allow much more complex character sets).
The app can be found in the App store. Be aware that there was a version for Android since several months ago.
You have here a demo of the app:
Two blog issues ago, we discussed the screen menu and how to make it work. We had to descend to pixel level programming. It is still far from low level, but it is considerably lower than anything done before. This is what “tab menus” entail.
There is a different (and simpler) way of choosing between different alternatives: the CHOOSE() command. Let’s see an example:
The latest HP Prime firmware has introduced several important features, which were lacking in previous releases and that allow to better use the machine resources.
Here we will discuss the drop-down menus and the screen tab menus. This makes communicating with your program much more seamless, GUI-oriented, than the typical question/answer and input field method (that we had to use if we didn’t have access to a Prime SDK (software development kit)
Both need some event-management concepts beforehand. An event is either a key click, or a mouse event. Mouse events can be of several types: click, long click, drag, etc., but we will now concentrate on the simple mouse click, in order to get up and running a short application (this program has been modified from one published by Eddie B. Shore, as many here). We have included some comments so that you can see what’s happening.
We will now focus on the screen tab menus and leave the dropdown menus for tomorrow:
Code:
EXPORT CM(x)
BEGIN
// CAS Custom Menu
// EWS 2014–04–20
LOCAL m,m1,mx,my; //variables containing the mouse event and coordinates
WHILE MOUSE(1)≥0 DO END;
RECT;
TEXTOUT_P(“Choose the function.”,1,1,4);
TEXTOUT_P(“Here you could put a description of the menu functions below”,1,18,4);
TEXTOUT_P(“2: …”,1,35,4);
TEXTOUT_P(“3: …”,1,52,4);
TEXTOUT_P(“4: …”,1,69,4);
TEXTOUT_P(“5: …”,1,86,4);
TEXTOUT_P(“6: …”,1,103,4);
DRAWMENU(“1”,”2”,”3”,”4”,”5”,”6”);
REPEAT
m:=MOUSE;
m1:=m(1);
UNTIL SIZE(m1)>0; //Waiting until the Mouse variable is filled with a mouse event
mx:=m1(1); //Getting the coordinates of such event - first X
my:=m1(2); //Then y
IF my≥220 AND my≤239 THEN //Only if the mouse event in in the lower part of the screen
IF mx≥0 AND mx≤51 THEN
RETURN SUB(x,1,1);
END;
IF mx≥53 AND mx≤104 THEN
RETURN; //your code here;
END;
IF mx≥106 AND mx≤157 THEN
RETURN ; //your code here;
END;
IF mx≥159 AND mx≤210 THEN
RETURN; //your code here;
END;
IF mx≥212 AND mx≤263 THEN
RETURN; //your code here;
END;
IF mx≥265 AND mx≤319 THEN
RETURN; //your code here;
END;
END;
END;
My elder son is beginning to enjoy maths! At 12 years old, he is fond of reading, and he has considered several possible future professions, all related with writing and reading: journalist, novel writer, actor, etc. But now that he has enjoyed a good teacher, that makes maths learning fun (basically by giving them homework that is based on investigation: he gives a general problem, and as you solve a question, other questions come that make you think and discover relations and laws by yourself), then he is beginning to enjoy maths.
What he hates (as I did and still do) is the mechanical part of maths. One of the problems of last week was to check which prime numbers below 100 could be expressed as the sum of two squares. While he has learn to check for “prime-ness” by himself, it is tiring to apply it to many numbers. Then I took the opportunity to show him some of the possibilities of the HP Prime CAS mode: the ifactor() function. This function gives the prime factors of an integer; for example, ifactor(60) would return 2^{2}*3*5; so, an integer is a prime number if there is a single factor. This speeds a lot the search!
Other concepts that were explained too were gcd and lcm; and it was explained using numbers in its factored form; here he could use the lcm() and gcd() functions once he had apprehended the concepts on the factored numbers, in order to test his results. Then the calculator is really a tool to learn faster, not to cheat on the learning process.
Another alternative that I could have used was the hp50g, or even the hp41cl running any of the Sandmath modules (this is what I use!), but a touch screen is difficult to fight against for a 12 year old kid. The fact that he can download some games written in HP basic helps too. The games are quite lame when compared with what he can get in a simple smartphone; but the beauty of them is that he can enter in the program and try to understand what it is doing; alter number of lives, speed, number of enemies or even their aspect by modifying their grobs; and learn some programming in the process. In that respect, HP basic is much more readable than RPL, and more adequate to write games too.
(For a good load of games for you HP Prime, visit hpcalc.org/prime/games or en.hpprime.club. Some of them are fun enough, and it is surprising how fast can the HP Prime be)
We discussed a couple of days ago about the formula library designed by Eddie B. Shore. I have added several equations on my own - namely all the decibel-watt and decibel-voltage gain calculations, loudspeaker sensitivity; but do you have equations you want to have included?
Also, it is good practice to include the equation in the “print” area, so you know what the equation does before using the solver. Tomorrow I will post the program with this latter change - but also a couple of additional equations too.
I have found that some equations that worked in the HP17bII solver don’t work in the Prime’s. “Bad guess”, the system says. Go figure. The function is not linear by any means - it has a MIN() function, but nevertheless the variable usually searched for is outside of that MIN() function, so there should be no problem. Worthy of more investigation.
Today I would like to draw your attention to a small HP Prime site. Small but very well thought out. I could find elsewhere all that information, but it is very well organized.
The site is:
(There is a Russian version too, but 95% of our readers would feel better with the English one)
It has a very good command reference tool. In the DOCs area, apart from the manuals that you can find here, with their latest versions (by the way, I have updated the versions in our site too, to the 2015 versions), you can find all commands, both one by one or in a long list. It takes you to a very good command definition, where you can even post comments. This is the part I am now using most. Far better than perusing the long manuals!
There is also an area with customer apps. There are many games - I will have to put some in my children’s calls to get them interested. Some of them were from when we were young - very young. It will be also a good way to learn Prime programming.
There is also a software area, where the most recent firmware, communication tool and emulator are posted. For us Mac users, there is also a calculator for MacOs, based on Wine. I am now opening it and downloading the latest firmware upgrade. I’ll tell you how it works!
I loaded Eddie Shore’s Equation program in my HP Prime, tested it, and liked it a lot. There are a couple of things that could be improved, but it would require more work. But the program is very simple and good to go through and learn how easy is to achieve things in HP Prime with simple, easy to understand programs.
Let’s go through it together, and learn a couple of things meanwhile:
EXPORT eqnlib()
BEGIN
// March-April 2015 EWS
// Equation Library Similar to HP 48 series
// Variables A-Z & θ are global
// Initialization
LOCAL ch,d;
STARTAPP(“Solve”);
(Eddie didn’t use the #pragma instruction since it wasn’t available at the time he wrote the program. I have already had some problems with it due to that - but that’s an easy fix. Just remember to place it at the very beginning of the file.
Then he initializes some local variables, that will be used for the menu choices. After that, he starts the solve app - since you could be anywhere else.)
// Choose
// Main Menu
CHOOSE(ch,”Equation Library”,
{“Geometry”,”Finance”,”Temperature”,
“Physics-Motion”,
“Sound”,”Optics”,
“Electronics”,”Astronomy”,
“Great Circle”,
“Angle of Incidence”,
“Gases”,”Fluids”});
IF ch==0 THEN KILL; END;
This instruction creates a menu on the screen, gives it a title, lays down the choices, and stores the choice in the variable ch.
// 1 Geometry Loop
IF ch==1 THEN
CHOOSE(d,”Geometry”,{“Area: Circle”,
“Area: Ellipse”,
“Area: Trapezoid”,
“Volume: Sphere”,
“Volume: Cylinder”,
“Volume: Cone”,
“Surface Area: Sphere”,
“Surface Area: Cylinder”,
“Distance of 2 Points”,
“Regular Polygon: Σ Angles”,
“Area: Regular Polygon”});
IF d==0 THEN KILL; END;
Then, choice after choice on menu 1, he does the same thing, but with a level two menu: you choose the equations you will use. Again, the choice is stored in a variable: d in this case. if clicked out of the menu, or escaped, you get zero and you’re out of the program.
// 1.1 Area: Circle
IF d==1 THEN
“A=π*R^{2”▶E1};
PRINT();
PRINT(“A = Area”);
PRINT(“R = Radius”);
END;
Choice per choice, he stores the equation in the system variable E1 (which is the first equation in the solver). Then, prints on the screen the variables used. These will disappear when you click any key; and then you’re left with the solver. (Here is one of the improvements that could be done: we could print the formula after the variables used. I’ll do it on Monday night, when I have some time.
…
// 1.11 Area: Regular Polygon
IF d==11 THEN
“A=NS^{2}/4COT(180°/N)”▶E1;
PRINT();
PRINT(“A = Area”);
PRINT(“N = Number of Sides”);
PRINT(“S = Length of a Side”);
END;
// Close out Geometry Loop
END;
Let’s not forget this last end, to close the Geometry chapter loop!!
Finalizing, we have these two instructions, to display the solver using the formula we have set in E1:
// Display Solver
CHECK(1);
STARTVIEW(2,1);
END;
Check(1) selects the first formula in the current app, which is the solver. (Check(2) w ould have checked the second, and so on)
Startview(2,1):
STARTVIEW(ViewNumber[,Redraw])
Starts a view of the current app. Redraw, is optional; if Redraw, is true (non 0), it will force a refresh for the view.
The view numbers are as follows:
0=Symbolic
1=Plot
2=Numeric
3=Symbolic Setup
4=Plot Setup
5=Numeric Setup
6=App Info
7=Views key
Did you learn anything? I did.
There is an undocumented function in HP Prime, very useful for us Central Europeans that insist on decimal comma instead of decimal point; and that had to move to decimal point if we wanted to be able to write programs that compile in all circumstances.
the line syntax is
Code:
This is telling the parser that, no matter what the calculator status is, you’re doing the programming with the following convention:
To insert the PRAGMA, while in program editor, press “Menu” key and “5 insert pragma”. It has to be put before the start of any program, or it will give a syntax error.
You can now test your programs with European and American conventions in the Home settings, and see what happens!
There is a discussion on HPmuseum.org regarding accuracy on the HP Prime. There were some simple calculations that should give zero, but that give an infinitesimal value.
The learning for me is that now I understand why there is a Home and a CAS environment. In fact, from a point of view, you have two calculators in one.
CAS should be used for exact, symbolic calculations, and implements XCAS from Parisse as well as the HP team could implement. All calculations and real numbers are represented internally with binary numbers. Therefore, decimal numbers have no exact representation in binary - and this has an effect on the accuracy of many operations.
Home should be used for normal decimal calculations. The internal system represents number in BCD, binary coded decimal. What is BCD? If you’re a frequent visitor of hpmuseum.org, you already know; if not, bear with me for a while.
the BCD format (of which HP uses the “packed” version, where an 8-bit byte contains two decimal digits, each one occupying a 4-bit word, called sometimes “nibble”) was used in early computers, like the IBM System/360 and DEC’s VAX. The drawbacks of BCD are more complex routines and slightly lower compactness; the advantage is the perfect representation of decimal magnitudes, as opposed to pure binary, that cannot represent exactly some of them; also that there are very refined algorithms for both normal arithmetic and transcendent functions. Nowadays, with enough memory and processing power, there are not too many differences between both systems when you have enough significant digits in your routines.
Most HP calculators were made to work with BCD; only the 12C Platinum comes to my mind as binary, as well as the “external job” calculators like 9s and 30s machines. So it is curious that the HP Prime is both a BCD AND a binary calculator!!
I 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?)