LB Booster
Programming >> Compatibility with LB4 >> Arbitrary precision arithmetic
http://lbb.conforums.com/index.cgi?board=compatibility&action=display&num=1421230430

Arbitrary precision arithmetic
Post by Richard Russell on Jan 14th, 2015, 09:13am

Just about the only feature of Liberty BASIC not supported natively by LBB is the arbitrary precision integer arithmetic (which LB inherits from SmallTalk). This is very rarely needed, and anyway LBB has quite a wide integer range. For example LBB returns the same answer as LB 4.04 for this calculation:

Code:
    print int(12345678901234567890 / 255) 

However on the rare occasions when arbitrary precision arithmetic is needed it would be a shame to have to resort to using LB 4, so user CryptoMan and I have been doing some experimentation with the My Arbitrary Precision Math (MAPM) library.

For example here's a little program which performs an arbitrary precision integer division:

Code:
    open "BB4WMAPM.DLL" for DLL as #mapm
    struct MAPMerror, code as long
    MAPMresult$ = SPACE$(65536) + CHR$(0)

    A$ = "1234567890123456789012345678901234567890123456789012345678901234567890"
    B$ = "255"
    CALLDLL #mapm, "mapm_integer_divide", MAPMerror as struct, MAPMresult$ as ptr, _
      A$ as ptr, B$ as ptr, -1 as long, res as long
    CALLDLL #mapm, "mapm_to_integer_string", MAPMerror as struct, MAPMresult$ as ptr, _
      MAPMresult$ as ptr, res as long
    PRINT LEFT$(MAPMresult$, INSTR(MAPMresult$, CHR$(0)))

    CLOSE #mapm 

A nice thing about MAPM is that it's not limited to integers, so it can perform arbitrary precision calculations on floating-point values too! It even has trig functions built in.

There might be something to be said for creating a Liberty BASIC wrapper for MAPM. Any takers?

Richard.