functions3.goΒΆ
// functions3.go
package main
import (
"fmt"
"math"
)
func derivative(f func(float64) float64, a, h float64) float64 {
return (f(a+h) - f(a)) / h
}
func main() {
fmt.Println(derivative(math.Sqrt, 2, 0.001))
fmt.Println(derivative(math.Sin, 2, 0.001))
f := func(x float64) float64 {
return x*x*x - x
}
fmt.Println(derivative(f, 2, 0.001))
fmt.Println(derivative(func(x float64) float64 {
return 1 / x
}, 2, 0.001))
}