Compare 2 integer by accepting a gap

Hi everyone,

Small request… I’m trying to compare 2 integer variable but i want to accept a gap between the 2 integer like this example :

  • First integer : 100
  • Second integer : 105

I accept a gap of 5 so i accept that the second integer to be set betwwen 95 and 105. i want a bolean variable as output.

Thx all :slight_smile:

If FirstInt and SecondInt are your 2 integer variables, and Thresh is an integer variable indicating how much of a gap you want to allow (in your case 5), you can check for this gap in If condition FirstInt <= SecondInt AndAlso FirstInt + Thresh >= SecondInt. This checks that the first integer is equal to the second one, or no less than whatever Thresh is set to.

If you want the check to pass if the difference between FirstInt and SecondInt is a number like 5 regardless of which number is larger, (e.g. FirstInt can be 104 and SecondInt can be 99), then you need the If condition to be (FirstInt <= SecondInt AndAlso FirstInt + Thresh >= SecondInt) OrElse (FirstInt >= SecondInt AndAlso FirstInt <= SecondInt + Thresh).

Using a variable like Thresh allows you to easily configure the difference you want to accept between the values.

1 Like

@BCdev
in such case the Math.Abs(i1 - i2) can also help as it takes the Absolute difference regardless if it is positive or negative. give a try
Math.Abs(i1 - i2) <= 5

1 Like

Thanks @ppr. To embellish on this, the 2nd statement could be written as Math.Abs(FirstInt - SecondInt) <= Thresh, which is much shorter and simpler.

1 Like

Hello @ppr and @Anthony_Humphries,

Sorry i’m late ! Thank you for your reply ! :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.