Since my tenure of the comp.sys.sinclair Crap Games Competition ended on 17th December, I bet you all thought you'd seen the last of me. You did, didn't you? Well, think again, because I've been flinging myself at some slightly tricky maths programs recently, and yet again, the QL and its very wide screen are a great help. Even more so than the banner-ad-shaped Z88.
A few years ago, I tried to write a cubic equation solver for the Spectrum using
Wikipedia's explanation - but I didn't get far, so I moved on to other projects. Part of the problem was, working out what was valid and what wasn't; I was presented with a formula that had a cube root (three potential solutions) with a square root under it (two potential solutions), minus another cube root (three potential solutions) with another square root under that (two potential solutions). This is where I completely lost the cubic plot.
But I've recently revisited it, with the help of a
video from Mathologer that's simple enough for anyone with A-level maths to follow (even the bit that involves complex numbers), and by careful calculation of the values that I actually
need (including how to evaluate the cube root of a complex number, which I didn't previously know), I've cracked it. The Spectrum version needed a few tweaks, and but for one example I've found where a root that's supposed to be 5 is calculated at 5.00000001, I've found it's accurate. This is despite having to convert a complex number x+iy into its polar form re^(iθ) - which means squaring the x and the y, adding them and taking a square root, then taking arctan(y/x) to find θ, adding 2π/3 and 4π/3 to θ to find the second and third roots, and taking cosine of all three. I've made some test cases such as (x-3)(x-6)(x-8)=0 in which I expect the roots to be exactly 3, 6 and 8 - and despite all the calculations that involve irrational numbers and rounding errors, bar that one example with the 5.00000001, the exact roots have been produced.
So, flushed with success, I moved onto the QL, and rewrote the Cubic Equation Solver to take advantage of the wide screen. I've kept it using two side-by-side windows, where the window on the left (#2) displays the results and the window on the right (#1) acts as a sketch pad, showing the interim values that are calculated to solve the equation. It will work with three roots (and that 5.00000001 comes out at exactly 5 this time), two roots where one is a duplicate, one real root and two complex conjugate roots, and one triplicate root.
Also, anyone tempted to start screaming about me using "Wild West" user-defined graphics - I haven't, anything that appears to be that is actually a procedure that draws it line by line with BLOCK statements, so it should work on any QL system from PM ROM to the latest SMSQ/E. And if I ever need the symbols again, I can copy the procedures.
That was a roaring success. And earlier today, I thought it might be possible to calculate factorials exactly, using the QL's 85-column display. I've used much the same principle as I did with the base converter earlier in this thread: store each digit of the number we're looking for in an array, multiply each digit, and then scan through them and carry if required. In the end I found I could keep the standard two-pixel border that cuts the window down to 84 characters, because 61! is 84 digits and 62! is 86 digits. I've checked with my calculator and - even though it shows 61! as 5.075802138×10^83, I can see the full value printed on the QL's screen is correct (as is 60! in the screenshot). It takes around two seconds to evaluate the smallest factorials and six and a half seconds for the last few, on a standard black box QL - though anyone who tries this will probably use QPC2 where the calculation will be effectively instant.
So if you've ever wondered what all the digits are in 61!, you can find out with this program.
I've got a grand plan to see if I can make an RPN calculator for the QL. The
Spectrum versions I made in 2017 are all well and good, but the QL's screen brings up the enticing prospect of a
complex RPN calculator - which will have four stacks for real, imaginary, magnitude and argument, and I'll need all four. Addition and subtraction are easy, multiplication is relatively straightforward, division is a tad more complicated, but taking a square root? That involves taking the positive square root of the magnitude, and halving the argument; it can't be done (easily) on the real and imaginary parts. Anyway, that's probably one for when I've got an enormous amount of time on my hands...
A reminder: these are regular PC zips, not QL zips - unzip them to reveal the _bas files inside.