Skip to content

Instantly share code, notes, and snippets.

@cls
Created May 12, 2018 16:02
Show Gist options
  • Save cls/d233b7bf6e538a7bd47fa600676e6972 to your computer and use it in GitHub Desktop.
Save cls/d233b7bf6e538a7bd47fa600676e6972 to your computer and use it in GitHub Desktop.
Derivative of a function in Standard ML, using Real.nextAfter
fun derivative (f : real -> real) (x : real) : real =
let val pos = Real.nextAfter (x, Real.posInf)
val neg = Real.nextAfter (x, Real.negInf)
in
(f pos - f neg) / (pos - neg)
end
@cls
Copy link
Author

cls commented May 12, 2018

I'm unsure if Standard ML guarantees that pos - neg here be non-zero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment