MathGame_V2/math.ml

34 lines
799 B
OCaml

let rec pw x n = match n with
| 0 -> 1
| 1 -> x
| k when k mod 2 = 0 -> pw (x*x) (n/2)
| k -> x * (pw (x*x) (n/2)) ;;
let rec pwf x n = match n with
| 0 -> 1.
| 1 -> x
| k when k mod 2 = 0 -> pwf (x *. x) (n/2)
| k -> x *. (pwf (x *. x) (n/2)) ;;
let rec ln_b b n = match n with
| k when k < 0 -> failwith "Are you sure about that ?"
| k when k < b -> 0
| k -> 1 + ln_b b (k/b) ;;
let convexf x y theta =
(1.0 -. theta) *. x +. theta *. y ;;
let absf = function
| x when x < 0.0 -> -. x
| x -> x ;;
let rec expand_fl = function
| k when float_of_int (int_of_float k) = k -> int_of_float k
| k -> expand_fl (10.0 *. k) ;;
let incree = function
| k when k < 10 -> 0
| _ -> 1 ;;
let round x n =
float_of_int (int_of_float (x *. pwf 10. n)) /. (pwf 10. n);;