1.

Solve : Square roots in a batch file??

Answer»

I'd like to start off saying I'm no expert in programming, so don't expect me to know exactly what you're talking about.  I'm trying to write a batch file that automatically solves quadratic equations, and I have most of it figured out.  I just can't figure out how to find the square root of a number.  It WOULD also be NICE if you could tell me how to graph something too.

Thanks,
JeffCheck out this page on Arithmetic Extraction of Square Roots

Quote

I'm trying to write a batch file that automatically solves quadratic equations, and I have most of it figured out

I, for one would truly love to see your code. hmm...that's pretty interesting.  Now I just have to figure out how to put that into a batch file 

I'll post the code once I have everything figured out...thanks for the helpCan anybody think of a way to put this into a batch file?  Quote from: juniorgiant8 on June 11, 2008, 02:05:45 PM
I'll post the code once I have everything figured out...thanks for the help

Quote from: Sidewinder on June 11, 2008, 01:58:32 PM
I, for one would truly love to see your code.

I would like to see it too. Show us what you've got so far, so we can understand where you are going with this. (It might help us to find the answer you need.)
dont batch files only work for cmd? how are you going to get cmd to solve your math problums?seriously, why should you use batch for such things as solving quadratic equations? Get a real programming language.! Since batch arithmetic only knows about integers, I think that finding square roots might be quite hard.

Here's what I have so far...
echo off

:input
echo what is A?
set /p A=
echo what is B?
set /p B=
echo what is C?
set /p C=
goto calculate

:calculate
set /a discrim=%B%*%B%-4*%A%*%C%
echo discriminant is %discrim%
goto positive

:positive
echo Is the discriminant positive?
set /p pos=
if %pos% equ y goto square
if %pos% lss y goto positive
if %pos% gtr y goto positive
if %pos% equ n goto imaginary
if %pos% lss n goto positive
if %pos% gtr n goto positive

:square
echo Is the discriminant a perfect square?
set /p sq=
if %sq% equ y goto graph
if %sq% lss y goto square
if %sq% gtr y goto square
if %sq% equ n goto quad
if %sq% lss n goto square
if %sq% gtr n goto square

:imaginary
echo Error...cannot be solved as roots are imaginary
goto input

:quad
set /a roots=

:graph



Also, I don't know any other programming languages yet I was just fooling around with batch files and came up with this idea...If you have any suggestions for a different programming language to use please tell me where a good guide is so I know how to use it. Code: [Select]if %pos% equ y goto square
I think you meant this

Code: [Select]if %pos% equ %y% goto square Quote
Can anybody think of a way to put this into a batch file?

I thought it might be interesting to follow the instructions from the link. This is actually easier to do with a pencil and paper than with batch language but if the ancient Greeks could do it, why not us? Except for perfect squares, square roots are irrational numbers and any mechanical method will only give close approximations.

Code: [Select]echo off
setlocal enabledelayedexpansion
set count=0
set /p dend=Enter Number:
set num=%dend%
for /l %%i in (%dend%, -1, 1) do (
set /a sqr=%%i*%%i
if !sqr! leq %dend% (
set digit=%%i.
  set root=%%i
goto out
)
)

:out
call set /a count=%%count%%+1
if %count% GTR 5 goto next
set /a dend=(%dend%-%sqr%)*100
set /a div=%root%*2
for /l %%i in (9,-1,0) do (
set /a sqr=%div%%%i*%%i
if !sqr! leq %dend% (
set root=%root%%%i
goto out
)
)

:next
set root=%root:~-5%
if %dend% neq 0 set digit=%digit%%root%
echo Square Root of %num% is %digit%

Precision is 5 decimals. The code can be tweaked if you require changes.

 

Note: This runs at glacial speed with large numbers.

Edit: I was typing this when you made your last post. Most script languages have a square root function which would put the above code to shame. Check out VBSCRIPT (installed with Windows), Python (requires download) or even the C language.

Good luck.



Quote from: Dias de verano on June 12, 2008, 01:17:15 PM
Code: [Select]if %pos% equ y goto square
I think you meant this

Code: [Select]if %pos% equ %y% goto square

No, I tried that in a program without using %y% and it worked just fine
y is supposed to mean yes. I guess I could add (y/n) to the question...Lol that looks really complicated Sidewinder.  I just tried it and it doesn't work...it just closes out after I enter the number.  I'll take a CLOSER look and try to figure out what is wrong.  Maybe I should look into another programming language I had to make a quick edit, but it only dealt with the format of the output line. I cannot duplicate your results. Did you cut and paste all the code? Posters in the past have had problems with copying code off the boards.

Quote
Lol that looks really complicated Sidewinder

That's why calculator were invented. yeah I couldn't get it to work... I think I copied the WHOLE thing


Discussion

No Comment Found