【问题标题】:Trig functions and square root in native batch?本机批处理中的三角函数和平方根?
【发布时间】:2013-10-26 07:15:47
【问题描述】:

我正在制作一个工具,在整个过程中向用户显示这个三角形:

:draw
echo   ^|\
echo   ^|a\
echo   ^|  \
echo   ^|   \
echo   ^|    \ C
echo  A^|     \
echo   ^|      \
echo   ^|       \
echo   ^|c      b\
echo   ^|---------\
echo        B 
GOTO:EOF

哪里有字母,哪里就有变数。首先,用户选择他们拥有的角度值。然后他们选择一个边值。之后,所有值都将自动填充。在我的源代码中,我只有 sin(a) 或类似的占位符,直到我可以在本机批处理中找到三角函数(sin、cos、tan)和 squareroot。

代码:http://pastebin.com/bDfY84Vr

【问题讨论】:

  • 那么你的问题到底是什么?
  • 有没有人有我可以使用的脚本或函数,无论是正弦、余弦、正切还是平方根。批量。

标签: batch-file trigonometry square-root


【解决方案1】:

你可以使用一个表(array真的)将输入值(度)映射到该值乘以一个公因数的sin,这样你就可以用这样的中间结果来实现算术运算。例如:

@echo off 
setlocal EnableDelayedExpansion

call :DefineSinTable

set st=
For /L %%i in (1,1,52) do set st=#!st!

For /L %%x in (0,4,90) do (
   set /a "int_sinx_result=(SIN[%%x]*52)>>16"
   call set st_=%%st:~0,-!int_sinx_result!%%
   echo/!st_!
)

For /L %%x in (90,-4,0) do ( 
   set /a "int_sinx_result=(SIN[%%x]*52)>>16"
   call set st_=%%st:~0,-!int_sinx_result!%%
   echo/!st_!
)
goto :EOF


:DefineSinTable

rem Definition of SIN table values (SIN(x)*65535) for 0-360 degrees
rem Antonio Perez Ayala

set Quad1=0
for %%a in ( 1144  2287  3430  4572  5712  6850  7987  9121 10252 11380 12505 13626 14742 15855 16962 
            18064 19161 20252 21336 22415 23486 24550 25607 26656 27697 28729 29753 30767 31772 32768 
            33754 34729 35693 36647 37590 38521 39441 40348 41243 42126 42995 43852 44695 45525 46341 
            47143 47930 48703 49461 50203 50931 51643 52339 53020 53684 54332 54963 55578 56175 56756 
            57319 57865 58393 58903 59396 59870 60326 60764 61183 61584 61966 62328 62672 62997 63303 
            63589 63856 64104 64332 64540 64729 64898 65048 65177 65287 65376 65446 65496 65526 65535
           ) do (
   set /A Quad1+=1, Quad2=180-Quad1, Quad3=180+Quad1, Quad4=360-Quad1
   set SIN[!Quad1!]=%%a
   set SIN[!Quad2!]=%%a
   set SIN[!Quad3!]=-%%a
   set SIN[!Quad4!]=-%%a
)
for %%a in (0 180 360) do set SIN[%%a]=0
exit /B

您可以使用相同的方法来获得任何其他函数的结果,或者您可以使用迭代方法来计算平方根。

编辑:添加平方根函数。

@echo off

:SquareRoot number

set /A number=%1, last=2, sqrt=number/last
:nextIter
   set /A last=(last+sqrt)/2, sqrt=number/last
if %sqrt% lss %last% goto nextIter
echo %last%

例如:

> SquareRoot.bat 214358881
14641

> SquareRoot.bat 14641
121

> SquareRoot.bat 121
11

【讨论】:

    【解决方案2】:

    这里可行:

    @echo off
    echo what number do you want to get the sin cos and tan values from of?
    set /p in=num:
    for /f "delims=" %%a in (' powershell "[Math]::sin(%in%)" ') do set "sin=%%a"
    echo the sin of %in% is %sin%
    for /f "delims=" %%a in (' powershell "[Math]::cos(%in%)" ') do set "cos=%%a"
    echo the cos of %in% is %cos%
    for /f "delims=" %%a in (' powershell "[Math]::tan(%in%)" ') do set "tan=%%a"
    echo the tan of %in% is %tan%
    pause
    

    【讨论】:

    • 不完全符合要求的“本地批次”,但考虑到批次本身不能做到这一点......
    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 2018-02-28
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多