[@@@import Basic, "basic.iml"]


let square (x : real) : real =
  (* Calculate square of x. *)
  Basic.multiply x x

let cube (x : real) : real =
  (* Calculate cube of x. *)
  Basic.multiply (Basic.multiply x x) x

let distance_1d (x1 : real) (x2 : real) : real =
  (* Calculate distance between two points on a line. *)
  Basic.absolute (Basic.subtract x2 x1)

let average (a : real) (b : real) : real =
  (* Calculate average of two numbers. *)
  Basic.multiply (Basic.add a b) 0.5

let sum_of_squares (a : real) (b : real) : real =
  (* Calculate sum of squares of two numbers. *)
  Basic.add (square a) (square b)

let is_negative (x : real) : bool =
  (* Check if number is negative. *)
  not (Basic.is_positive x) && x <> 0.0

let sign (x : real) : real =
  (* Return sign of number: 1.0 for positive, -1.0 for negative, 0.0 for zero. *)
  if Basic.is_positive x then
    1.0
  else if x = 0.0 then
    0.0
  else
    ~-.1.0
