Commits

arkdro committed bcaab14

add bias numbers, that is necessary for ocaml Int32.to_int
on 32-bit platform

Comments (0)

Files changed (1)

erlang/src/c_server.erl

     Cmd = term_to_binary({get, none}),
     port_command(Port, Cmd),
     {noreply, State#state{from=From}};
-handle_call({multiply_list, Term}, From,
+handle_call({multiply_list, List}, From,
             #state{status=running, port=Port} = State) ->
-    Cmd = term_to_binary({mult_list, Term}),
+    Blist = bias_list(List),
+    Cmd = term_to_binary({mult_list, Blist}),
     port_command(Port, Cmd),
     {noreply, State#state{from=From}};
 handle_call({multiply, N1, N2}, From,
             #state{status=running, port=Port} = State) ->
-    Cmd = term_to_binary({multiply, N1, N2}),
+    B1 = bias_number(N1),
+    B2 = bias_number(N2),
+    Cmd = term_to_binary({multiply, B1, B2}),
     port_command(Port, Cmd),
     {noreply, State#state{from=From}};
-handle_call({sum, Term}, From, #state{status=running, port=Port} = State) ->
-    Cmd = term_to_binary({sum_list, Term}),
+handle_call({sum, List}, From, #state{status=running, port=Port} = State) ->
+    Blist = bias_list(List),
+    Cmd = term_to_binary({sum_list, Blist}),
     port_command(Port, Cmd),
     {noreply, State#state{from=From}};
 handle_call({add, N1, N2}, From,
             #state{status=running, port=Port} = State) ->
-    Cmd = term_to_binary({add, N1, N2}),
+    B1 = bias_number(N1),
+    B2 = bias_number(N2),
+    Cmd = term_to_binary({add, B1, B2}),
     port_command(Port, Cmd),
     {noreply, State#state{from=From}};
 handle_call({subtract, N1, N2}, From,
             #state{status=running, port=Port} = State) ->
-    Cmd = term_to_binary({subtract, N1, N2}),
+    B1 = bias_number(N1),
+    B2 = bias_number(N2),
+    Cmd = term_to_binary({subtract, B1, B2}),
     port_command(Port, Cmd),
     {noreply, State#state{from=From}};
 handle_call({divide, N1, N2}, From,
             #state{status=running, port=Port} = State) ->
-    Cmd = term_to_binary({divide, N1, N2}),
+    B1 = bias_number(N1),
+    B2 = bias_number(N2),
+    Cmd = term_to_binary({divide, B1, B2}),
     port_command(Port, Cmd),
     {noreply, State#state{from=From}};
 handle_call(stop, _From, St) ->
     {ok, State}.
 
 %-------------------------------------------------------------------
+bias_number({Sign, Mant, Exp}) when Mant >= (2 bsl 30) and Mant < (2 bsl 31) ->
+    %% this is necessary as ocaml Int32.to_int on 32-bit platform
+    %% behaves as Int31, e.g. truncates highest bit
+    {Sign, Mant * 10000000000, Exp - 10};
+bias_number(N) ->
+    N.
+
+%-------------------------------------------------------------------
+bias_list(L) ->
+    [bias_number(X) || X <- L].
+
+%-------------------------------------------------------------------