-4

How in the world do I do square roots in batch?

@echo off
Title SquareRoot

:SquareRoot
cls
echo Number:
set /p number=
set /a answer=sqrt %number%
echo Number: %number%
echo Answer: %answer%
pause
goto SquareRoot
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • you can use a utility, such as octave or bc. Ports are available for [windows](http://gnuwin32.sourceforge.net/). – user1095108 Oct 24 '18 at 20:19
  • 1
    There is no purpose built function for that. Would also like to inform you that `SET /A` can only process 32 bit Integers. Hope you were not expecting any decimal output. – Squashman Oct 24 '18 at 20:21
  • 1
    You have a few choices. You can call out to Powershell from your script. You could create a hybrid bat/jscript script. Or even create a quick vbscript to call out to. – Squashman Oct 24 '18 at 20:56

2 Answers2

2

Most newer operating systems comes with Powershell, so the easiest solution is to use that ability. You could do similar solutions with Vbscript and jscript.

@echo off
set /p number=Number:
for /F "delims=" %%G IN ('powershell -command "[math]::Sqrt(%number%)"') do @echo %%G

And here is a hybrid jscript/batch file.

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment
@echo off
set /P number=Number:
FOR /F "delims=" %%G IN ('cscript //E:JScript //nologo "%~f0" %number%') DO set sqr=%%G
echo Square root of %number% is %sqr%
pause
exit /b

************ JScript portion ***********/
WScript.StdOut.Writeline(Math.sqrt(WScript.Arguments.Unnamed(0)));
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • What is the variable the represent the square root, I'm look to do something so that after the math is done I'll do a "echo Answer: %answer%. – Oswald Ninjadragon Oct 27 '18 at 22:24
  • @OswaldNinjadragon, what's wrong with how I have it programmed? `echo Square root of %number% is %sqr%` – Squashman Oct 27 '18 at 23:16
  • I'm trying to understand how to use the answer that I get for other calculations. – Oswald Ninjadragon Oct 28 '18 at 14:31
  • When I do "echo Square root of %number% is %sqr%" it just shows me "Square root (number) is" I can seem to figure out how to make it say the answer of the square root. – Oswald Ninjadragon Oct 28 '18 at 16:34
  • I think I understand the problem, I was using the powershell version but the java script version seems to work better – Oswald Ninjadragon Oct 28 '18 at 16:42
  • @OswaldNinjadragon, both work just fine. Look at both of them. What is different? How does the square root variable get assigned in the Jscript version versus the Powershell version. Once you understand that you can fix the Powershell version to do the same thing. This is BATCH 101. The basics of every programming language are variable assignments. – Squashman Oct 28 '18 at 18:14
  • I figured it out thanks man. – Oswald Ninjadragon Oct 28 '18 at 20:34
1

Here is my solution:

@echo off

Title SquareRoot
:StartSquareRoot
cls
echo Number:
set /p number=

call :SquareRoot %number%

echo Number: %number%
echo Answer: %answer%

pause

goto StartSquareRoot

:SquareRoot
    SETLOCAL EnableDelayedExpansion
    set root=1
    set /a sqr=%root%*%root%
    :Loop
    if %sqr% LSS %number% (
        set /a root=!root!+1
        set /a sqr=!root!*!root!
        goto Loop
    )
(EndLocal && set answer=%root% && exit /B)

Example Run

C:\>sqrt.bat
Number:
25
Number: 25
Answer: 5
Press any key to continue . . .
Number:
36
Number: 36
Answer: 6
Press any key to continue . . .
Number:
49
Number: 49
Answer: 7
Press any key to continue . . .
Number:
56
Number: 56
Answer: 8
Press any key to continue . . .
abelenky
  • 63,815
  • 23
  • 109
  • 159