simple math

  • Thread starter Thread starter NuT CrAcKeR
  • Start date Start date
N

NuT CrAcKeR

Hello All,

A coworker asked if I knew how to do additon in a batch file, and while I
have done some jiggy things in the past, basic math opertations have not
been one of them.

Could someone enlighten me as to how I would take the values of a couple
variables, and add them together, and evaluate the sum?

Thanks,

NuTs
 
SET /A x=1+1
echo %x%


SET /A is the key. See SET /? for an explanation of SET /A.

Ray at work
 
That is simple enough...

how about adding 2 existing variables within the batch, that are not
environement variables?

NuTs
 
It's the same whether you enter literal numbers or variables.

SET a=3
SET b=4
set /a c=(%a%^2 + %b%^2)^.5

Damn. This doesn't work though. SET /A doesn't seem to like raising
numbers to decimal powers. Damn.

Ray at work
 
In
Ray at said:
It's the same whether you enter literal numbers or variables.

SET a=3
SET b=4
set /a c=(%a%^2 + %b%^2)^.5

Damn. This doesn't work though. SET /A doesn't seem to like raising
numbers to decimal powers. Damn.

Ray at work

Isn't "^" used for bitwise XOR, and not for exponents?

ws
 
That is simple enough...

how about adding 2 existing variables within the batch, that are not
environement variables?

set /a X=22+33

Clay Calvert
(e-mail address removed)
Replace "W" with "L"
 
Hello Ray.

Ray at work said:
It's the same whether you enter literal numbers or variables.

SET a=3
SET b=4
set /a c=(%a%^2 + %b%^2)^.5

Damn. This doesn't work though. SET /A doesn't seem to like raising
numbers to decimal powers. Damn.

Ray at work

The set arithmetic calculates with 32 bit integers.
Try this:
@ECHO OFF

SET /A a=3, b=4, c2 = a*a + b*b

CALL :SQUAREROOT %c2% c
ECHO Result: %c%
GOTO :EOF

:SQUAREROOT
SET /A test = %1, bits = 0, sqrt = 0, res = 0
IF /I %test% LEQ 0 GOTO END

:RESBITS
SET /A test ">>=" 1, bits += 1
IF /I %test% NEQ 0 GOTO RESBITS

SET /A test= bits "&" 1
IF /I %test% EQU 1 SET /A bits += 1

:LOOP
SET /A bits -= 2, res "<<=" 2, test = %1 ">>" bits, test "&=" 3
SET /A res += test, test= sqrt * 4 + 1, dig = 0
IF /I %test% LEQ %res% SET /A res -= test, dig = 1
SET /A sqrt += sqrt + dig
IF /I %bits% GTR 0 GOTO LOOP

:END
SET /A %2 = %sqrt%

The subroutine calculates the integer-part of the square root.
 
Very cool! Thanks Wolfgang.

Ray at work

Wolfgang Kais said:
Hello Ray.



The set arithmetic calculates with 32 bit integers.
Try this:
@ECHO OFF

SET /A a=3, b=4, c2 = a*a + b*b

CALL :SQUAREROOT %c2% c
ECHO Result: %c%
GOTO :EOF

:SQUAREROOT
SET /A test = %1, bits = 0, sqrt = 0, res = 0
IF /I %test% LEQ 0 GOTO END

:RESBITS
SET /A test ">>=" 1, bits += 1
IF /I %test% NEQ 0 GOTO RESBITS

SET /A test= bits "&" 1
IF /I %test% EQU 1 SET /A bits += 1

:LOOP
SET /A bits -= 2, res "<<=" 2, test = %1 ">>" bits, test "&=" 3
SET /A res += test, test= sqrt * 4 + 1, dig = 0
IF /I %test% LEQ %res% SET /A res -= test, dig = 1
SET /A sqrt += sqrt + dig
IF /I %bits% GTR 0 GOTO LOOP

:END
SET /A %2 = %sqrt%

The subroutine calculates the integer-part of the square root.
 
Back
Top