6

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.

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Daniel Lopez
  • 1,728
  • 3
  • 24
  • 40
  • 6
    What do you mean by 'approaching'? – M.Babcock Dec 30 '11 at 15:17
  • 1
    Are you asking if you can detect an asymptotic limit – rerun Dec 30 '11 at 15:17
  • Your input is a little thin. What do you mean by "approaching" and in what context? – juergen d Dec 30 '11 at 15:18
  • 1
    Of course there is. But I'm afraid that you're going to need to be a little more specific. Can you show us what you've tried so far? – Aaron Dec 30 '11 at 15:18
  • are you trying to take a limit of a function in C#? – msarchet Dec 30 '11 at 15:18
  • If you want to solve limits programmatically then you should probably write a program where you apply the mathematic rules for that. – dimme Dec 30 '11 at 15:19
  • I assume you have a sequence of numbers or a variable that is being iteratively updated? you can probably program in some analysis of the sequence but it would likely be fairly crude since for any n points you can create an Nth order polynomial (not necessarily easily) that fits them and being a polynomial will tend towards infinity... – Chris Dec 30 '11 at 15:20
  • @BryceAtNetwork23: I'm not as confident as your "Of course there is." since I think there are times when you don't have enough information to do so. You are right though about needing to be more specific for sure though. :) – Chris Dec 30 '11 at 15:22
  • 1
    @tr3: in that particular case you aren't using the certain number. Do you mean `abs(newvalue-target) - abs(oldvalue-target) > 0` This would be closer but still wouldn't necessarily tell you if it was converging to that target. eg imagine asking if 1/x approaches -1 as x gets larger. 1/x is always getting closer to -1 but it actually approaches 0. Doing it with just a sequence is not possible without making some assumptions. – Chris Dec 30 '11 at 15:25
  • Can I ask what the aim of this is as well? are you wanting to be able to have equations inputted and analysed or are you looking at analysing data samples? In most cases I'd probably say you don't want to do this. Pre-calculate if possible or if not then look for another solution to your problem that is easier. :) – Chris Dec 30 '11 at 15:32

2 Answers2

2

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
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    -1: Not checking if a function is approaching a value x. You check only if it is close. Approaching a value involves a lot more. – dimme Dec 30 '11 at 15:22
  • I'm not sure I follow this answer. For something to be approaching x it has to be moving but you seem to be just defining a single value and seeing if it is close to the target. Or is the last bit meant to be included in a loop of some kind and if so how do you determine if something is just close to x for a short time? – Chris Dec 30 '11 at 15:28
  • @Dimme, Chris, you are correct. This is only the check whether a variable is already close to a given value. As far as approaching is concerned we are all waiting for the OP to explain what he is doing, what algorithm did he choose to solve the issue, what difficulties did he encounter, ... – Darin Dimitrov Dec 30 '11 at 15:40
1

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).

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166