Commits

arkdro committed 42f24b8

use decimal tuple for divide

Comments (0)

Files changed (1)

   | Eterm.ET_int x -> Num.num_of_int (Int32.to_int x)
   | Eterm.ET_bignum x -> x
 
+let op_divide x1 x2 =
+  let t1 = make_tuple x1 in
+  let t2 = make_tuple x2 in
+  match t1, t2 with
+  | Some _, Some _ ->
+    let (Some n1) = make_num t1 in
+    let (Some n2) = make_num t2 in
+    let res = Num.div_num n1 n2 in
+    Eterm.ET_tuple [|(Eterm.ET_atom "ok"); make_decimal_tuple res;|]
+  | _, _ ->
+    Eterm.ET_tuple [|(Eterm.ET_atom "error"); (Eterm.ET_atom "wrong_arguments");|]
+
 let handle_payload = function
-    | Eterm.ET_tuple [|(Eterm.ET_atom "divide"); Eterm.ET_bignum n1; Eterm.ET_bignum n2;|] ->
-      Trace.dbg "handle_payload" "divide bignums\n%s\n%s\n%!"
-        (Num.string_of_num n1) (Num.string_of_num n2);
-      let div_res = Num.div_num n1 n2 in
-      let res = make_decimal_term div_res in
-      res
+    | Eterm.ET_tuple [|(Eterm.ET_atom "divide"); x1; x2;|] ->
+      Trace.dbg "handle_payload" "divide\n%!";
+      op_divide x1 x2
     | Eterm.ET_tuple [|(Eterm.ET_atom "subtract"); Eterm.ET_bignum n1; Eterm.ET_bignum n2;|] ->
       Trace.dbg "handle_payload" "subtract bignums\n%s\n%s\n%!"
         (Num.string_of_num n1) (Num.string_of_num n2);