I want to create some batch files to answer math equations.This is a simple one of them
@echo off
echo r
set /p "r=>"
set /a "c=2*22/7*%r%"
echo %c%
pause
when i put 7 instead of 44 , its showing 42
please help me fix this, Thanks
I want to create some batch files to answer math equations.This is a simple one of them
@echo off
echo r
set /p "r=>"
set /a "c=2*22/7*%r%"
echo %c%
pause
when i put 7 instead of 44 , its showing 42
please help me fix this, Thanks
22/7
is a very rough value for PI. Better give it a fixed value.
Because set /a
can't calculate with floating points, you have to emulate that:
set pi=31416
set /p "r=>"
set /a c=2* pi * r
set /a c1=c/10000, c2=c %% 10000
echo %c1%.%c2%
Adapat to your required precision.
Note: set /a
doesn't require the %var%
syntax, just using var
is fine (that's only for set /a
!)
Here are my results:
r= 1, c=6.2832
r= 2, c=12.5664
r= 3, c=18.8496
r= 4, c=25.1328
r= 5, c=31.4160
r= 6, c=37.6992
r= 7, c=43.9824
r= 8, c=50.2656
r= 9, c=56.5488
r= 10, c=62.8320
r= 11, c=69.1152
r= 12, c=75.3984
r= 13, c=81.6816
r= 14, c=87.9648
r= 15, c=94.2480
r= 16, c=100.5312
r= 17, c=106.8144
r= 18, c=113.976
r= 19, c=119.3808
r= 20, c=125.6640
Edit 2018-05-15
22/7
isn't pi
. And calculating with each of those values is per definition inaccurate.
If you insist on those values (1 must be 6.2857(and more), 7 must be 44 , 14 must be 88
):
@echo off
set /p "r=>"
set /a c=1000000*44*r/7
set /a c1=c/1000000, c2=c %% 1000000
echo r=%r%, c=%c1%.%c2%
I included your "formula" for pi
into the calculation, reducing the errors (due to the "integer only" calculation) by first doing all multiplications before dividing .
Results of for /l %a in (1,1,20) do @echo %a|t
:
>r=1, c=6.285714
>r=2, c=12.571428
>r=3, c=18.857142
>r=4, c=25.142857
>r=5, c=31.428571
>r=6, c=37.714285
>r=7, c=44.0
>r=8, c=50.285714
>r=9, c=56.571428
>r=10, c=62.857142
>r=11, c=69.142857
>r=12, c=75.428571
>r=13, c=81.714285
>r=14, c=88.0
>r=15, c=94.285714
>r=16, c=100.571428
>r=17, c=106.857142
>r=18, c=113.142857
>r=19, c=119.428571
>r=20, c=125.714285
(Note: in a mathematical sense, this is even more inaccurate than my first solution, but it fit's your request)