Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Square roots in a batch file?  (Read 22377 times)

0 Members and 1 Guest are viewing this topic.

juniorgiant8

    Topic Starter


    Greenhorn

    Square roots in a batch file?
    « 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

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Square roots in a batch file?
    « Reply #1 on: June 11, 2008, 01:58:32 PM »
    Check 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. 8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    juniorgiant8

      Topic Starter


      Greenhorn

      Re: Square roots in a batch file?
      « Reply #2 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

      juniorgiant8

        Topic Starter


        Greenhorn

        Re: Square roots in a batch file?
        « Reply #3 on: June 11, 2008, 06:02:42 PM »
        Can anybody think of a way to put this into a batch file?  ???

        llmeyer1000



          Intermediate

          Thanked: 1
          Re: Square roots in a batch file?
          « Reply #4 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.)

          computeruler



            Egghead

            Thanked: 63
            • Yes
            • Yes
          • Experience: Experienced
          • OS: Mac OS
          Re: Square roots in a batch file?
          « Reply #5 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?

          ghostdog74



            Specialist

            Thanked: 27
            Re: Square roots in a batch file?
            « Reply #6 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.!

            Dias de verano

            • Guest
            Re: Square roots in a batch file?
            « Reply #7 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.


            juniorgiant8

              Topic Starter


              Greenhorn

              Re: Square roots in a batch file?
              « Reply #8 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.

              Dias de verano

              • Guest
              Re: Square roots in a batch file?
              « Reply #9 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

              Sidewinder



                Guru

                Thanked: 139
              • Experience: Familiar
              • OS: Windows 10
              Re: Square roots in a batch file?
              « Reply #10 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)



              « Last Edit: June 12, 2008, 01:58:16 PM by Sidewinder »
              The true sign of intelligence is not knowledge but imagination.

              -- Albert Einstein

              juniorgiant8

                Topic Starter


                Greenhorn

                Re: Square roots in a batch file?
                « Reply #11 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...

                juniorgiant8

                  Topic Starter


                  Greenhorn

                  Re: Square roots in a batch file?
                  « Reply #12 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

                  Sidewinder



                    Guru

                    Thanked: 139
                  • Experience: Familiar
                  • OS: Windows 10
                  Re: Square roots in a batch file?
                  « Reply #13 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)
                  The true sign of intelligence is not knowledge but imagination.

                  -- Albert Einstein

                  juniorgiant8

                    Topic Starter


                    Greenhorn

                    Re: Square roots in a batch file?
                    « Reply #14 on: June 12, 2008, 01:45:50 PM »
                    yeah I couldn't get it to work... I think I copied the whole thing