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

Author Topic: Finding the Area of a Circle  (Read 7080 times)

0 Members and 1 Guest are viewing this topic.

zask

    Topic Starter


    Intermediate

    • Experience: Experienced
    • OS: Other
    Finding the Area of a Circle
    « on: February 17, 2016, 09:56:25 PM »
    okay so lets say i had the radius of a circle, im trying to find the area.

    The area of a circle can be found by multiplying pi ( n = 3.14) by the square of the radius
    If a circle has a radius of 4, its area is 3.14*4*4=50.24
    If you know the diameter, the radius is 1/2 as large.

    tried this hunk of junk

    @echo off
    set /p "RESULTA=Enter radius : "
    set /a "RESULTB = 3.14 * %RESULTA% * %RESULTA%"
    echo Your result is "%RESULTB%".
    pause

    But i cant use decimals in batch so i made a work around
    314 divided by 100 times RESULTA also gives you the area,
    but it's not rounded.
     
    @echo off
    set /p "RESULTA=Enter radius : "
    set /a "RESULTB = 314 / 100 * %RESULTA% * %RESULTA%"
    echo Your result is "%RESULTB%".
    pause

    last thing i need to do is round RESULTB, how can i do this in a batch file, i know its possible i just dont know how.
    « Last Edit: February 17, 2016, 10:32:42 PM by zask »

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Finding the Area of a Circle
    « Reply #1 on: February 17, 2016, 11:18:52 PM »
    Don't try to round it off.
    Make a print feature that can represent decimals values. Use integers

    Write a batch stub that will represent the decimal visual presentation of  an integer divided by another.
    Example. A stub called _show_divide  is called this way:
    Code: [Select]
    call _show_divide 314 100
    ...the output is
    Quote
    3.14
    Sorry I can't do it for you. Would take me three hours.  :)

    zask

      Topic Starter


      Intermediate

      • Experience: Experienced
      • OS: Other
      Re: Finding the Area of a Circle
      « Reply #2 on: February 18, 2016, 05:43:10 PM »
      sorry i dont understand exactly what you mean, how do i do it your way?

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Finding the Area of a Circle
      « Reply #3 on: February 19, 2016, 02:18:29 PM »
      Just multiply 314 * radius * radius.
      Then use an implied decimal.  The last two numbers in your output would be your decimal output.
      Code: [Select]
      H:\>set /a area=314 * 5 * 5
      7850
      H:\>echo area=%area:~0,-2%.%area:~-2%
      area=78.50

      H:\>
      « Last Edit: February 19, 2016, 02:40:10 PM by Squashman »

      zask

        Topic Starter


        Intermediate

        • Experience: Experienced
        • OS: Other
        Re: Finding the Area of a Circle
        « Reply #4 on: February 20, 2016, 04:43:43 PM »
        Thank you this is exactly what i'm looking for

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Finding the Area of a Circle
        « Reply #5 on: February 20, 2016, 08:26:35 PM »
        But also realize at some point your are going to exceed the 32 bit integer limit.

        zask

          Topic Starter


          Intermediate

          • Experience: Experienced
          • OS: Other
          Re: Finding the Area of a Circle
          « Reply #6 on: February 20, 2016, 09:30:02 PM »
          But also realize at some point your are going to exceed the 32 bit integer limit.


          ive realized that, is there any way around this?

          DaveLembke



            Sage
          • Thanked: 662
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: Finding the Area of a Circle
          « Reply #7 on: February 21, 2016, 12:39:43 PM »
          Scaling it down is one way to do this, but you will lose precision. Same as the 3.14 turning into 314 where you only have precision to 3.14xxxxxxxxxxx-> and no other digits to infinity.

          You can scale it down to do the math and then in the end scale it back up to avoid exceeding 32 bit integer when the product of the equation itself will exceed 32 bit but the answer itself is smaller than 32-bit. With your equation you could bring it to 31 for 3.1, losing the hundreths precision, but it gets ugly fast when you reduce the precision. This use for greater than 32 bit is best for a real language intended for large numbers and precision. Something that batch isnt very good at alone.

          This is something that is far more easier to accomplish with just about any language other than batch as well as you would have a greater depth of precision if you did this with a real programming language geared for mathematical equations such as I have a program I wrote that does crypto shuffling and I was looking for poor keys that would shuffle back to their original characters for up to 1 Trillion Keys. It took an 8-core AMD FX-8350 at 4000Mhz a week to process all of these keys at 125 Billion Keys processed per core running full tilt. I had to use Long Long INT's to achieve this with C++.

          In the end I added a routine that counts between 2 arrays matching pairs for each element and it avoids use of keys that exceed a specified allowance of matching input and output characters. I can set it to 0 and Z will never come back out as Z etc. or set it to 2 in which 2 characters can be same with rest shuffled etc. So the week of crunching was pretty much a waste but interesting as to how many combinations had 10 or more unshuffled characters which would make the output weak and potentially able to be decrypted too easily.

          Salmon Trout

          • Guest
          Re: Finding the Area of a Circle
          « Reply #8 on: February 21, 2016, 01:11:55 PM »
          VBScript handles floats.

          patio

          • Moderator


          • Genius
          • Maud' Dib
          • Thanked: 1769
            • Yes
          • Experience: Beginner
          • OS: Windows 7
          Re: Finding the Area of a Circle
          « Reply #9 on: February 21, 2016, 01:51:17 PM »

          ive realized that, is there any way around this?

          Only if you want to re-write the mathematics of geometry...
          " Anyone who goes to a psychiatrist should have his head examined. "

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: Finding the Area of a Circle
          « Reply #10 on: February 21, 2016, 02:11:52 PM »
          A few years ago, a user named Judago wrote some pure batch files to do floating point math and it also did numbers larger than 32 bits.  I use his division batch file in one of my scripts. You might be able to find his multiply batch file. His website no longer exists though.

          patio

          • Moderator


          • Genius
          • Maud' Dib
          • Thanked: 1769
            • Yes
          • Experience: Beginner
          • OS: Windows 7
          Re: Finding the Area of a Circle
          « Reply #11 on: February 21, 2016, 02:46:59 PM »
          " Anyone who goes to a psychiatrist should have his head examined. "

          DaveLembke



            Sage
          • Thanked: 662
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: Finding the Area of a Circle
          « Reply #12 on: February 22, 2016, 05:27:41 AM »
          This caught my interest when I read that someone made a work around in batch....No hits on pure batch on google, but I found this article here, but its not pure batch they are using VBScript. http://www.celticproductions.net/articles/11/batch+files/add+floating+point+numbers+in+batch+file.html

          zask

            Topic Starter


            Intermediate

            • Experience: Experienced
            • OS: Other
            Re: Finding the Area of a Circle
            « Reply #13 on: February 24, 2016, 11:36:02 AM »
            thank you this information is very helpful, it's hard finding out ways to do this in pure batch but ive seen it done before i just cant remember where to look. the vbs helps for now, i guess ill just have to delete the vbs file after it's use.

            Salmon Trout

            • Guest
            Re: Finding the Area of a Circle
            « Reply #14 on: February 24, 2016, 01:50:06 PM »
            i guess ill just have to delete the vbs file after it's use.
            You don't have to delete it if you don't want to. It's only 45 bytes. (About 7 years ago I thought I had independently invented this!)
            Code: [Select]
            Wscript.echo eval(WScript.Arguments(0))You could even put it somewhere on your PATH, set the default vbscript engine to Cscript, and call it by name:

            C:\>type c:\batch\eval.vbs
            Wscript.echo eval(WScript.Arguments(0))

            C:\>dir c:\batch\eval.vbs
             Volume in drive C is WIN-SSD1
             Volume Serial Number is 9C5F-D658

             Directory of c:\batch

            11/03/2007  18:21                45 eval.vbs
                           1 File(s)             45 bytes
                           0 Dir(s)  34,618,871,808 bytes free

            C:\>eval 2+1
            3

            C:\>eval now
            24/02/2016 20:41:11

            C:\>eval weekdayname(weekday(now))
            Wednesday

            C:\>eval 4*ATN(1)
            3.14159265358979

            C:\>for /f "delims=" %A in ('eval 35.333/2.277') do @set result=%A

            c:\>echo %result%
            15.5173473869126

            C:\>eval weekdayname(weekday(now))
            Wednesday




            Salmon Trout

            • Guest
            Re: Finding the Area of a Circle
            « Reply #15 on: February 25, 2016, 01:33:43 PM »
            You could even put it somewhere on your PATH, set the default vbscript engine to Cscript, and call it by name:
            And, of course, if you haven't already done so, set the default Cscript logo option to NOLOGO.

            Salmon Trout

            • Guest
            Re: Finding the Area of a Circle
            « Reply #16 on: February 26, 2016, 01:03:00 AM »
            Python oneliner similar to eval.vbs:
            Code: [Select]
            import sys; from math import *; print eval(sys.argv[1])
            Use decimal point to avoid int results from integer values in Python 2.xx e.g. 3/2.0 (I believe Python 3 defaults to float?)

            Examples

            C:\>eval.py 5/3
            1

            C:\>eval.py 5/3.0
            1.66666666667

            C:\>eval.py sqrt(3)
            1.73205080757

            C:\>eval.py pi
            3.14159265359

            C:\>eval.py e
            2.71828182846
            « Last Edit: February 26, 2016, 01:27:14 AM by Salmon Trout »

            zask

              Topic Starter


              Intermediate

              • Experience: Experienced
              • OS: Other
              Re: Finding the Area of a Circle
              « Reply #17 on: April 07, 2016, 09:51:51 PM »
              Scaling it down is one way to do this, but you will lose precision. Same as the 3.14 turning into 314 where you only have precision to 3.14xxxxxxxxxxx-> and no other digits to infinity.

              You can scale it down to do the math and then in the end scale it back up to avoid exceeding 32 bit integer when the product of the equation itself will exceed 32 bit but the answer itself is smaller than 32-bit. With your equation you could bring it to 31 for 3.1, losing the hundreths precision, but it gets ugly fast when you reduce the precision. This use for greater than 32 bit is best for a real language intended for large numbers and precision. Something that batch isnt very good at alone.

              This is something that is far more easier to accomplish with just about any language other than batch as well as you would have a greater depth of precision if you did this with a real programming language geared for mathematical equations such as I have a program I wrote that does crypto shuffling and I was looking for poor keys that would shuffle back to their original characters for up to 1 Trillion Keys. It took an 8-core AMD FX-8350 at 4000Mhz a week to process all of these keys at 125 Billion Keys processed per core running full tilt. I had to use Long Long INT's to achieve this with C++.

              In the end I added a routine that counts between 2 arrays matching pairs for each element and it avoids use of keys that exceed a specified allowance of matching input and output characters. I can set it to 0 and Z will never come back out as Z etc. or set it to 2 in which 2 characters can be same with rest shuffled etc. So the week of crunching was pretty much a waste but interesting as to how many combinations had 10 or more unshuffled characters which would make the output weak and potentially able to be decrypted too easily.

              okay good to know, could i maybe borrow one of your C++ examples?

              zask

                Topic Starter


                Intermediate

                • Experience: Experienced
                • OS: Other
                Re: Finding the Area of a Circle
                « Reply #18 on: April 12, 2016, 07:23:14 PM »
                You don't have to delete it if you don't want to. It's only 45 bytes. (About 7 years ago I thought I had independently invented this!)
                Code: [Select]
                Wscript.echo eval(WScript.Arguments(0))You could even put it somewhere on your PATH, set the default vbscript engine to Cscript, and call it by name:

                C:\>type c:\batch\eval.vbs
                Wscript.echo eval(WScript.Arguments(0))

                C:\>dir c:\batch\eval.vbs
                 Volume in drive C is WIN-SSD1
                 Volume Serial Number is 9C5F-D658

                 Directory of c:\batch

                11/03/2007  18:21                45 eval.vbs
                               1 File(s)             45 bytes
                               0 Dir(s)  34,618,871,808 bytes free

                C:\>eval 2+1
                3

                C:\>eval now
                24/02/2016 20:41:11

                C:\>eval weekdayname(weekday(now))
                Wednesday

                C:\>eval 4*ATN(1)
                3.14159265358979

                C:\>for /f "delims=" %A in ('eval 35.333/2.277') do @set result=%A

                c:\>echo %result%
                15.5173473869126

                C:\>eval weekdayname(weekday(now))
                Wednesday



                wow thats a very helpful peace of code, thank you.