China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-08 01:21
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Square Root (precision to 5 decimal places) View 697 Replies 4
Original Poster Posted 2008-04-05 16:51 ·  中国 广东 广州 越秀区 电信
初级用户
Credits 38
Posts 10
Joined 2007-10-01 13:25
18-year member
UID 98696
Gender Male
From GZ
Status Offline
The algorithm idea was proposed by terse. I expanded the feasible range.

@echo off
set /p n=Enter the number to be square-rooted (0~89999999):
set a=2
:acr
set /a a=(n/a+a)/2,b=a*a
if %b% gtr %n% goto :acr
if %b% lss 0 goto acr
if %b% equ %n% goto :ok
set /a t=n-b,k=a
:loop1
set /a t*=100,x=k*20,m=10
:loop2
set /a m-=1,s=m*(m+x)
if %s% gtr %t% goto loop2
set /a t-=s,c+=1,k=k*10+m,r=r*10+m
if %c% lss 5 goto loop1
:ok
if not "%r%" == "" set a=%a%.%r%
echo Result: %a%
pause>nul


[ Last edited by dreamseed on 2008-4-5 at 05:13 PM ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
plp626 +6 2008-04-05 17:48
knoppix7 +8 2008-04-05 18:33
沉浸于脚本的流浪客。
纳须弥于芥子,容世界之脚本...
Floor 2 Posted 2008-04-05 17:08 ·  法国
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
() - grouping
! ~ - - unary operators
* / % - arithmetic operators
+ - - arithmetic operators
<< >> - logical shifts
- bitwise "and"
^ - bitwise "xor"
| - bitwise "or"
= *= /= %= += -= - assignment
&= ^= |= <<= >>=
, - expression separator

Make use of logical operators
菩提本无树,明镜亦非台,本来无一物,何处惹尘埃.
Floor 3 Posted 2008-04-05 17:12 ·  中国 广东 广州 越秀区 电信
初级用户
Credits 38
Posts 10
Joined 2007-10-01 13:25
18-year member
UID 98696
Gender Male
From GZ
Status Offline
Can you write out a concrete example?
沉浸于脚本的流浪客。
纳须弥于芥子,容世界之脚本...
Floor 4 Posted 2008-04-05 18:04 ·  中国 陕西 西安 电信
银牌会员
★★★★
钻石会员
Credits 2,278
Posts 1,020
Joined 2007-11-19 13:34
18-year member
UID 103127
Gender Male
Status Offline
Using batch files for math calculations, especially decimals, I really haven't studied that before,

Threads like this are just for fun, and can be kept around as a small utility function,

Let's study the algorithm
---------------------------
It's purely a computational mathematics problem, mainly the iterative formula: Xn+1=(Xn+A/Xn)/2 limit----->A^(1/2)

At the start, the OP sets X1 to 2, then in the :acr module iteratively computes the integer part of A^(1/2),

if %b% lss 0 goto acr is not redundant; it's there to prevent overflow into a negative number. I overlooked that

Also, setting X1 to n/2 can reduce the number of iterations,

As for the decimal part afterward, someone else can analyze it, I don't have the patience anymore

[ Last edited by plp626 on 2008-4-5 at 06:46 PM ]
山外有山,人外有人;低调做人,努力做事。

进入网盘(各种工具)~~ 空间~~cmd学习
Floor 5 Posted 2008-04-07 19:02 ·  中国 甘肃 兰州 电信
初级用户
Credits 47
Posts 31
Joined 2008-03-17 19:04
18-year member
UID 113236
Gender Male
Status Offline
Let the next person take a look, thanks.
Forum Jump: