32bit number division

hax

New Member
Hi all,
I'm trying to use ChatGPT to give me some picaxe code to divide a 32bit number (Two word variables) by a 16 bit number (one word variable). Unfortunately it hasn't quite got there yet. Lots of syntax errors. Can anyone help with this? I don't need the remainder or anything fancy. Just a rough approximation is fine.
Thanks!
 
I haven't got time to do a search for this right now but I think that member Jeremy Leach posted code for some 32-bit routines a few years ago.
 
Yes, Indeed Jeremy did this. Here is the link to the 32-bit integer maths version if I am right (particularly look at the Picaxe-28x2 code example adapted by Mawrob at post#11 and for the 20X2 at the end of the thread (post #14), they seems to contain all routines plus an example in the main body. I would prefer these posts as they are based on the modern X2 processors):


Note that the example link in post #13 provides useful info (code + excellent documentation by Jeremy) on fractional (i.e. floating point type) calculations : https://www.picaxeforum.co.uk/threads/maths-module-for-more-accurate-calculations.13723/

Apart from Jeremy's excellent work, I believe also Allycat did significant work on advanced maths for the Picaxe, but then maybe it is best to leave it to the master himself to give a proper entry point ... .

Regards,
Jurjen

EDIT: Added some relevant details to the above
 
Last edited:
Hi,

Yes, I have written several different versions of Basic code which divides "two words by one word" numbers. They're generally located in the "Code Snippets" section of the forum around the year 2012 (page 8).

My general "go to" program actually divides 31 bits by 15 bits (because PICaxe Basic does not natively support a "Carry" Bit/Flag), which requires less than 40 bytes of Program space and 7 bytes of RAM/registers. Note also the 2014 update in post #10 which uses less than 30 bytes of code.

A full 32-bit by 16-bit routine is documented HERE (together with some further links) but it looks quite "complicated" now, even though I wrote it myself !

Perhaps rather Off Topic, but an alternative solution that I adopted for my "Interpolation" Programs (for higher accuracy Trigonometry, etc.) was to automatically scale the 16-bit Word values to maintain resolution rather than increase to 32 bits:
Code:
    for b2 = 0 to 7                     ; Multiply numerator/divisor by 256 to give result = 0 - 255
        if wx < 32768 then             ; WX can be doubled
            wx = wx + wx            ; Multiply numerator by 2
        else            
            wy = wy / 2            ; Divide divisor by 2    
        endif    
    next
        wx = wx / wy max 255
Cheers, Alan.
 
Back
Top