Determine the point where function output will go from positive to negative
Determine the point where function output will go from positive to negative
I have a function that is like:
f(x) = c - x^2 (c = some constant positive integer, x = +ve integer >= 0)
The output of this function, goes from positive to negative as x -> +infinity.
Is there a way to directly figure out the x
which produces last of the positive outputs before entering in the negative domain?
x
Also, if I use an iterative method to locate such a point, how should I find the right increment to go with so that I can reach that value of x
as quick as possible? Right now, I am initializing x = floor(sqrt(c) * 0.9)
and using dumb +1
increments to x
to reach the point where f(x)
enters the negative range.
x
x = floor(sqrt(c) * 0.9)
+1
x
f(x)
An observation: f(x)
seems to behave in a weird way for c > 10e40
(i.e. when we initiate x = floor(sqrt(c) * 0.9)
it gives out a value way too far from the desired point) and you can imagine how long the +1 increment takes to get to the desired output with such large values of c
.. ;-(.
f(x)
c > 10e40
x = floor(sqrt(c) * 0.9)
c
Please help. thanks.
I am using standard C/Python interface to do the calculations of sqrt..can this be the reason?
– khan
Aug 27 at 6:17
2 Answers
2
The mathematical answer is that given by Jasper Loy. The computational solution may be better done the following way in order to avoid the detour via floating point numbers and surprises caused by conversion.
I assume that you have integer operations $+,-,cdot,/$ available (where $/$ is integer division as usual in programming) that can deal with integers in the range $0,ldots,c$. The following algorithm corresonds to Heron's method of computing square roots (assuming $c>2$ perhaps)
Very interesting indeed. A question though: Is this method more expensive than computational square root?
– khan
Aug 27 at 6:42
@khan For questions of performance, a computing group may be better but first consider whether you prefer: the correct answer slowly or the wrong answer quickly.
– badjohn
Aug 27 at 8:26
That is right. I agree.
– khan
Aug 27 at 13:48
@Hagen von Eitzen what do you suggest about figuring out the right increment to
x
, to reach the desired point, if we choose to go with an iterative approach?– khan
Aug 27 at 16:40
x
If we solve the quadratic equation $c-x^2=0$ for a real number $x$, we get $x=sqrt c$ or $x=-sqrt c$. Since we are looking at the branch $xge 0$, the required solution is the largest integer not exceeding $sqrt c$. For example, if $c=2$ then $sqrt c=1.414ldots$ and the solution would be $1$.
That is sqrt(c) - 1?
– khan
Aug 27 at 6:26
@khan - No it is not "sqrt(c) - 1". It is "floor(sqrt(c))" where floor() returns the next lower integer value of the argument. In some computer languages you can use "int(sqrt(c))" to achieve the same result but some languages will round up of the fractional part of the argument is greater than or equal to 0.5.
– Michael Karas
Aug 27 at 11:31
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
$f$ does not behave weirdly for $c>10^40$. Are you using floating point math on a computer perhaps?
– Hagen von Eitzen
Aug 27 at 6:15