Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Visual FoxPro Bignum class
Message
From
19/02/2014 21:27:43
 
 
To
15/02/2014 21:55:28
General information
Forum:
Visual FoxPro
Category:
Third party products
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01594440
Message ID:
01594746
Views:
77
I've created the framework for a big number (arbitrary precision) class for Visual FoxPro. You can see the code here:

https://github.com/RickCHodgin/libsf/tree/master/source/xbase

(You can download the project ZIP if you go to the main libsf project, and click the link, then extract that one directory.)

I have switched to a Precision library written by people from a different organization. It seems to be working and you can see how it operates if you like.
CD \libsf\source\xbase\bignum\
MODIFY PROJECT project\bignum.pjx
DO bp
DO go
*MODIFY FORM frmTest.scx
*MODIFY CLASS bignum OF bignum.vcx
Basically you can drop the bignum class label object onto a form, and name it "bn". Or create a singleton called "bn". Then, to allocate a big number, use this VFP code:
lnId1 = thisForm.bn.new()         && By default it uses 256-bit precision (you can change it on a class property)
lnId2 = thisForm.bn.new(0, 512)   && An example using 512-bit precision by single instance override, setting initial value to 0
lnId3 = thisForm.bn.new(3, 1024, .t.)  && Use 1024-bit precision, and store 1024 as the new default, setting initial value to 3

* If a singleton, use:
bn = CREATEOBJECT("bignum")
lnId1 = bn.new()

* In general, if you take the number of significant digits you want in base 10, and multiply by 3.3333333, and
* round up to the nearest 64 bits, that's how big you need your value to be.
* Using this bignum class will allow access to numbers that are literally megabytes long.
* You could find the next prime number. :-)
This returns an ID which lets you use it in later computations. To set a value:
thisForm.bn.set(lnId, "1234.56")
lcValue = thisForm.bn.to_text(lnId)
? lcValue
To do calculations, do things like this:
thisForm.bn.mul(lnId1, lnId2)    && Multiply the lnId1 bignum by the lnId2 bignum and store the result in lnId1
thisForm.bn.div(lnId1, lnId2)    && Divide the lnId1 bignum by the lnId2 bignum and store the result in lnId1
? thisForm.bn.to_text(lnId1)     && Show the value of lnId1 in text form

* Or if using a singleton
bn.mul(lnId1, lnId2)    && Multiply the lnId1 bignum by the lnId2 bignum and store the result in lnId1
bn.div(lnId1, lnId2)    && Divide the lnId1 bignum by the lnId2 bignum and store the result in lnId1
? bn.to_text(lnId1)     && Show the value of lnId1 in text form

* et cetera
The only issue I'm aware of is the number format. I'll get that sorted out at some point soon. If someone wants to write the algorithm to convert the current format to a non-scientific notation format, please do so and submit it and I'll update the project.

Up to 2,048 bignums can be active at any one time. To free and release a previous id = bn.new(), use bn.free(id).

-----
Use the frmTest.scx as an example of how to use the functions. Look at the code on the buttons, and trace through with SET STEP ON. Please ask any questions. Please report any bugs. Thank you.
Previous
Reply
Map
View

Click here to load this message in the networking platform