Trig functions and square root in native batch?

后端 未结 2 1752
盖世英雄少女心
盖世英雄少女心 2021-01-27 23:08

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

:draw
echo   ^|\\
echo   ^|a\\
echo   ^|  \\
echo   ^|   \\
echo   ^|    \\ C
ec         


        
相关标签:
2条回答
  • 2021-01-27 23:33

    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
    
    0 讨论(0)
  • 2021-01-27 23:40

    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
    
    0 讨论(0)
提交回复
热议问题