Question

Is it possible to check if a variable (the variable can be a float, double, or int) is approaching a certain number. I have done some google search but it comes up nothing.

For example as n^x as x gets more negative it approaches zero.

Was it helpful?

Solution

You could use the Math.Abs function to measure whether a given value is approaching to x:

double x = ...
double someVariable = ...

// define the precision you are working with
double epsilon = 1e-6;

// now test whether someVariable is approaching x
if (Math.Abs(someVariable - x) < epsilon)
{
    // someVariable is approaching x given the precision you have defined
}

OTHER TIPS

The closest thing you could do is compare the magnitude of the difference between your variable and your target, and see if it is less than your acceptable threshold. Strictly this isn't approaching but close (I'd deem "approaching" to mean that many samples are trending towards your target; and that's virtually impossible to do simply, especially with harmonic decays).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top