OCaml Forge

Browse | Submit A New Snippet | Create A Package

Sine computations

Type:
Function
Category:
Math Functions
License:
BSD License
Language:
Other Language
 
Description:
Compute the sine function using a Taylor series and a cubic iteration.

Versions Of This Snippet:

Snippet ID Download Version Date Posted Author Delete
912010-01-13 01:31Matías Giovannini

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: 1

let epsilon = 1e-9 let sin_taylor x = let rec accumulate s y k = if abs_float y < epsilon then s else accumulate (s +. y) (-. y *. x *. x /. (k +. 1.) /. (k +. 2.)) (k +. 2.) in accumulate 0. x 1. let sin_cube x = let rec reduce k y = if abs_float y < epsilon then restore k y else reduce (k + 1) (y /. 3.) and restore k y = if k = 0 then y else restore (k - 1) (y *. (3. -. 4. *. y *. y)) in reduce 0 x let half_pi = 1.570796326794896619231321691638 let reduce_quadrant a = let q = floor (a /. half_pi) in (truncate q mod 4 + 4) mod 4, a -. half_pi *. q let sine loop a = let (i, x) = reduce_quadrant a in let y = loop (if i mod 2 = 0 then x else half_pi -. x) in if i < 2 then y else -. y

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..