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 aneval
-able string in your languagea
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)
asx
approachesa
will always exist. - There will be real-number values just before and just after
f(a)
: you will never getf(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 outputf(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!
What would you like to have displayed in the case of
f(x)=1/x
anda=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.347A 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