From 9091e0fc630913999e3a51e860a2c4e07c6143c1 Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Fri, 16 Mar 2018 19:34:42 +0100 Subject: erts: Fix float_to_list(F, [{decimals,D}]) to better conform with io_lib:format("~.*f", [D,F]) --- erts/emulator/test/num_bif_SUITE.erl | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'erts/emulator/test/num_bif_SUITE.erl') diff --git a/erts/emulator/test/num_bif_SUITE.erl b/erts/emulator/test/num_bif_SUITE.erl index 592542405f..104bd37817 100644 --- a/erts/emulator/test/num_bif_SUITE.erl +++ b/erts/emulator/test/num_bif_SUITE.erl @@ -213,6 +213,20 @@ fts_rand_float_decimals(N) -> [begin F0 = rand_float_reasonable(), L0 = float_to_list(F0, [{decimals, D}]), + case conform_with_io_lib_format(F0,D) of + false -> ok; + true -> + IOL = lists:flatten(io_lib:format("~.*f", [D, F0])), + true = case L0 =:= IOL of + true -> true; + false -> + io:format("F0 = ~w ~w\n", [F0, <>]), + io:format("decimals = ~w\n", [D]), + io:format("float_to_list = ~s\n", [L0]), + io:format("io_lib:format = ~s\n", [IOL]), + false + end + end, L1 = case D of 0 -> L0 ++ ".0"; _ -> L0 @@ -234,6 +248,17 @@ fts_rand_float_decimals(N) -> fts_rand_float_decimals(N-1). +conform_with_io_lib_format(_, 0) -> + %% io_lib:format("~.*f") does not support zero decimals + false; +conform_with_io_lib_format(_, D) when D > 10 -> + %% Seems float_to_list gets it slightly wrong sometimes for many decimals + false; +conform_with_io_lib_format(F, D) -> + %% io_lib:format prints '0' for input bits beyond mantissa precision + %% float_to_list treats those unknown input bits as if they were zeros. + math:log2(abs(F) * math:pow(10,D)) < 54. + max_diff_decimals(F, D) -> IntBits = floor(math:log2(abs(F))) + 1, FracBits = (52 - IntBits), -- cgit v1.2.3 From 9d19331d3e2402a74346e664befb1d1e0749240f Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Tue, 10 Apr 2018 16:15:18 +0200 Subject: erts: Workaround test t_float_to_string for windows --- erts/emulator/test/num_bif_SUITE.erl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'erts/emulator/test/num_bif_SUITE.erl') diff --git a/erts/emulator/test/num_bif_SUITE.erl b/erts/emulator/test/num_bif_SUITE.erl index 104bd37817..290bb61fc8 100644 --- a/erts/emulator/test/num_bif_SUITE.erl +++ b/erts/emulator/test/num_bif_SUITE.erl @@ -213,7 +213,7 @@ fts_rand_float_decimals(N) -> [begin F0 = rand_float_reasonable(), L0 = float_to_list(F0, [{decimals, D}]), - case conform_with_io_lib_format(F0,D) of + case conform_with_io_lib_format_os(F0,D) of false -> ok; true -> IOL = lists:flatten(io_lib:format("~.*f", [D, F0])), @@ -248,6 +248,15 @@ fts_rand_float_decimals(N) -> fts_rand_float_decimals(N-1). +conform_with_io_lib_format_os(F, D) -> + case os:type() of + {win32,_} -> + %% io_lib:format("~.*f") buggy on windows? OTP-15010 + false; + _ -> + conform_with_io_lib_format(F, D) + end. + conform_with_io_lib_format(_, 0) -> %% io_lib:format("~.*f") does not support zero decimals false; -- cgit v1.2.3