can someone enlighten me on absolute values?
Printable View
can someone enlighten me on absolute values?
An Absolute Value is the Magnitude (Size) of a value, so the Absolute value of 1000 is 1000 and the Absolute value of -1000 is also 1000 .
It can easily be expressed in VB code. I think you already know that the keyword "Abs" in VB does this right?
This ends the maths lesson ;)Code:' note that the value of "a" can be any real number
' you would decalre it like this:
Dim a as Double
' and although the value of abs_a can only ever be positive,
' you still declare it as double because that's the closest
' than VB has..
Dim abs_a as Double
' now for any value of a,
' where a>=0, the value of abs_a is equal to a
' and where the value of a is less than 0, then the value
' of abs_a is equal to 0 - a
' the IIf statement does this nicely
abs_a = IIf(a >= 0, a, -a)
' otherwise the more elaborate cousing..
If a >= 0 then
abs_a = a
Else
abs_a = -a
End If
' to code it with the least number of characters without using the Abs function (but relying on something that VB does with boolean expressions)
abs_a = (a < 0) * a
Cheers
P.S. I should have mentioned that another way of thinking of Absolute value is "the distance from 0" since distance is always "positive".
[Edited by PaulLewis on 09-20-2000 at 10:45 PM]
IAW, It it the POSITIVE VALUE of any number.
The absolute value of a imaginary value is sqr(real^2+imaginary^2) and the absolute value of a vector is he length of it, sqr(Vx^2+Vy^2)
Ooops!
returns the absolute value when a is negative, but it returns 0 when a is non-negative (a >= 0)Code:abs_a = (a < 0) * a
Well that's pretty darn stupid of me. I should have known better than to try to be smart on the last line of a post! hahaQuote:
returns the absolute value when a is negative, but it returns 0 when a is non-negative (a >= 0)
How many times have we all "added just one more line of code" before finishing for the day, only to come in the next morning and wonder, "What sort of idiot would have added that line of code? It won't compile now!"
As a rule, I try not to code after lunch for that very reason. I find I'm even more slow-witted than normal after lunch..
Thanks for spotting the error ccoder. I would like to say it was deliberate to see who was watching but ummm...wellll...
Cheers