3
\$\begingroup\$

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!

\$\endgroup\$
5
  • \$\begingroup\$ What would you like to have displayed in the case of f(x)=1/x and a=0 \$\endgroup\$
    – Matt
    Oct 15, 2012 at 12:01
  • \$\begingroup\$ @Matt: You will never get that input. The limit of f(x) as x approaches a will always exist. \$\endgroup\$
    – beary605
    Oct 15, 2012 at 23:58
  • \$\begingroup\$ A mathematician wouldn't say "the limit exists" in a case like 1/x² | ₓ→₀, but I suppose it's clear what you mean. \$\endgroup\$ Oct 17, 2012 at 22:38
  • \$\begingroup\$ @leftaroundabout: What would they call it then? Indeterminate? :) I'd like to know. \$\endgroup\$
    – beary605
    Oct 18, 2012 at 0:23
  • \$\begingroup\$ 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. \$\endgroup\$ Oct 18, 2012 at 0:37

2 Answers 2

4
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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)
\$\endgroup\$
9
  • \$\begingroup\$ 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. \$\endgroup\$
    – beary605
    Oct 15, 2012 at 4:32
  • 1
    \$\begingroup\$ This 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 \$\endgroup\$
    – Matt
    Oct 15, 2012 at 12:08
  • \$\begingroup\$ Darn, I guess I'll have to make i the smallest float possible, which should bandadge those problems. \$\endgroup\$
    – walpen
    Oct 15, 2012 at 12:21
  • \$\begingroup\$ python provides a machine epsilon as sys.float_info.epsilon, which is probably more accurate for what you're attempting \$\endgroup\$
    – ardnew
    Oct 15, 2012 at 22:18
  • \$\begingroup\$ 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). \$\endgroup\$
    – walpen
    Oct 16, 2012 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.