-3

I have a few problem with my derivative because it shows me an error when I approach Denominator to zero .

func derivativeOf(fn: (Double) -> Double, atX x: Double) -> Double {
    let h -> 0
    return (fn(x + h) - fn(x))/h 
}

i know my syntax sucks but currently it is common in calculus and mathematical Differential.

  • Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour), read what's [on-topic](https://stackoverflow.com/help/on-topic) here, [How to Ask](https://stackoverflow.com/questions/how-to-ask), and the [question checklist](https://meta.stackoverflow.com/q/260648/843953), and provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – RJ Adriaansen Feb 15 '21 at 21:12

1 Answers1

1

You can't express "number approaching zero" as let h -> 0 - this is just an invalid syntax in Swift.

There's also no specific operator for "number approaching zero". But depending on what you need, you could for example express "smallest possible positive number", using Double.leastNonzeroMagnitude:

let h = Double.leastNonzeroMagnitude
timbre timbre
  • 12,648
  • 10
  • 46
  • 77