Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Invert a monotonic function
--ghc 8.0.1 /opt/ghc/8.0.1/lib/ghc-8.0.0.20160127/ -- Computes the inverse of a monotonic function -- fn is the function we would like to invert -- x is the value whose inverse we are seeking -- lbfn gives a lower bound for the inverse -- ubfn gives an upper bound for the inverse inversefn fn x lbfn ubfn = let inv approx lb ub count = if count >= 100 then approx else let fnapprox = fn approx in if fnapprox == x then approx else if fnapprox > x then inv ((approx+lb)/2) lb approx (count+1) else inv ((approx+ub)/2) approx ub (count+1) in inv (lbfn x) (lbfn x) (ubfn x) 0 sqroot x = inversefn (\x -> x*x) x (\x -> 0) (\x -> x) cuberoot x = inversefn (\x -> x*x*x) x (\x -> min 0 x) (\x -> max 0 x) main = do print(sqroot 289) print(cuberoot (-8))
run
|
edit
|
history
|
help
0
preparednessQuotient
Kleintjes
Sierpinski Triangle as lazy list
project euler 6, haskell
=)
New wall
Producter
Removed input
Lambda Parser with long ids
Calculate Integral of function