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 22376 times)

0 Members and 1 Guest are viewing this topic.

Sidewinder



    Guru

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

-- Albert Einstein

juniorgiant8

    Topic Starter


    Greenhorn

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

    Dias de verano

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


    juniorgiant8

      Topic Starter


      Greenhorn

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

      Dias de verano

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

      Sidewinder



        Guru

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

      -- Albert Einstein

      bob_man801



        Starter

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

        Sidewinder



          Guru

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

        -- Albert Einstein