Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.58 KB | None | 0 0
  1.  
  2. (* sin approx *)
  3. let sin (x : float):float =
  4.     let nth (n: float) (x : float) : float = (* gives nth element for fiven x *)
  5.         let rec fact (x:float) : float =
  6.             if x = 0.0 then 1.0
  7.             else x *. (fact(x -. 1.0)) in
  8.                 (-1.0)**(n +. 1.0) *. x**(2.0*.n -. 1.0) /. fact (2.0*.n -. 1.0) in
  9.     let rec sin_tay (acc:float) (element:float) : float =
  10.         let new_acc : float = acc +. nth element x  in
  11.         if new_acc <> acc (*stop on precision error*)
  12.             then sin_tay new_acc (element +. 1.0)
  13.             else new_acc
  14.         in
  15.     sin_tay 0.0 1.0;; (*intitial value of acc = 0, starting from first element*)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement