Finding limits of functions

3

A limit in math determines the value that a function f(x) approaches as x gets closer and closer to a certain value.

Let me use the equation f(x)=x^2/x as an example.
Obviously, f(x) is undefined if x is 0 (x=0, x2=0, 0/0 is undefined).
But, what happens when we calculate f(x) as x approaches 0?

x=0.1, f(x)=0.01/0.1 = 0.1
x=0.01, f(x)=0.0001/0.01 = 0.01
x=0.00001, f(x)=0.0000000001/0.00001 = 0.00001
We can easily see that f(x) is approaching 0 as x approaches 0.

What about when f(x)=1/x^2, and we approach 0?

x=0.1, f(x)=1/0.01=100
x=0.01, f(x)=1/0.0001=10000
x=0.00001, f(x)=1/0.0000000001=10000000000
As x approaches 0, f(x) approaches positive infinity.

You will be given two things in whatever input format you like:

  • f(x) as an eval-able string in your language
  • a as a floating point

Output the value that f(x) approaches when x approaches a. Do not use any built-in functions that explicitly do this.

Input Limitations:

  • The limit of f(x) as x approaches a will always exist.
  • There will be real-number values just before and just after f(a): you will never get f(x)=sqrt(x), a=-1 or anything of the sort as input.

Output Specifications:

  • If f(x) approaches positive or negative infinity, output +INF or -INF, respectively.
  • If f(a) is a real number, then output f(a).

Test Cases:

f(x)=sin(x)/x, a=0; Output: 1
f(x)=1/x^2, a=0; Output: +INF
f(x)=(x^2-x)/(x-1), a=1; Output: 1
f(x)=2^x, a=3; Output: 8

Shortest code wins. Good luck!

beary605

Posted 2012-10-14T22:02:02.927

Reputation: 3 904

What would you like to have displayed in the case of f(x)=1/x and a=0 – Matt – 2012-10-15T12:01:37.567

@Matt: You will never get that input. The limit of f(x) as x approaches a will always exist. – beary605 – 2012-10-15T23:58:44.347

A mathematician wouldn't say "the limit exists" in a case like 1/x² | ₓ→₀, but I suppose it's clear what you mean. – ceased to turn counterclockwis – 2012-10-17T22:38:27.430

@leftaroundabout: What would they call it then? Indeterminate? :) I'd like to know. – beary605 – 2012-10-18T00:23:17.557

At least in standard analysis they'd just say "the function diverges for x → 0". The destinction between positive and negative infinity is quite cumbersome (and hardly worth it) when doing proper mathematical proofs. – ceased to turn counterclockwis – 2012-10-18T00:37:15.737

Answers

0

Python 146 185

Figured I'd post the obvious answer before anyone else does. Note, reads two lines from stdin with f(x) on the first and a on the second.

Edit: Made it actually work, as per beary's comment Edit 2: i is much smaller, so there are much fewer inputs that it should fail for.

from math import*
f,a,i=eval("lambda x:"+input()),eval(input()),2e-308
while 1:
 try:
     r=str(f(a+i)).upper()
     if'NAN'!=r:break
 except:
     i*=-2
print('I'==r[0]and'+'+r or r)

walpen

Posted 2012-10-14T22:02:02.927

Reputation: 3 237

Can you put in the input format? Also, you seem to have neglected the sign on the INF (it should be +inf or -inf). I really don't mind "obvious" answers, as long as it works without any functions calculating it. – beary605 – 2012-10-15T04:32:28.690

1This is probably being overly picky, but this does not work for f(x) = 1/x, a = -1*10^-161 and f(x) = 1/(x-1*10^-161), a = 0 – Matt – 2012-10-15T12:08:41.477

Darn, I guess I'll have to make i the smallest float possible, which should bandadge those problems. – walpen – 2012-10-15T12:21:53.440

python provides a machine epsilon as sys.float_info.epsilon, which is probably more accurate for what you're attempting – ardnew – 2012-10-15T22:18:45.493

Thanks for that ardnew, though I think you meant sys.float_info.min? i in my code is nearly that on my machine (saving a few characters, sacrificing portability). – walpen – 2012-10-16T14:00:16.110

@walpen: No, the min value will provide the smallest possible representable number in the FPS, but your calculated values a+min may not necessarily be representable. Using epsilon will guarantee a value a+e that is not equal to a – ardnew – 2012-10-16T15:09:14.817

Unfortunately, epsilon will give the wrong answer for the cases Matt gave (at least with the very basic algorithm I used), which is why starting at about min and working up to larger values seems to work in more cases. (Though the opposite, starting at epsilon and working down to the smallest representable in that case, is probably a more optimal algorithm). – walpen – 2012-10-16T20:47:32.220

@walpen i see. your code seems to be shorter then! (i cant get it to run any tests (saved source in file.py, run python file.py?)) – ardnew – 2012-10-16T22:40:24.880

Ex input: echo "1/x" > input; echo 0 >> input; cat input | python3 file.py – walpen – 2012-10-17T00:17:09.727

4

C, 402 bytes

Plenty of room for golfing, just proof of concept to begin with :P

Compiles under clang, osx.

Note: For linux you may have to change the code to use something like: "ld -shared f.o -o f.so -lm"

#include<dlfcn.h>
#include<stdio.h>
float e,r;
main(int i,char**v){
    // create and compile temporary c file for 'f(x)'
    // note: include offset within function
    FILE*s=fopen("f.c","w");
    fprintf(s,"#include<math.h>\nfloat f(float x){x+=%s;return %s;}",v[2],v[1]); 
    fclose(s);
    system("cc -fPIC -c f.c;ld -bundle f.o -o f.so -lm");
    float(*f)(float)=dlsym(dlopen("f.so",0),"f");

    for(i=0;1;e=(e==0)?1e-37:e*2){
        r=f(e);
        if(isinf(r))i=1;else if(!isnan(r)){
            i?printf("%cINF\n",r>0?43:45):printf("%f\n",r);
            break;
        }
    }
}

Run as:

./a.out "sin(x)/x" 0
./a.out "1/(x*x)" 0
./a.out "(x*x-x)/(x-1)" 1
./a.out "pow(2,x)" 3

Results:

1.000000
+INF
1.000000
8.000000

baby-rabbit

Posted 2012-10-14T22:02:02.927

Reputation: 1 623