Thanks a lot, Dave!dbenham wrote: Very cool and impressive Antonio
The example below add two numbers up to 18 digits each:dbenham wrote:... there is no similar method for addition/subtraction
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set A=%1
set B=%2
set highA=%A:~0,-9%& set lowA=%A:~-9%
set highB=%B:~0,-9%& set lowB=%B:~-9%
for /L %%a in (1,1,8) do (
if "!lowA:~0,1!" equ "0" set lowA=!lowA:~1!
if "!lowB:~0,1!" equ "0" set lowB=!lowB:~1!
)
set /A low=lowA+lowB, carry=low/1000000000, low%%=1000000000, high=highA+highB+carry
if %high% gtr 0 (
set low=00000000%low%
echo %high%!low:~-9!
) else (
echo %low%
)
Code: Select all
>AddBigNums 0 1
1
>AddBigNums 999999999 1
1000000000
>AddBigNums 999999999 999999999
1999999998
>AddBigNums 999999999999999999 1
1000000000000000000
>AddBigNums 123456789012345678 987654321098765432
1111111110111111110
You may also achieve operations with fractional parts implying the decimal point position (that is similar to the examples above). I stated a brief introduction to fractional operations at this post.
If you want to achieve any sequence of operations on big numbers, then original numbers must be split in groups of just 4 digits (quads) because 9999*9999 is the maximum result that fits in a 32-bits number. I wrote an (unfinished yet) library of BigNum functions; I copied this part from it:
Code: Select all
rem The format of a BigNum variable is:
rem
rem varInt, varInt-1, ..., var0 contain integer 4-digits groups (Quads)
rem var-1, var-2, ..., var-Frac contain fractional 4-digits groups (Quads)
rem var=Sign,Int,-Frac,-Last (BigNum descriptor)
rem Sign: 0 for positive, 1 for negative
rem Int: number of integer Quads minus 1
rem Frac: number of non-zero fractional Quads (with minus sign)
rem Last: number of total fractional Quads (with minus sign)
rem
rem For example: call SetBigNum var=12340008.43215432654376 result in:
rem var=0,1,-4,-4 var1=1234 var0=8 var-1=4321 var-2=5432 var-3=6543 var-4=7600
Let me know if I may help you any further.
Antonio