1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2002-2020. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%% 20-module(float_SUITE). 21-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 22 init_per_group/2,end_per_group/2, 23 pending/1,bif_calls/1,math_functions/1,mixed_float_and_int/1, 24 subtract_number_type/1,float_followed_by_guard/1, 25 fconv_line_numbers/1,float_zero/1,exception_signals/1]). 26 27-include_lib("common_test/include/ct.hrl"). 28 29suite() -> [{ct_hooks,[ts_install_cth]}]. 30 31all() -> 32 [pending, bif_calls, math_functions, float_zero, 33 mixed_float_and_int, subtract_number_type, 34 float_followed_by_guard,fconv_line_numbers, 35 exception_signals]. 36 37groups() -> 38 []. 39 40init_per_suite(Config) -> 41 test_lib:recompile(?MODULE), 42 Config. 43 44end_per_suite(_Config) -> 45 ok. 46 47init_per_group(_GroupName, Config) -> 48 Config. 49 50end_per_group(_GroupName, Config) -> 51 Config. 52 53float_zero(Config) when is_list(Config) -> 54 <<16#0000000000000000:64>> = match_on_zero_and_to_binary(1*0.0), 55 <<16#8000000000000000:64>> = match_on_zero_and_to_binary(-1*0.0), 56 ok. 57 58match_on_zero_and_to_binary(0.0 = X) -> <<X/float>>. 59 60%% Thanks to Tobias Lindahl <tobias.lindahl@it.uu.se> 61%% Shows the effect of pending exceptions on the x86. 62 63pending(Config) when is_list(Config) -> 64 case catch float_mul(1, 1.1e300, 3.14e300) of 65 {'EXIT',{badarith,_}} -> ok; 66 Other -> ct:fail({expected_exception,Other}) 67 end, 68 0.0 = float_sub(2.0). 69 70float_sub(A)-> 71 catch A - 2.0. 72 73float_mul(0, _, _)-> 74 ok; 75float_mul(Iter, A, B) when is_float(A), is_float(B) -> 76 _ = A*B, 77 float_mul(Iter-1, A, B). 78 79%% Thanks to Mikael Pettersson and Tobias Lindahl (HiPE). 80 81bif_calls(Config) when is_list(Config) -> 82 {'EXIT',{badarith,_}} = (catch bad_arith(2.0, 1.7)), 83 {'EXIT',{badarith,_}} = (catch bad_arith_again(2.0, [])), 84 {'EXIT',{badarith,_}} = (catch bad_arith_xor(2.0, [])), 85 {'EXIT',{badarith,_}} = (catch bad_arith_hd(2.0, [])), 86 {'EXIT',{badarith,_}} = (catch bad_negate(2.0, 1.7)), 87 ok. 88 89bad_arith(X, Y) when is_float(X) -> 90 X1 = X * 1.7e+308, 91 X2 = X1 + 1.0, 92 Y1 = Y * 2, %Calls erts_mixed_times/2. 93 %(A BIF call.) 94 {X2, Y1}. 95 96bad_arith_xor(X, Y) when is_float(X) -> 97 X1 = X * 1.7e+308, 98 Y1 = Y xor true, %A failing BIF call. 99 {X1 + 1.0, Y1}. 100 101bad_arith_hd(X, Y) when is_float(X) -> 102 X1 = X * 1.7e+308, 103 Y1 = hd(Y), %A failing BIF call. 104 {X1 + 1.0, Y1}. 105 106bad_arith_again(X, Y) when is_float(X) -> 107 X1 = X * 1.7e+308, 108 Y1 = element(1, Y), %A failing BIF call. 109 {X1 + 1.0, Y1}. 110 111bad_negate(X, Y) when is_float(X) -> 112 X1 = X * 1.7e+308, 113 X2 = X1 + 1.0, 114 Y1 = -Y, %BIF call. 115 {X2, Y1}. 116 117%% Some math functions are not implemented on all platforms. 118-define(OPTIONAL(Expected, Expr), 119 try 120 Expected = Expr 121 catch 122 error:undef -> ok 123 end). 124 125math_functions(Config) when is_list(Config) -> 126 %% Mostly silly coverage. 127 0.0 = math:tan(0), 128 0.0 = math:atan2(0, 1), 129 0.0 = math:sinh(0), 130 1.0 = math:cosh(0), 131 0.0 = math:tanh(0), 132 1.0 = math:log2(2), 133 1.0 = math:log10(10), 134 -1.0 = math:cos(math:pi()), 135 1.0 = math:exp(0), 136 1.0 = math:pow(math:pi(), 0), 137 0.0 = math:log(1), 138 0.0 = math:asin(0), 139 0.0 = math:acos(1), 140 ?OPTIONAL(0.0, math:asinh(0)), 141 ?OPTIONAL(0.0, math:acosh(1)), 142 ?OPTIONAL(0.0, math:atanh(0)), 143 ?OPTIONAL(0.0, math:erf(0)), 144 ?OPTIONAL(1.0, math:erfc(0)), 145 146 0.0 = math:tan(id(0)), 147 0.0 = math:atan2(id(0), 1), 148 0.0 = math:sinh(id(0)), 149 1.0 = math:cosh(id(0)), 150 0.0 = math:tanh(id(0)), 151 1.0 = math:log2(id(2)), 152 1.0 = math:log10(id(10)), 153 1.0 = math:exp(id(0)), 154 0.0 = math:log(id(1)), 155 0.0 = math:asin(id(0)), 156 0.0 = math:acos(id(1)), 157 ?OPTIONAL(0.0, math:asinh(id(0))), 158 ?OPTIONAL(0.0, math:acosh(id(1))), 159 ?OPTIONAL(0.0, math:atanh(id(0))), 160 ?OPTIONAL(0.0, math:erf(id(0))), 161 ?OPTIONAL(1.0, math:erfc(id(0))), 162 163 5.0 = math:floor(5.6), 164 6.0 = math:ceil(5.6), 165 5.0 = math:floor(id(5.4)), 166 6.0 = math:ceil(id(5.4)), 167 168 0.0 = math:fmod(42, 42), 169 0.25 = math:fmod(1, 0.75), 170 -1.0 = math:fmod(-4.0, 1.5), 171 -0.375 = math:fmod(-3.0, -0.875), 172 0.125 = math:fmod(8.125, -4), 173 {'EXIT',{badarith,_}} = (catch math:fmod(5.0, 0.0)), 174 175 %% Only for coverage (of beam_type.erl). 176 {'EXIT',{undef,_}} = (catch math:fnurfla(0)), 177 {'EXIT',{undef,_}} = (catch math:fnurfla(0, 0)), 178 {'EXIT',{badarg,_}} = (catch float(kalle)), 179 {'EXIT',{badarith,_}} = (catch name/1), 180 ok. 181 182mixed_float_and_int(Config) when is_list(Config) -> 183 129.0 = pc(77, 23, 5), 184 ok. 185 186pc(Cov, NotCov, X) -> 187 round(Cov/(Cov+NotCov)*100) + 42 + 2.0*X. 188 189subtract_number_type(Config) when is_list(Config) -> 190 120 = fact(5). 191 192fact(N) -> 193 fact(N, 1). 194 195fact(0, P) -> P; 196fact(1, P) -> P; 197fact(N, P) -> fact(N-1, P*N). 198 199float_followed_by_guard(Config) when is_list(Config) -> 200 true = ffbg_1(5, 1), 201 false = ffbg_1(1, 5), 202 ok. 203 204ffbg_1(A, B0) -> 205 %% This is a non-guard block followed by a *guard block* that starts with a 206 %% floating point operation, and the compiler erroneously assumed that it 207 %% was safe to skip fcheckerror because the next block started with a float 208 %% op. 209 B = id(B0) / 1.0, 210 if 211 A - B > 0.0 -> true; 212 A - B =< 0.0 -> false 213 end. 214 215%% ERL-1178: fconv instructions didn't inherit line numbers from their 216%% respective BIF calls. 217fconv_line_numbers(Config) when is_list(Config) -> 218 fconv_line_numbers_1(id(gurka)), 219 ok. 220 221fconv_line_numbers_1(A) -> 222 %% The ?LINE macro must be on the same line as the division. 223 {'EXIT',{badarith, Stacktrace}} = (catch 10 / A), Line = ?LINE, 224 true = lists:any(fun({?MODULE,?FUNCTION_NAME,1,[{file,_},{line,L}]}) -> 225 L =:= Line; 226 (_) -> 227 false 228 end, Stacktrace). 229 230%% ERL-1471: compiler generated invalid 'fclearerror' / 'fcheckerror' 231%% sequences. 232exception_signals(Config) when is_list(Config) -> 233 2.0 = exception_signals_1(id(25), id(true), []), 234 2.0 = exception_signals_1(id(25), id(false), []), 235 2.0 = exception_signals_1(id(25.0), id(true), []), 236 2.0 = exception_signals_1(id(25.0), id(false), []), 237 ok. 238 239exception_signals_1(Width, Value, _Opts) -> 240 Height = Width / 25.0, 241 _Middle = case Value of 242 true -> Width / 2.0; 243 false -> 0 244 end, 245 _More = Height + 1. 246 247id(I) -> I. 248 249