第二部分- :sqrt
- for /f "tokens=1-2 delims=." %%i in ("%~1") do (
- set "p1_int_sqrt=%%i"
- if "%%j"=="" (set /a p1_fd_sqrt=0) else set "p1_fd_sqrt=%%j"
- )
- call :int2bin "%p1_int_sqrt%"
- set "p1_int_sqrt_bin=%int_bin%"
- call :fd2bin "%p1_fd_sqrt%"
- set "p1_fd_sqrt_bin=%fd_bin%"
- set "p1_sqrt_bin=%p1_int_sqrt_bin%%p1_fd_sqrt_bin%"
- call :strlen "%p1_sqrt_bin%"&set /a before=len
- for /f "tokens=* delims=0" %%i in ("%p1_sqrt_bin%") do set "p1_sqrt_bin=%%i"&call :strlen "%%i"
- set /a zero_offset=len-before
- call :strlen "!p1_int_sqrt_bin!"
- set /a p1_sqrt_offset=len-1+zero_offset,e_de=127+p1_sqrt_offset
- call ::int2bin "%e_de%"
- set "e_bin=%int_bin%"
- set "zero_fill="
- for /l %%i in (1,1,8) do set "zero_fill=!zero_fill!0"
- set "e_bin=%zero_fill%%e_bin%"
- set "e_bin=%e_bin:~-8%"
- set "m=%p1_sqrt_bin:~1%"
- set "m=%m:~0,23%"
- set "p1_ieee=0%e_bin%%m%"
- rem echo,%p1_ieee%
- call :sub_bin "1011111001101110101101010000110" "0%p1_ieee:~0,-1%"
- call :bin2dec "%sub_bin_result:~1,8%"
- set /a sub_bin_result_offset=int_sum-127
- set "sub_bin_result_m=1%sub_bin_result:~-23%"
- if %sub_bin_result_offset% gtr 0 (set /a sub_bin_result_offset+=1)
- if %sub_bin_result_offset% gtr 0 (
- set "sub_bin_result_int=!sub_bin_result_m:~0,%sub_bin_result_offset%!"
- set "sub_bin_result_fd=!sub_bin_result_m:~%sub_bin_result_offset%!"
- ) else (
- for /l %%i in (-1,-1,%sub_bin_result_offset%) do set "sub_bin_result_m=0!sub_bin_result_m!"
- set "sub_bin_result_int=!sub_bin_result_m:~0,1!"
- set "sub_bin_result_fd=!sub_bin_result_m:~1!"
- )
- call :bin2dec "%sub_bin_result_int%" "%sub_bin_result_fd%"
- set "sqrt_result1=%bin2dec_result%"
- call :div "1" "%sqrt_result1:~0,9%"
- set "sqrt_result0=%quo_result%"
- rem echo,%sqrt_result0%
- for /f "tokens=1-2 delims=." %%i in ("%sqrt_result0%") do set "j=%%j"&set "sqrt_result0=%%i.!j:~0,9!"
- call :div "%sqrt_result0%" "2"
- set "x=%quo_result%"
- call :add "%~1" "0"
- for /f "tokens=1-2 delims=." %%i in ("%add_result%") do set "j=%%j"&set "pre_y=%%i.!j:~0,9!"
- call :div "%pre_y%" "%sqrt_result0:~0,9%"
- for /f "tokens=1-2 delims=." %%i in ("%quo_result%") do set "j=%%j"&set "y=%%i.!j:~0,9!"
- call :div "%y%" "2"
- set "y=%quo_result%"
- call :add "%x%" "%y%"
- set "sqrt_result00=%add_result%"
- for /f "tokens=1-2 delims=." %%i in ("%sqrt_result00%") do set "j=%%j"&set "sqrt_result00=%%i.!j:~0,9!"
- call :div "%sqrt_result00%" "2"
- set "x=%quo_result%"
- call :add "%~1" "0"
- for /f "tokens=1-2 delims=." %%i in ("%add_result%") do set "j=%%j"&set "pre_y=%%i.!j:~0,9!"
- call :div "%pre_y%" "%sqrt_result00:~0,9%"
- for /f "tokens=1-2 delims=." %%i in ("%quo_result%") do set "j=%%j"&set "y=%%i.!j:~0,9!"
- call :div "%y%" "2"
- set "y=%quo_result%"
- call :add "%x%" "%y%"
- set "sqrt_result=%add_result%"
- goto :eof
-
- :strlen
- set "s=%~1"
- set "zz=0 1 2 3 4 5 6 7 8 9 a b c d e f"
- if not defined list for %%a in (%zz%)do for %%b in (%zz%)do set list=!list!%%a%%b
- set "s=!list!!s!!s!"
- set /a len=0X%s:~-512,2%
- goto :eof
-
- :int2bin
- set /a i=%~1
- set "int_bin="
- if %i% gtr 0 (set /a s=0) else if %i% lss 0 (set /a s=1,i=-i) else (set /a int_bin=0,s=0&goto :eof)
- for /l %%i in (1,1,32) do if !i! neq 1 (
- set /a ii=i,i=i/2,bin=ii%%2
- set int_bin=!bin!!int_bin!) else (
- set int_bin=1!int_bin!
- goto :eof
- )
- goto :eof
-
- :fd2bin
- if %~1 equ 0 (
- set "zero_fill="
- for /l %%i in (1,1,32) do set "zero_fill=!zero_fill!0"
- set "fd_bin=!zero_fill!"
- goto :eof
- )
- set "fd_bin="
- set "f=0.%~1"
- for /l %%i in (1,1,32) do (
- call :add "!f!" "!f!"
- if !int_dec! geq 1 (set fd_bin=!fd_bin!1) else set fd_bin=!fd_bin!0
- set "f=0.!fd_dec!"
- )
- goto :eof
-
- :add_bin
- set "p1_bin=%~1"
- set "p2_bin=%~2"
- set "bin_result="
- set /a carry=0
- call :strlen "%p1_bin%"
- set /a p1_bin_len=%len%
- call :strlen "%p2_bin%"
- set /a p2_bin_len=%len%
- if %p1_bin_len% geq %p2_bin_len% (set /a bin_len_max=p1_bin_len+1) else set /a bin_len_max=p2_bin_len+1
- for /l %%i in (-1,-1,-%bin_len_max%) do (
- if %%i lss -%p1_bin_len% (set /a p1_bin_place=0) else set "p1_bin_place=!p1_bin:~%%i,1!"
- if %%i lss -%p2_bin_len% (set /a p2_bin_place=0) else set "p2_bin_place=!p2_bin:~%%i,1!"
- set /a bin_place=!p1_bin_place!+!p2_bin_place!+!carry!
- if !bin_place! geq 2 (set /a carry=bin_place/2,bin_place=bin_place-2) else set /a carry=0
- set "bin_result=!bin_place!!bin_result!"
- )
- set "add_bin_result=%bin_result%"
- goto :eof
-
- :bin2dec
- set "int_bin=%~1"
- set "fd_bin=%~2"
- set /a int_sum=0
- if "%~1"=="" goto :fd_bin2dec
- call :strlen "%int_bin%"
- set /a int_len=len
- set /a bin2dec_n=1,int_sum=0
- for /l %%i in (-1,-1,-%int_len%) do (
- if "!int_bin:~%%i,1!"=="1" (
- call :add "!int_sum!" "!bin2dec_n!"
- set "int_sum=!add_result!"
- )
- call :add "!bin2dec_n!" "!bin2dec_n!"
- set "bin2dec_n=!add_result!"
- )
- if "%~2"=="" goto :eof
- :fd_bin2dec
- set "bin2dec_half=0.5"
- set /a fd_sum=0
- for /l %%i in (0,1,32) do if not "!fd_bin:~%%i,1!"=="" (
- if "!fd_bin:~%%i,1!"=="1" (
- call :add "!fd_sum!" "!bin2dec_half!"
- set "fd_sum=!add_result!"
- )
- call :fd_half "!bin2dec_half!"
- set "bin2dec_half=!fd_half!"
- )
- call :add "%int_sum%" "%fd_sum%"
- set "bin2dec_result=%add_result%"
- goto :eof
-
- :sub_bin
- set "sub_bin_p1=%~1"
- set "sub_bin_p2=%~2"
- set "sub_bin_result="
- set /a sub_bin_carry=0
- for /l %%i in (-1,-1,-32) do (
- set /a sub_bin_p1_place=!sub_bin_p1:~%%i,1!+sub_bin_carry
- set "sub_bin_p2_place=!sub_bin_p2:~%%i,1!"
- if !sub_bin_p1_place! lss !sub_bin_p2_place! (set /a sub_bin_p1_place+=2,sub_bin_carry=-1
- ) else set /a sub_bin_carry=0
- set /a diffe=sub_bin_p1_place-sub_bin_p2_place
- set "sub_bin_result=!diffe!!sub_bin_result!"
- )
- goto :eof
-
- :fd_half
- set "fd_half="
- set "one=%~1"
- set "one=%one:*.=%"
- for /l %%i in (0,1,32) do (
- if "!one:~%%i,1!"=="" (set "one_i=!pre!0") else set "one_i=!pre!!one:~%%i,1!"
- set /a one_ii=one_i,one_i=one_i/2,one_mod=one_ii%%2
- if "!one_mod!"=="0" (set "pre=") else set /a pre=one_mod
- set "fd_half=!fd_half!!one_i!"
- if "!one:~%%i,1!"=="" (if "!one_mod!"=="0" goto :fd_half_next)
- )
- :fd_half_next
- set "fd_half=0.%fd_half%"
- goto :eof
复制代码
|