3

Problem

Is there a way to put a variable "to the power" of a number or other variable a batch file? Does a function exist for this? An example of this would be in Python where you can use ** for "to the power of".

EDIT

You can do maths in a batch file... http://en.wikipedia.org/wiki/Batch_file

avitex
  • 2,478
  • 3
  • 22
  • 22

4 Answers4

5

The power to function is not available in batch scripting, as you may have already figured out from the answers.

One option is to use a loop. You can do the looping the way @Kirk Broadhurst did it last time he had to do the batch scripting, or you can use another way that has become available since then or otherwise may have gone unnoticed by Kirk:

:: calculate x^n
SET x=3
SET n=5
SET result=1
FOR /L %%i IN (1,1,%n%) DO SET /A result*=x
ECHO %result%

Another option is to use the approach described in this answer.

Community
  • 1
  • 1
Andriy M
  • 76,112
  • 17
  • 94
  • 154
1

You don't have many maths functions / operators to work with, and you don't have proper loops either so you need to simulate these.

The basic algorithm for x^n

result = 1
for (i = 0 ; i < n; i++)
{
    result = result * x;
}

In a batch file you'd need to use goto statements rather than a real loop.

set result=1
set i=1
:multiply
set /a result=result*x
set /a i=i+1
if %i% lss %n% goto multiply

This won't work for non-integer or negative / zero exponents, but you can work that out.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
0
@echo off

:start
echo Set the value of the Base 
set x=
set /p x=
echo set the power the Base is raised to 
set n= 
set /p n=
set y=%x%
If %n%==0 goto sol1
If %n%==1 goto solx
set /a n=%n%-1
set p=0

:eloop
set p=%P%
set /a p=%P%+1
set /a y=%y%*%x%
If %P%==%n% goto sol
goto :eloop

:sol1
set y=1
echo %y%
pause
goto :start

:solx
set y=%x%
echo %Y%
pause
goto start

:sol
set y=%y%
echo %y%
pause
goto start

This code works for all positive integers you select the base then the power the base is raised to it then loops the expression multiple times by adding 1 to a variable p until it matches a variable n multiplying x by itself each time then giving the solution it also gives a solution of %x% for n=1 and 1 for n=0.

Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
0

I really like the answer by @Andriy, but I'd add one thing to make it a reusable function.

@echo off

CALL :pow 2 3    :: 8
CALL :pow 3 3    :: 27
CALL :pow 5 5    :: 3125
CALL :pow 256 3  :: 16777216

set /p=End of Script, press any key to exit...
GOTO :EOF

:: ----- Call Functions -----
:pow
    SET pow=1
    FOR /L %%i IN (1,1,%2) DO SET /A pow*=%1
    ECHO %pow%
GOTO :EOF

P.S. You can also put the "functions" in files (for example "pow.bat", usage would be just "pow n n") and call them that way, which can be handy (especially if you start using the path variable). I've always found creating reusable functions in Batch to be the coolest but least known "feature" of the scripting language. Additionally, you'd be able to use the variable %pow% in your script (or assign it to another variable) until overwritten by calling the function again.

One last point I'd like to make is that while this is a fun exercise, there is a precision limitation to Batch.. I've found that batch fails to compute properly numbers greater than 2**31 (32 bit limitation).

Best!

jacktrader
  • 588
  • 5
  • 10