Trig functions and square root in native batch?

点点圈 提交于 2019-12-04 05:37:27

问题


I am making a tool where the user is shown this triangle throughout the process:

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

Where any letters are, there are variables. First the users chooses which angle value they have. Then they choose a side value. After that, all of the values will automatically be filled in. In my source code I just have sin(a) or something similar as a placeholder until I can find the trig functions (sin, cos, tan) and squareroot in native batch.

Code: http://pastebin.com/bDfY84Vr


回答1:


You may use a table (array really) to map the input value (degrees) into the sin of the value multiplied by a common factor, so you may achieve aritmethic operations with such intermediate result. For example:

@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

You may use the same method to get the result of any other function, or you may use an iterative method to calculate the square root.

Edit: Square root function added.

@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%

For example:

> SquareRoot.bat 214358881
14641

> SquareRoot.bat 14641
121

> SquareRoot.bat 121
11



回答2:


here this works:

@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



回答3:


You can USE vbscript: https://en.wikipedia.org/wiki/VBScript

The following example shows 30 decimals of the square root of x:

@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


来源:https://stackoverflow.com/questions/19604052/trig-functions-and-square-root-in-native-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!