Batch Script limitação de 32 bits do inteiro

1

Existe alguma maneira de ultrapassar a limitação de 32 bits da operação matemática inteira do Batch. Eu estou fazendo alguma operação de adição no meu script e o valor da soma se torna mais do que o intervalo (2 ^ 31 -1).

Eu estou procurando por alguma idéia, incluindo algumas outras linhas de script como o VBscript ou algo parecido no meu script em lote.

    
por ajith 16.10.2014 / 08:03

2 respostas

0

Fonte Matemática em arquivos em lote do NT

Workarounds: 32-bit

Workarounds for the 32-bit limitation include:

  1. dividing by 1000 (or any power of 10) by chopping off the last (3) digits

  2. splitting up the numbers into separate decimal digits and perform all the math and carry logic "manually"

  3. other scripting languages

Workaround #1 can be used to add up disk space, for example:

"Chop" code example

The trick is that each (big) number is treated as strings, then the rightmost 6 characters (digits) are chopped off, and only then the result is treated as a number.

This is a rather crude workaround, as it "rounds" all numbers before doing the math. Adding half a MegaByte for each subdirectory (%Count% / 2) to %Total% does compensate for the truncations, though, so the grand total is more accurate than the individual numbers. Note that the numbers don't represent "real" MegaBytes (1024 x 1024) buth rather Million Bytes (1000 x 1000).

Workaround #2 is perfectly demonstarted by Brian Williams' batch files:

Add.bat

IsLarger.cmd

Multiply.cmd

Perfect, but quite complex.

Workaround #3, other scripting languages, is self-explanatory.

Workarounds: integers

There are no real workarounds that allow floating point math, except using other scripting languages.

The only exception may be if you have a limited and fixed number of decimals (e.g. 2), then you can just multiply everything by 100.

To display a decimal delimiter in the end results, concatenate the ineger divide by 100, followed by the decimal delimiter, followed by the modulo divide by 100:

SET Whole = Result / 100 SET "Fraction = Result %% 100" SET Result=%Whole%.%Fraction%

This may break on the 32-bit limit, though.

In general, for floating point math I would recommend using other scripting languages.

    
por 16.10.2014 / 09:58
0

Ferramentas externas são necessárias neste caso. Como ponto de partida, o bc (que suporta precisão arbitrária e é pré-instalado na maioria dos sistemas Linux) pode ser usado:

echo '123456789 * 123456789' | bc

    
por 16.10.2014 / 08:21