批处理无法正确计算方程式 [英] Batch does not compute equations properly

查看:80
本文介绍了批处理无法正确计算方程式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试批量编写一个程序,该程序无需使用外部命令或工具即可计算任何实数(而不是负数)的平方根,并且该程序基于以下算法:链接1

I recently tried to write a program in batch that calculates square root of any real number (instead of negative numbers) without using external commands or tools and the program is based on the algorithm that can be found here: Link1

编辑:我已解决了大多数问题,但仍有一些未被我发现的问题.

Most of the problem I fixed, but there is still a slight undetected by me problem.

请运行以下调试模式"代码: Link2 并测试输出的数字15625和精度3.

Please run this 'debug mode' code: Link2 and test the output for number 15625 and precision 3.

为了使代码正确,调试代码必须显示:

In order for the code to be correct, the debug code has to display:

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

推荐答案

不好意思.如果您希望我们检查您的代码以查找错误,我将为您提供帮助.您的代码很大,没有单个描述性注释,并且几个变量名容易造成混淆,并容易引起编辑错误.我查看了您与Wikipedia文章的链接,并且该方法对我来说似乎很有趣,但是当我将所描述的方法与您的程序进行比较时,代码似乎不必要地复杂,因此,我决定编写自己的方法版本.抱歉,这不是您要的.

Excuse me. If you want that we review your code looking for errors I can not help you. Your code is large and have not a single descriptive comment, and several variable names are confusing and prone to cause editing errors. I reviewed your link to the Wikipedia article and the method seemed interesting to me, but when I compared the described method with your program the code seemed unnecessarily complex, so I decided to write my own version of that method; I apologize if this is not what you asked for.

在此程序中,精度设置为输入数字的小数位数/2,但是非常简单的修改将允许将其设置为固定数字.我用多个数字测试了该程序,并且可以正常工作.

In this program the precision is set to the number of decimals / 2 of the input number, but a very simple modification would allow to set it to a fixed number. I tested this program with multiple numbers and works correctly.

@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

这篇关于批处理无法正确计算方程式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆