erlang:评估算术表达式时的异常错误

时间:2015-07-03 01:24:57

标签: erlang

ind = sub2ind(size(X) , ones(6,1)* i , (5:10)'*ones(1,N))  ; 
sum(X(ind))

执行上述代码很奇怪:

-module(test2).
-export([main/1]).
calc(Cnt, Total) when Cnt > 0 ->
  calc(Cnt - 1, (Total + 2 * 34 + 1) / 2 * 39);
calc(0, Total)->
  io:format("~p ~n", [Total]),
  ok.
main([A])->
  Cnt = list_to_integer(A),
  calc(Cnt, 1).

如果我删除$ escript test2.beam 900000000 escript: exception error: an error occurred when evaluating an arithmetic expression in function test2:calc/2 (test2.erl, line 4) in call from escript:run/2 (escript.erl, line 752) in call from escript:start/1 (escript.erl, line 276) in call from init:start_it/1 in call from init:start_em/1 ,那么一切正常。

问题是什么?

1 个答案:

答案 0 :(得分:2)

浮点溢出。浮点除法运算符/将您的Total变量转换为双精度浮点数。经过一定次数的迭代后,乘以39的结果超过了该类型可以容纳的最大值。