From 81ae2be76046dc2c1847845cb79e38ab48b6db8a Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Fri, 18 Mar 2022 15:42:01 +0800 Subject: [PATCH] fix(rules): enlarge precisions of floats to maximum 17 decimal places. When printing floats to strings, we have to define a small decimal limits to avoid print a too long and "inaccurate" float number: ``` 2> float_to_binary(0.3). <<"2.99999999999999988898e-01">> ``` This fix sets precision of floats to 17 digits after the decimal point. This precision is larger than precision of most `double` data type used by databases(14 digits for mysql and 15 digits for pgsql). --- apps/emqx_rule_engine/src/emqx_rule_utils.erl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/emqx_rule_engine/src/emqx_rule_utils.erl b/apps/emqx_rule_engine/src/emqx_rule_utils.erl index 3791b1386..67436ea5e 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_utils.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_utils.erl @@ -60,8 +60,8 @@ ]}). -define(EX_PLACE_HOLDER, "(\\$\\{[a-zA-Z0-9\\._]+\\})"). - -define(EX_WITHE_CHARS, "\\s"). %% Space and CRLF +-define(FLOAT_PRECISION, 17). -type(uri_string() :: iodata()). @@ -336,12 +336,12 @@ bool(Bool) -> error({invalid_boolean, Bool}). number_to_binary(Int) when is_integer(Int) -> integer_to_binary(Int); number_to_binary(Float) when is_float(Float) -> - float_to_binary(Float, [{decimals, 10}, compact]). + float_to_binary(Float, [{decimals, ?FLOAT_PRECISION}, compact]). number_to_list(Int) when is_integer(Int) -> integer_to_list(Int); number_to_list(Float) when is_float(Float) -> - float_to_list(Float, [{decimals, 10}, compact]). + float_to_list(Float, [{decimals, ?FLOAT_PRECISION}, compact]). parse_nested(Attr) -> case string:split(Attr, <<".">>, all) of