The Museum of HP Calculators

HP Forum Archive 18

MCODE multiprecision library for HP-41
Message #1 Posted by PeterP on 6 Nov 2008, 11:37 a.m.

## MCODE Multi-Precision Library for HP-41

A little while ago I decided to learn a bit of MCODE programming and hence I was looking for a project. Guided by the helpful hints from Valentin during his SSMC #4 and SSMC #20 - scroll down to very last post I started to build a MCODE based multi-precision library for the HP41.

Many hours later and thanks to the help of many here on the forum (in particular Egan Ford) I was able to show a somewhat acceptable 4k module providing some multi-precision functionality during the Allschwill meeting. While by no means complete and perfect, I think it might be a decent starting point to breath some, albeit limited, multi-precision life into the HP-41.

In its current state, the MPL provides

• ~1000 digits, exponents up to +/-2048 for each MPN (Multi-Precision-Number)
• a complete set of basic floating point arithmetic (+,-,*,/)
• some special floating point arithmetic (x2, yx, 1/x)
• an (almost) complete set of basic arithmetic between MPN and normal numbers (+,-,*).
• MPN/NN is missing but can be easily accomplished by MPN* (1/NN).
• Harder to replicate is N/MPN, which is also missing. It can be accomplished by converting the NN into a MPN via MPN2M, followed by MP* (which is not nearly as elegant as a separate MPN/M function would be)
• special functions for fast division and multiplication by 10
• a complete set of MPN handling and I/O functions (create, set, view, get MPN, convert between MPN and NN, Copy MPN, CHS, CLX MPN, etc)
• some comparison functions (MP=0?, MP=1?)
• custom MPL error messages
• entirely in MCODE

Naturally there is an even longer list of things the MPL does not do as of today, for example

• MPN comparison functions (MPNX>MPNY, etc)
• sqrt(x), ex, ln(x), fact(x)
• constants pi and e to many digits
• direct division instead of the simple, yet slow Newton iteration
• more effective use of storage by storing a full 14 digits in each register. This would also speed up MP*, MPY^X and MP/ by at least 100%
• much more testing
• etc

One idea to tackle these was to create a little Open Source project. Maybe there are others that enjoy programming in MCODE and enjoy the challenge.

In the meantime, for those that are interested, please find below links to

If you do play around and discover bugs, it would be great if you can send me an email via the forum (please with steps to replicate it).

Cheers,

Peter

Edited: 6 Nov 2008, 9:50 p.m. after one or more responses were posted

 Re: MCODE multiprecision library for HP-41Message #2 Posted by Juergen Keller on 6 Nov 2008, 4:07 p.m.,in response to message #1 by PeterP I had the pleasure to listen to your talk in Allschwil. Impressive and very interesting work, Peter. Congratulations! And thank you for providing the .MOD files and the documentation. As it seems, it's never too late to start learning MCODE; I guess I have to look for a project ;-) Best Regards, Juergen

 Re: MCODE multiprecision library for HP-41Message #3 Posted by Egan Ford on 6 Nov 2008, 7:22 p.m.,in response to message #1 by PeterP Thanks! Going to write my first program with it this weekend.

 Re: MCODE multiprecision library for HP-41Message #4 Posted by HrastProgrammer on 7 Nov 2008, 3:12 a.m.,in response to message #1 by PeterP From what I can see, this is really an excellent work! It is very nice to see some new developments here. I cannot wait to find some time and try your library. Congratulations and keep up the good work ...

 Re: MCODE multiprecision library for HP-41Message #5 Posted by PeterP on 8 Nov 2008, 1:58 p.m.,in response to message #1 by PeterP a few first tips from feedback I got 1) don't do a normal RCL to any of the registers that hold mpn's . The normalization process of the hp41 destroys their content making them unusable by the MPL 2) when you turn off the calc the internal buffers for the mpl get released. This is to free the memory they use. Your MPNs are *not* destroyed. When you want to work with them again just do an MPINIT with the correct number of regs again. I might change that behavior and make the buffer constant but I have not decided. Would be interested to hear your opinion/preference on this. I'm working on a function that will tell you the number of regs for a particular MPN which might be helpful. 3) I don't have printer support. This is a clear oversight of me as I never had a printer or used one. So I know next to nothing about printing especially out of mcode. See the related posted question so that I can ad a MPRINT function. Cheers Peter

 Re: MCODE multiprecision library for HP-41Message #6 Posted by Etienne Victoria on 10 Nov 2008, 5:44 p.m.,in response to message #1 by PeterP Peter, Thank you for providing this new code for us to enhance the number-crunching power of the 41! I'll start to experiment tomorrow. I understand it is Xrom 03 so won't collide with much widespread modules except Surveying. Just a small question if you have some time: - I assume the library must reside on page #8 and cannot be relocated. Is this something you would reconsider in a future evolution? - When burnt on a Novram with Hepax, I think it collides with the Hepax RAM which resides on pages #8 to #B. So I'll start using it on Emu41. Many thanks again for providing such an entertaining and useful tool. Cheers from France. Etienne

 Re: MCODE multiprecision library for HP-41Message #7 Posted by PeterP on 11 Nov 2008, 4:08 a.m.,in response to message #6 by Etienne Victoria Etienne, Thanks for your kinds words, very much appreciated! I believe all of your statements are correct, maybe with the exception of the HEPAX-RAM. You can definitely put it into a RAMBOX, CY, MLDL2000, Clonix, NoV, V41, mm41 and emu41. And I actually do think that you can copy the ROM into the HEPAX RAM on page 8 if you take page 8 out of the HEPAX file system (address x100 I think...) but I admit that this is one configuration I have not tested, despite my love for the HEPAX. It is a mere lack of knowledge on my part on how to create relocatable MOD files that the MPL is currently fixed to page 8. I'll add this to the list of incoming requests I am working on (print an MPN when printer is attached, create a function to generate the key assignments as per the - fabulous - overlay from Jeff, make the buffers from MPINIT permanent via polling points, make the .mod relocatable). While I have spent a good amount of time on the rounding problem with regards to the last digit for MP1/x and MP/, there is no good way without a major overhaul as well as memory, time and usage inconveniences to fix this cleanly. What I might do as a crutch(? Kruecke in german) is a function that rounds each MPN by its last digit, which would make 9.999....9 a clean 1.000 And if I get really fancy, I could even make that behaviour steered by a user flag; SF X means the MPN gets rounded or not. But don't hold your breath, my processor is at best a 8086 in general and especially with regards to MCODE. Yet it loves to learn new things! Thanks again and please do note my previous post about the dangers of RCL and let me know of any other ideas, request and bugs (especially bugs) you might encounter. Cheers Peter

 UPDATE - Re: MCODE multiprecision library for HP-41Message #8 Posted by PeterP on 13 Nov 2008, 9:07 p.m.,in response to message #1 by PeterP Updated MPL2 Version 1-F .MOD and .ROM Files Hi, For those that are interested, please find an updated MPL. It has the following additional features/changes: Printer support for MPVIEW, MPGETX,MPGETM, MPNOR? and MPNOD. So to print a MPN, just do a MPVIEW with the printer connected and in Trace or Norm mode protected MPINIT. I have made the internal buffers of the MPL permanent (via the polling points). The space they require can now only be freed via MPUNL. Note: While this was tested on a real CX, MM41, emu41, i41CX+ and V41 it appears that the implementation of either the polling points and/or the packing routine in V41 are slight deviations from the other emulators and the real 41cx. In V41, turning the calc off and on and doing a pack will still delete the internal buffers. Make MP Key Assignements. There is now a (usercode program btw) called MPMKEYS that creates the keyboard outlay according to the overlay from Jeff (see separate thread). Existing assignments will be overwritten. If a printer is attached, the program will stop before each assignment due to the AVIEW instruction which shows you the current assignment that is done. CF 21 to avoid the interruption. If you add two numbers that differ more than the available number of digits (e.g. 1e-100 + 1e100 with 3 registers) the result is naturally only the larger number. There is now a tiny acoustic feedback (synth Tone 89) to make you aware of this. Relocatable .MOD file. The new mod file can now be used in any page, not only page 8. That's it, I think. Thanks for all the feedback I have received! Cheers Peter edited to include last change and update tested emulators Updated MPL2 Version 1-F .MOD and .ROM Files Edited: 14 Nov 2008, 10:52 a.m. after one or more responses were posted

 Re: UPDATE - Re: MCODE multiprecision library for HP-41Message #9 Posted by Etienne Victoria on 14 Nov 2008, 7:59 a.m.,in response to message #8 by PeterP Hallo Peter, Thank you very much for the update!! And for making the Rom relocatable!! It now burns perfectly on my NovRam page #D. I'll experiment this week-end. Tchüss! Etienne

Go back to the main exhibit hall