You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
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
'
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
'
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*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.
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.
> 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.
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
> 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.
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment