Skip to content

Instantly share code, notes, and snippets.

@vigdorchik
Created June 6, 2014 07:59
Show Gist options
  • Save vigdorchik/3c9a6d5e31cc4059ca7d to your computer and use it in GitHub Desktop.
Save vigdorchik/3c9a6d5e31cc4059ca7d to your computer and use it in GitHub Desktop.
Prelude> let eps = 0.00000001
Prelude> let fix f x_1 x_2 = let f_x = x_1 - (f x_1)*(x_1 - x_2)/((f x_1) - (f x_2)) in if abs (f_x - x_1) < eps then f_x else fix f f_x x_1
Prelude> let root a = fix ((flip (-)) a . flip (^) 3) (a-1) (a+1)
Prelude> root 27
3.0
@vigdorchik
Copy link
Author

Prelude> let eps = 0.00000001
Prelude> let n = 10000
Prelude> let fix f x y m = let z = x - (f x)*(x-y)/((f x) - (f y)) in if abs (z - x) < eps || m>n then z else fix f z x (m+1)
Prelude> let root a = fix ((flip (-)) a . flip (^) 3) (a-1) (a+1) 0
Prelude> root 27
3.0
Prelude> root 6000000000000000000000000
NaN

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