let range step first last =
assert (step > 0.0);
let n =
(((last -. first) /. step) |> Float.abs |> Float.round_up |> Int.of_float) + 1 in
let a = Array.create n 0.0 in
let (op,comp) = if first <= last then ((+.),(<=)) else ((-.),(>=)) in
Array.iteri (fun i _ -> a.(i) <- op first (Float.of_int i *. step)) a;
if comp a.(n-1) last
then a
else Array.sub a 0 (n-1)