Skip to content
Snippets Groups Projects
Commit 398afda2 authored by Maxime Jacquemin's avatar Maxime Jacquemin
Browse files

Marking the power function as returning an option and avoiding exceptions

parent 9b3bb6e2
No related branches found
No related tags found
No related merge requests found
......@@ -38,7 +38,7 @@ let two_power n =
else
two_power_of_int k
let power_int_positive_int n e =
let power_int_positive_int_opt n e =
try Some (Big_int_Z.power_int_positive_int n e)
with Invalid_argument _ -> None
......
......@@ -203,7 +203,7 @@ val two_power : t -> t
val two_power_of_int : int -> t
(** Computes [2^n] *)
val power_int_positive_int: int -> int -> t option
val power_int_positive_int_opt : int -> int -> t option
(** Exponentiation *)
val extract_bits : start:t -> stop:t -> t -> t
......
......@@ -143,10 +143,10 @@ let parse_positive_float_with_shortcut ~man_size ~min_exp ~max_exp s =
let frac = Str.matched_group 2 s in
let len_frac = String.length frac in
let num = Integer.of_string (n ^ frac) in
let* den = Integer.power_int_positive_int 5 len_frac in
let* den = Integer.power_int_positive_int_opt 5 len_frac in
if Integer.is_zero num then raise (Shortcut zero) ;
let exp10 = match_exp 3 in
let+ pow5 = Integer.power_int_positive_int 5 (abs exp10) in
let+ pow5 = Integer.power_int_positive_int_opt 5 (abs exp10) in
let num = if exp10 >= 0 then Integer.mul num pow5 else num in
let den = if exp10 >= 0 then den else Integer.mul den pow5 in
let exp = exp10 - len_frac in
......@@ -156,14 +156,14 @@ let parse_positive_float_with_shortcut ~man_size ~min_exp ~max_exp s =
let frac = Str.matched_group 2 s in
let len_frac = String.length frac in
let num = Integer.of_string (n ^ frac) in
let+ den = Integer.power_int_positive_int 5 len_frac in
let+ den = Integer.power_int_positive_int_opt 5 len_frac in
return ~num ~den ~exp:(~- len_frac)
else if Str.string_match num_exp s 0 then
let n = Str.matched_group 1 s in
let num = Integer.of_string n in
if Integer.is_zero num then raise (Shortcut zero) ;
let exp10 = match_exp 2 in
let+ pow5 = Integer.power_int_positive_int 5 (abs exp10) in
let+ pow5 = Integer.power_int_positive_int_opt 5 (abs exp10) in
let num = if exp10 >= 0 then Integer.mul num pow5 else num in
let den = if exp10 >= 0 then Integer.one else pow5 in
return ~num ~den ~exp:exp10
......
......@@ -767,9 +767,8 @@ module Make
subvalues that are all evaluated. Limits the number of splits to
keep the number of evaluations linear on [nb]. *)
let subdivnb =
let pow n e = Integer.power_int_positive_int n e |> Option.get in
if nb > 3
then (subdivnb * nb) / (Integer.to_int_exn (pow 2 (nb - 1)))
if 3 < nb && nb < 63
then (subdivnb * nb) / (1 lsl (nb - 1))
else subdivnb
in
Self.result ~current:true ~once:true ~dkey
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment