// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
object FunctionFun {
    def findZero(f: (Double) => Double) = {
        def f1(x: Double) = (f(x + 0.0000005) - f(x - 0.0000005))/0.000001
        def zero(v: Double) = -0.00000001 < v && v < 0.00000001
        def it(x: Double, f: (Double) => Double): Double = if(zero(f(x))) x else it(x - f(x)/f1(x), f)
        it(1, f)
    }
    def parabel(x: Double) = x*x-4
    def main(args: Array[String]) {
        println(findZero(parabel))
        println(findZero((x: Double) => x*x-4))
    }
}
