|
-
Jun 16th, 2010, 02:43 PM
#1
Thread Starter
Lively Member
"roughly equals"
It has occoured to me that i compare a lot of numbers that i want to be roughly the same like 103 and 105 = good or 3.2 and 3.1 = good and writing out the "If a+3 > b and a-3<b then" to give it a range is tedious, harder to read and i imagine not as good performance as possible alternatives so i was wondering if there is any kind of operator or .net function to get a true or false from 2 numbers where i can define the accuracy they must meet at?
-
Jun 16th, 2010, 02:57 PM
#2
Re: "roughly equals"
I don't know of anything built in, though you could certainly write something like that. However, if you were to write it, it would not provide you any advantage in speed. For instance, you could write a method that took three arguments. The first two would be the right and left sides of the comparison, and the third would be the tolerance. It would look like what you have written so many times already, though, so the only advantage would be that you could call it as a single line which would be somewhat easier to type.
My usual boring signature: Nothing
 
-
Jun 16th, 2010, 02:57 PM
#3
Re: "roughly equals"
not inherently... but you could write your own function... you just need to define the parameters....
in a simplistic form, I envision needing 3 pieces of info... the "a"... the "b"... and the tolerance...
Code:
Private Function CloseEnough(ByVal myAValue As Integer, ByVal myBValue As Integer, ByVal myTolerance As Integer) As Boolean
Return Math.Abs(myAValue - myBValue) <= myTolerance
End Function
If the absolute value of the difference between the two numbers is less than or equal to the tolerance value... returns true... otherwise it returns false. For the 3.2 and 3.1 version.... make a decimal version
Code:
Private Function CloseEnough(ByVal myAValue As decimal, ByVal myBValue As decimal, ByVal myTolerance As decimal) As Boolean
Return Math.Abs(myAValue - myBValue) <= myTolerance
End Function
then you can call it:
Code:
CloseEnough(103,105,3)
or
Code:
CloseEnough(3.1D, 3.2D, 0.5D)
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|