Announcement

Collapse
No announcement yet.

Calculating percentage

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Calculating percentage

    Hello everybody.

    Do we have a FUNCTION in PBCC 6 that calculate the percentage difference between two numbers?

    Thanks

    #2
    No intrinsic function, but PB has subtraction, division and multiplication.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


      #3
      No built in function, but a simple macro does it:
      '
      Code:
      #COMPILE EXE
      #DIM ALL
      MACRO PCDiff(x,y) = ((y-x)/x)*100
      FUNCTION PBMAIN() AS LONG
          LOCAL lDebug AS LONG: TXT.WINDOW EXE.FULL$, 10,10,45,85 TO lDebug
          LOCAL a,b AS LONG
          LOCAL c,d AS DOUBLE
          a = 100: b = 67
          TXT.PRINT "Percentage difference between";a;" and" ; b; "is  " ;PCDiff(a,b); "%"
          c = 90 :d = 123.5
          TXT.PRINT "Percentage difference between";c;" and" ; d; "is  " ;PCDiff(c,d); "%"
      
      'Finalise
          TXT.COLOR = %RGB_BLUE
          TXT.PRINT
          TXT.PRINT "  ....Press any key to exit": TXT.WAITKEY$: TXT.END
      END FUNCTION
      '
      =========================
      https://camcopng.com
      =========================

      Comment


        #4
        Originally posted by Michael Mattias View Post
        No intrinsic function, but PB has subtraction, division and multiplication.
        Most probably we got addition as well

        Comment


          #5
          Originally posted by Stuart McLachlan View Post
          No built in function, but a simple macro does it:
          '
          Code:
          #COMPILE EXE
          #DIM ALL
          MACRO PCDiff(x,y) = ((y-x)/x)*100
          FUNCTION PBMAIN() AS LONG
          LOCAL lDebug AS LONG: TXT.WINDOW EXE.FULL$, 10,10,45,85 TO lDebug
          LOCAL a,b AS LONG
          LOCAL c,d AS DOUBLE
          a = 100: b = 67
          TXT.PRINT "Percentage difference between";a;" and" ; b; "is " ;PCDiff(a,b); "%"
          c = 90 :d = 123.5
          TXT.PRINT "Percentage difference between";c;" and" ; d; "is " ;PCDiff(c,d); "%"
          
          'Finalise
          TXT.COLOR = %RGB_BLUE
          TXT.PRINT
          TXT.PRINT " ....Press any key to exit": TXT.WAITKEY$: TXT.END
          END FUNCTION
          '
          Thanks Bro!

          Comment


            #6
            Most probably we got addition as well
            Yes we do; but you don't need addition to calculate the percentage difference between two numbers.

            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


              #7
              exactly
              Dale

              Comment


                #8
                Originally posted by Michael Mattias View Post

                Yes we do; but you don't need addition to calculate the percentage difference between two numbers.
                As a matter of fact you are right

                Code:
                Percent = ((Num2/Num1) * 100) - 100
                No addition required.

                Comment


                  #9
                  Originally posted by salvatore renda View Post
                  Code:
                  Percent = ((Num2/Num1) * 100) - 100
                  ??? What is "- 100" for?

                  See MACRO in post 3 for percentage difference as requested in post 1.

                  Dale

                  Comment


                    #10
                    Originally posted by Dale Yarker View Post
                    ??? What is "- 100" for?

                    See MACRO in post 3 for percentage difference as requested in post 1.
                    Just a slightly different way of getting the same result. There are several different ways of ordering the single multiplication, division and subtraction operators.

                    (y-x)/x*100 = (y/x-1)*100 = y/x*100-100​ = 100/x*(y-x)= 100*(y-x)/x = 100*y/x-100

                    y/x*100-100 (or y*100/x-100​) may be marginally more efficient once compiled since it uses two constant values and only one operation on two variables.
                    =========================
                    https://camcopng.com
                    =========================

                    Comment


                      #11
                      Only if 100 is one of x or y
                      Dale

                      Comment


                        #12
                        Originally posted by Dale Yarker View Post
                        Only if 100 is one of x or y
                        Nope. See my final edited version of the above.

                        x = 2: y = 1
                        1/2*100 = 50 : 50 - 100 = -50%

                        x = 2: y = 4
                        4/2*100 = 200 : 200 - 100 = 100%

                        x = 2: y = 3
                        3/2*100 = 150 : 150 - 100 = 50%
                        =========================
                        https://camcopng.com
                        =========================

                        Comment


                          #13
                          Got X and Y reversed going into calculator. That gave a negative result when Y is greater than X compared to positive from shown code lines.
                          Dale

                          Comment


                            #14
                            As a matter of fact you are right
                            Please don't tell me this was a surprise!
                            Michael Mattias
                            Tal Systems (retired)
                            Port Washington WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment


                              #15
                              > the percentage difference between two numbers?

                              It depends on exactly what you mean. If X = 100 and Y = 150, then X is 66.7% of Y, but Y is 50% larger than X.
                              "Not my circus, not my monkeys."

                              Comment


                                #16
                                The 'percentage difference between two numbers' is always 100% of the difference.
                                OR
                                Using Eric's values for X and Y we get the following:
                                Y-X=50 because 150-100=50
                                50/Y*100= 33.33%
                                50/X*100= 50%
                                Ergo the 'percentage difference between two numbers' in this case is 50%-33.33%=16.67%

                                I rest my case... or cases, as the case may be.
                                Rod
                                In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

                                Comment


                                  #17
                                  Originally posted by Eric Pearson View Post
                                  > the percentage difference between two numbers?
                                  It depends on exactly what you mean. If X = 100 and Y = 150, then X is 66.7% of Y, but Y is 50% larger than X.
                                  Originally posted by Rodney Hicks View Post
                                  The 'percentage difference between two numbers' is always 100% of the difference.
                                  ...
                                  Ergo the 'percentage difference between two numbers' in this case is 50%-33.33%=16.67%
                                  ​I just realised that we've been talking about "percentage change", while the OP asked about "percentage difference" which is not the same thing. - so Rodney is partially correct and the solutions above are incorrect

                                  i.e. The difference between two values divided by the average of the two values. Shown as a percentage.​
                                  https://www.mathsisfun.com/percentage-difference.html

                                  My previous solution should have been:
                                  MACRO PCDiff(x,y) = ABS((x-y)/((x+y)/2)*100)

                                  WHere x = 100 and y = 150
                                  (100-150) = -50
                                  (100+150)/2 = 125
                                  Percentage difference is 50/125*100 = 40%
                                  =========================
                                  https://camcopng.com
                                  =========================

                                  Comment


                                    #18
                                    Well done, Stuart!
                                    Rod
                                    In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

                                    Comment


                                      #19
                                      Originally posted by Michael Mattias View Post
                                      Yes we do; but you don't need addition to calculate the percentage difference between two numbers.
                                      N.B. Percentage difference does require addition!
                                      =========================
                                      https://camcopng.com
                                      =========================

                                      Comment


                                        #20
                                        Originally posted by Eric Pearson View Post
                                        > the percentage difference between two numbers?
                                        It depends on exactly what you mean. If X = 100 and Y = 150, then X is 66.7% of Y, but Y is 50% larger than X.
                                        Percentage change, which you are calculating is generally taken as the change from an "old" or "first" value to a "second" or "new" value (in this case, the first value is x)
                                        i.e. order of the parameters is important.
                                        =========================
                                        https://camcopng.com
                                        =========================

                                        Comment

                                        Working...
                                        X
                                        😀
                                        🥰
                                        🤢
                                        😎
                                        😡
                                        👍
                                        👎