Batch does not calculate equations properly

I recently tried to write a program in a package that calculates the square root of any real number (instead of negative numbers) without using external commands or tools, and the program is based on an algorithm that can be found here: https://en.wikipedia.org/wiki/ Shifting_nth_root_algorithm

EDIT: most of the problem I fixed, but I still left a little undetected problem.

Run this code "debug mode": http://pastebin.com/Rd8HGYKV and check the result on number 15625 and accuracy 3.

For the code to be correct, the debug code must be displayed:

- finalpart: 1
2
adder: 56
- finalpart: 44
3
adder: 25
- finalpart: 1225
4
adder: 0
Answer: 125

Please, help! Thank you in advance!

+4
2

. , , , . , . , , , , ; , , .

/2 , . .

@echo off
setlocal

:nextNumber
   set "number="
   set /P "number=Number:      "
   if not defined number goto :EOF
   call :SquareRoot %number% sqrt=
   echo Square root: %sqrt%
   echo/
goto nextNumber


:SquareRoot number result=
setlocal EnableDelayedExpansion

rem Separate the number in aligned blocks of 2 digits each
for /F "tokens=1,2 delims=." %%a in ("%1") do set "int=%%a" & set "frac=%%b"
set /A i=11, f=10
:nextInt
   if not defined int goto nextFrac
   set /A i-=1
   set "block[%i%]=%int:~-2%"
   set "int=%int:~0,-2%"
goto nextInt
:nextFrac
   if not defined frac goto checkLastBlock
   set /A f+=1
   set "block[%f%]=%frac:~0,2%"
   set "frac=%frac:~2%"
goto nextFrac
:checkLastBlock
if %f% gtr 10 if "!block[%f%]:~1!" equ "" set "block[%f%]=!block[%f%]!0"

rem Get square root of first block: digit between 0 and 9
set /A num=block[%i%], iP1=i+1, addZeros=0
for /L %%r in (0,1,9) do (
   set /A r2=%%r*%%r
   if !r2! leq %num% set /A sqrt=%%r, remainder=num-r2
)

rem Get square root of next blocks
for /L %%i in (%iP1%,1,%f%) do (
   set /A remainder1=remainder*10+!block[%%i]:~0,1!, remainder2=remainder*100+1!block[%%i]!-100, sqrtT2=sqrt*2
   if !sqrtT2! equ 0 (
      rem The number started with zeros: no sqrt yet
      set "sqrt="
      set /A addZeros+=1
      for /L %%r in (0,1,9) do (
         set /A r2=%%r*%%r
         if !r2! leq !remainder2! set /A nextDigit=%%r, remainder=remainder2-r2
      )
   ) else if !remainder1! lss !sqrtT2! (
      rem There is no sqrt for this block
      set /A nextDigit=0, remainder=remainder2
   ) else (
      set /A nextDigit=remainder1/sqrtT2, test=sqrtT2*10+nextDigit, this=test*nextDigit
      if !this! gtr !remainder2! (
         rem Next digit is too large: decrease it
         set /A "times=(this-remainder2)/test+1"
         for /L %%t in (1,1,!times!) do if !this! gtr !remainder2! (
            set /A nextDigit-=1, test=sqrtT2*10+nextDigit, this=test*nextDigit
         )
      )
      set /A remainder=remainder2-this
   )
   set "sqrt=!sqrt!!nextDigit!"
)

for /L %%i in (1,1,%addZeros%) do set "sqrt=0!sqrt!"
set /A point=11-i
set "sqrt=!sqrt:~0,%point%!.!sqrt:~%point%!"
endlocal & set "%2=%sqrt%"
exit /B

:

Number:      15625.000000
Square root: 125.000

Number:      625
Square root: 25.

Number:      64
Square root: 8.

Number:      9
Square root: 3.

Number:      0.25
Square root: 0.5

Number:      987654321987654321
Square root: 993807990.

Number:      1234567890123456789
Square root: 1111111106.

Number:      2.000000000000000000
Square root: 1.414213562
+2

vbscript: https://en.wikipedia.org/wiki/VBScript

30 .

:

@echo off
title SQR
set /p x=PLS Enter Your Number = 
echo.
echo Wscript.Echo (FormatNumber(SQR(Wscript.Arguments(0)),30))>Q.vbs
cscript //nologo Q.vbs %x% & DEL "Q.vbs"
PAUSE>NUL
0

Source: https://habr.com/ru/post/1612663/


All Articles