-2

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

Anuja Nimesh
  • 408
  • 3
  • 13
  • 2
    What do you expect? `set /a` does Integer only (INT32). So 2*7/22*x = 14/22*x = 0 *x (14/22 = 0.63636... and Integer of that is simply 0. And 0*x stays 0 – Stephan Feb 25 '18 at 12:06
  • @Stephan ,so then how ? – Anuja Nimesh Feb 25 '18 at 12:10
  • how *what*? What's your input, what's your expected output, what precision do you need? – Stephan Feb 25 '18 at 12:11
  • @Stephan , my mistake its 2*22/7*%r% lets think r=7 so even when i do that instead of 44 ,its 42 in cmd – Anuja Nimesh Feb 25 '18 at 12:19
  • In spite of what you learnded in school, `2*22/7*3` is not the same as `(2*22)/(7*3)` (because of the "Integer-Only-thing"). I don't know your intention, but in general: be more precise as you are used to with "normal" math. – Stephan Feb 25 '18 at 12:29
  • would you please say what should put for `c` instead of telling how – Anuja Nimesh Feb 25 '18 at 12:53
  • I can't, because I don't know your intention (what you want to achieve) – Stephan Feb 25 '18 at 12:55
  • I want to find circumference ,that's my intention – Anuja Nimesh Feb 25 '18 at 12:57
  • Possible duplicate of [SET Command - Floating point numbers?](https://stackoverflow.com/questions/25951196/set-command-floating-point-numbers) – phuclv Feb 25 '18 at 15:02
  • [Floating point division in a batch file](https://stackoverflow.com/q/1503888/995714), [decimals in batch script](https://stackoverflow.com/q/18689450/995714) – phuclv Feb 25 '18 at 15:03
  • @Stephan `2*22/7*3` is not the same as `(2*22)/(7*3)` in school, too. Because division and multiplication have the same precedence there, which is also consistent with programming languages. And I don't know why but most questions about pi being 22/7 are from Indians – phuclv Feb 25 '18 at 15:06

1 Answers1

0

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)

Stephan
  • 53,940
  • 10
  • 58
  • 91