
(* Approximated cosine

   Claude Marché (Inria) *)

module M

  use real.RealInfix
  use real.Abs
  use real.Trigonometry
  use floating_point.Rounding
  use floating_point.Single

val single_of_real_exact (x: real) : single
  ensures { value result = x }

val add (x y: single) : single
  requires { no_overflow NearestTiesToEven ((value x) +. (value y)) }
  ensures  { value result = round NearestTiesToEven ((value x) +. (value y))}

val sub (x y: single) : single
  requires { no_overflow NearestTiesToEven ((value x) -. (value y)) }
  ensures  { value result = round NearestTiesToEven ((value x) -. (value y))}

val mul (x y: single) : single
  requires { no_overflow NearestTiesToEven ((value x) *. (value y)) }
  ensures  { value result = round NearestTiesToEven ((value x) *. (value y))}

let my_cosine (x:single) : single
  requires { abs (value x) <=. 0x1p-5 }
  ensures { abs (value result -. cos (value x)) <=. 0x1p-23 }
= assert {
    abs (1.0 -. (value x) *. (value x) *. 0.5 -. cos (value x)) <=. 0x1p-24
  };
  sub (single_of_real_exact 1.0) (mul (mul x x) (single_of_real_exact 0.5))

end
