Computer Hope

Microsoft => Microsoft DOS => Topic started by: juniorgiant8 on June 11, 2008, 01:47:08 PM

Title: Square roots in a batch file?
Post by: juniorgiant8 on June 11, 2008, 01:47:08 PM
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,
Jeff
Title: Re: Square roots in a batch file?
Post by: Sidewinder on June 11, 2008, 01:58:32 PM
Check out this page on Arithmetic Extraction of Square Roots (http://www.tpub.com/content/engineering/14069/css/14069_25.htm)

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. 8)
Title: Re: Square roots in a batch file?
Post by: juniorgiant8 on June 11, 2008, 02:05:45 PM
hmm...that's pretty interesting.  Now I just have to figure out how to put that into a batch file  :o

I'll post the code once I have everything figured out...thanks for the help
Title: Re: Square roots in a batch file?
Post by: juniorgiant8 on June 11, 2008, 06:02:42 PM
Can anybody think of a way to put this into a batch file?  ???
Title: Re: Square roots in a batch file?
Post by: llmeyer1000 on June 11, 2008, 06:45:02 PM
I'll post the code once I have everything figured out...thanks for the help

I, for one would truly love to see your code. 8)

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.)
Title: Re: Square roots in a batch file?
Post by: computeruler on June 11, 2008, 07:20:09 PM
dont batch files only work for cmd? how are you going to get cmd to solve your math problums?
Title: Re: Square roots in a batch file?
Post by: ghostdog74 on June 11, 2008, 10:38:24 PM
seriously, why should you use batch for such things as solving quadratic equations? Get a real programming language.!
Title: Re: Square roots in a batch file?
Post by: Dias de verano on June 12, 2008, 12:17:47 AM
Since batch arithmetic only knows about integers, I think that finding square roots might be quite hard.

Title: Re: Square roots in a batch file?
Post by: juniorgiant8 on June 12, 2008, 01:13:41 PM
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.
Title: Re: Square roots in a batch file?
Post by: 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
Title: Re: Square roots in a batch file?
Post by: Sidewinder on June 12, 2008, 01:21:33 PM
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.

 8)

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. 8)



Title: Re: Square roots in a batch file?
Post by: juniorgiant8 on June 12, 2008, 01:28:20 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...
Title: Re: Square roots in a batch file?
Post by: juniorgiant8 on June 12, 2008, 01:33:09 PM
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 :D
Title: Re: Square roots in a batch file?
Post by: Sidewinder on June 12, 2008, 01:40:52 PM
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. 8)
Title: Re: Square roots in a batch file?
Post by: juniorgiant8 on June 12, 2008, 01:45:50 PM
yeah I couldn't get it to work... I think I copied the whole thing
Title: Re: Square roots in a batch file?
Post by: Sidewinder on June 12, 2008, 01:55:40 PM
I'll repost it in reply #10. Looks good though, just as it was written. You could try turning echo on and seeing where the error is, but as I mentioned I cannot duplicate your results.

 8)

You could always type the code yourself.....the ancient Greeks did. ;D
Title: Re: Square roots in a batch file?
Post by: juniorgiant8 on June 12, 2008, 02:00:06 PM
Ok I figured out what was wrong.  You needed to add pause at the end of it so that it gave me enough time to see the answer.  It was just closing after it finished and it went too fast for me to see the square root. Thanks a bunch!
Title: Re: Square roots in a batch file?
Post by: Dias de verano on June 12, 2008, 02:00:34 PM

Note: This runs at glacial speed with large numbers.

Bravo, Sidewinder!

It runs out of precision pretty soon anyway.

I don't know why the others had problems; I copied it straight off the forum into Notepad and saved it. I added a couple of timing statements. It runs just fine from the prompt.

Code: [Select]
S:\Test\>sqrt.cmd
Enter Number:10
start 20:56:59.38
done  20:56:59.42
Square Root of 10 is 3.16227

S:\Test>sqrt.cmd
Enter Number:100
start 20:57:17.45
done  20:57:17.52
Square Root of 100 is 10.

S:\Test>sqrt.cmd
Enter Number:1000
start 20:57:27.60
done  20:57:27.75
Square Root of 1000 is 31.62277

S:\Test>sqrt.cmd
Enter Number:10000
start 20:57:37.53
done  20:57:38.55
Square Root of 10000 is 100.

S:\Test>sqrt.cmd
Enter Number:100000
start 20:57:53.14
Invalid number.  Numbers are limited to 32-bits of precision.
done  20:57:57.63
Square Root of 100000 is 92682.43999

Title: Re: Square roots in a batch file?
Post by: juniorgiant8 on June 12, 2008, 02:09:16 PM
Also an update on the code that I posted earlier...I had to change the order of the If statements, so the if statements should look like:

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

And:
Quote
:square
echo Is the discriminant a perfect square?
set /p sq=
if %sq% equ y goto graph
if %sq% equ n goto quad
if %sq% lss n goto square
if %sq% gtr n goto square
Title: Re: Square roots in a batch file?
Post by: Dias de verano on June 12, 2008, 02:15:55 PM
Whoops!

Code: [Select]
S:\Test>sqrt.cmd
Enter Number:50000
start 21:14:46.88
done  21:14:50.53
Square Root of 50000 is 50000.50000
Title: Re: Square roots in a batch file?
Post by: Sidewinder on June 12, 2008, 03:21:57 PM
Quote
Whoops!

The code was written as a throwaway exercise. The max number I could get to work is 46341. Had I followed the instructions exactly, I might have eliminated the error. But by finding the highest square in the number using the arithmetic iterations, I may have exceeded 32 bit addressability.

Oh yeah, it won't find the square root of -1 either.

 ;D
Title: Re: Square roots in a batch file?
Post by: bob_man801 on August 22, 2008, 06:57:15 AM
@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%

look at what happens if you type in 9.5(remember to PAUSE at the end)
infact it does not seem to be able to do any decimal numbers with 0.1's or smaller
Title: Re: Square roots in a batch file?
Post by: Sidewinder on August 22, 2008, 03:31:05 PM
bob_man801,

Aside from resurrecting a two month old post, and having read the entire thread you probably learned that:

1) batch code can only do integer arithmetic
2) except for perfect squares, square roots are irrational

This thread morphed into a whimsical attempt to find square roots using batch code. Unfortunately,  both 1 & 2 above are at direct odds with one another. Beside the fact that no one in their right mind would use batch code for this solution, there is a loss of precision with large numbers. Even the ancient Greeks could do no better than approximations.

Most languages have functions to handle this situation. Batch code has no functions, much less one for square roots.

PS. Until 9.5 grows up to be a 10, it is not an integer. ;D