Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically check if a variable is approaching x?

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.

like image 415
Daniel Lopez Avatar asked Dec 30 '11 15:12

Daniel Lopez


1 Answers

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
}
like image 84
Darin Dimitrov Avatar answered Oct 04 '22 05:10

Darin Dimitrov