numbers in variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: numbers in variables

#16 Post by orange_batch » 02 Dec 2011 23:57

Again it depends what you're trying to do, and you can change the digit where it splits (changes limitations).

The sum of split values are still limited to the 32-bit limit (2,147,483,647) so neither the "low" nor the "high" can exceed it, but...

4-digit split allows for 21,476,983,953,647 (21 terabyte range).

5-digit split allows for 214,750,512,183,647 (214 terabyte range), etc...

I use similar code in for loops to combine sets of numbers above 32-bits and operate on them, but I also use VBScript sometimes for single large-number operations.

Code: Select all

@echo off&setlocal enabledelayedexpansion

:: 55 gigabyte number: 55,123,456,789
set num1=55123456789

:: 44 gigabyte number: 44,765,320,877
set num2=44765320877

:: Digit where split occurs. Must be negative. Padding must be same number of 0s as split.
set split=-5
set pad=00000

for %%a in (%num1% %num2%) do (
set num=%%a
set lead=%pad%!num:~%split%!
set /a low+=1!lead:~%split%!-1%pad%,high+=!num:~,%split%!+0
)

:: Now we have split and combined num1 and num2 into low (ten-thousands range) and high (above ten-thousands).
:: Multiply numbers by 1000, result of entire operation is the same as (num1+num2)*1000.

set /a low*=1000,high*=1000

set /a result=!low:~,%split%!+%high%
set result=%result%!low:~%split%!

echo: ^> Result:    %result%
echo: ^> Should be: 99888777666000

pause
exit

The leading zero stripping prevents the hex/octal/decimal number error.

The +0 after high+= is just to prevent a missing operand error if there are no digits above 4 in any processed number.

Here's some limitation info on 4 and 5-digit splits:

Code: Select all

32-bit limit: 2,147,483,647

4-digit split:
low range max:  allows for adding 214769 values (files) of 9999 (low-range bytes)
high range example: [21,474,836,47-,---] allows for adding 21474 values (files) of 1,000,000,000 (1 GB size)

5-digit split:
low range max:  allows for adding 21475 values (files) of 99999 (low-range bytes)
high range example: [214,748,364,7--,---] allows for adding 214748 values (files) of 1,000,000,000 (1 GB size)

Quite acceptable boundaries.

Judago
Posts: 15
Joined: 04 Nov 2011 07:59

Re: numbers in variables

#17 Post by Judago » 03 Dec 2011 01:33

Thanks for the example orange_batch, I agree that they are sensible limits. Much easier to incorporate as a specialized workound ;)

Post Reply