Results 1 to 3 of 3

Thread: "roughly equals"

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    75

    "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?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width