erlang - ebin 中的雅司病和 erlang 梁文件

标签 erlang floating-point integer yaws

当我的表单帖子中有整数和 float 并在我有梁文件的 ebin 文件中收到这些时,我遇到了问题。希望有人能帮助我。

npower.yaws

   <erl>
kv(K,L) ->
{value, {K, V}} = lists:keysearch(K,1,L), V.        
out(A) ->
L = yaws_api:parse_post(A),
N = kv("number", L),
    npower62:math3(N).
    </erl>

npower62.erl编译为beam文件
-模块(npower62)。
-导出([math3/1])。

math3( [N] ) ->
数量 = N,
Nsquare = 数字 * 数字,
{html, io_lib:format("~c 的平方 = ~w", [N, Nsquare])}.

给我 3 的平方 = 2601
而不是
3 的平方 = 9
我尝试使用 Number = list_to_integer(atom_to_list(N)) (不起作用)
我尝试使用 Number = list_to_float(atom_to_list(N)) (不起作用)
我尝试使用 Number = list_to_integer(N) (不起作用)

最佳答案

首先,您可以缩小 math3/1 函数接受的范围:

-module(npower62). 
-export([math3/1]). 

math3(N) when is_number(N) -> 
  N2 = N * N,
  io_lib:format("square of ~p = ~p", [N, N2]).

请注意,我们已经对该函数进行了相当多的重写。它不再接受列表,而是任何数字,仅N。另外,您传递给 io_lib:format/2 的格式字符串完全关闭,请参阅 man -erl io 。

我们现在可以攻击雅司病代码:

<erl>
  kv(K,L) ->
      proplists:get_value(K, L, none).

  out(A) ->
    L = yaws_api:parse_post(A),
    N = kv("number", L),
    none =/= N, %% Assert we actually got a value from the kv lookup

    %% Insert type mangling of N here so N is converted into an Integer I
    %% perhaps I = list_to_integer(N) will do. I don't know what type parse_post/1
    %% returns.

    {html, npower62:math3(I)}
</erl>

请注意,您的 kv/2 函数可以使用 proplists 查找函数编写。在您的代码变体中,从 kv/2 返回的值是 {value, {K, V}},该值永远都不正确在您的 math3/1 版本中。 proplists:get_value/3 仅返回 V 部分。另请注意,我将 {html, ...} 提升到了这个级别。让 npower62 处理它是不好的风格,因为它不应该知道它是从 yaws 内部调用的事实。

我的猜测是您需要调用 list_to_integer(N)。解决这个问题的最简单方法是调用 error_logger:info_report([{v, N}]) 并在 shell 或日志文件中查找 INFO REPORT 并查看是什么术语N 是。

TL;DR:问题在于您的值(value)观并非处处匹配。因此,您将面临大量的函数崩溃,yaws 可能会捕获、记录并幸存下来。这会让你困惑不已。

此外,从 erl shell 测试您的函数 npower62:math3/1 函数。这样,您就会从一开始就发现它是错误的,从而减少您的困惑。

关于erlang - ebin 中的雅司病和 erlang 梁文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4370773/

相关文章:

string - 将整数字符串日期转换为实际日期

erlang - 嵌入了 appmod 的 YAW 对我不起作用

python - 浮点运算 : Possible unsafe reliance on specific comparison?

R:为什么 "identical(c(1:3), c(1, 2, 3))"是假的?

python - 恢复标准化操作时的精度问题

javascript - 在 JS 中读取/写入 float 字节

javascript - JS Number.MAX_SAFE_INTEGER 和 MAX_VALUE 有什么区别?

erlang - 管理和构建Erlang应用

erlang - 运行多个 Erlang 应用程序。一台还是多台虚拟机?

erlang - 打包 Elixir CLI 应用程序的最佳方式是什么?