[AccessD] Math in VBA

Jurgen Welz jwelz at hotmail.com
Thu Jul 19 12:07:45 CDT 2007


You can perform math on the string just like you do it with pen and paper.  
For multiplication, start with the last digit of the 2nd number, multiply 
each digit of the first number, add the carries as you go and prepend the 
numbers to your string.  Then take the 2nd last number, multiply each from 
the last digit, add the carries and then add the existing product to your 
current product, again as strings and add the carries, just like you would 
do it on a piece of paper.  Keep going until you've gotten through all the 
digits.

Division is a bit tougher because you need to truncate and then test with 
the multiplication whether you are above or below the number you're dividing 
into and the truncation can require you to try a lower digit if the 
additional digits put you over.

All you really need to do is write an algorithm that works exactly the way 
you do it with pencil and paper.  When we do multiplication, division, 
additon or subraction, we generally work with 2 'characters' at a time and 
that is fairly easy to mimic in VBA.  There is no reason you couldn't do 
10,000 digit numbers with complete precision.

I've never seen such an algorithm but I'm sure they exist.  If I needed it, 
I imagine I could write my own and get all four operators running in an hour 
or so.  I'd start with an addition function; subraction is just adding a 
negative number and then multiplication and division would call the addition 
(and -) function with the intermediate solutions.  I don't imagine it would 
be terribly fast, but 16 digits should be a piece of cake for a computer.

Ciao
Jürgen Welz
Edmonton, Alberta
jwelz at hotmail.com





>From: "Mark A Matte" <markamatte at hotmail.com>
>
>Hello All,
>
>This is just a 'mild' form of encryption...but I need to use math(+-*/) 
>with numbers up to 16 digits(9,999,999,999,999,999)...
>
>Is there any way to do this without losing accuracy in VBA?
>
>Thanks,
>
>Mark A. Matte

_________________________________________________________________
New Windows Live Hotmail is here. Upgrade for free and get a better look. 
www.newhotmail.ca?icid=WLHMENCA150




More information about the AccessD mailing list