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
26-include_lib("common_test/include/ct.hrl").
27
28suite() -> [{ct_hooks,[ts_install_cth]}].
29
30all() ->
31    [pending, bif_calls, math_functions,
32     mixed_float_and_int, subtract_number_type,
33     float_followed_by_guard].
34
35groups() ->
36    [].
37
38init_per_suite(Config) ->
39    test_lib:recompile(?MODULE),
40    Config.
41
42end_per_suite(_Config) ->
43    ok.
44
45init_per_group(_GroupName, Config) ->
46    Config.
47
48end_per_group(_GroupName, Config) ->
49    Config.
50
51
52%% Thanks to Tobias Lindahl <tobias.lindahl@it.uu.se>
53%% Shows the effect of pending exceptions on the x86.
54
55pending(Config) when is_list(Config) ->
56    case catch float_mul(1, 1.1e300, 3.14e300) of
57	{'EXIT',{badarith,_}} -> ok;
58	Other -> ct:fail({expected_exception,Other})
59    end,
60    0.0 = float_sub(2.0).
61
62float_sub(A)->
63    catch A - 2.0.
64
65float_mul(0, _, _)->
66    ok;
67float_mul(Iter, A, B) when is_float(A), is_float(B) ->
68    _ = A*B,
69    float_mul(Iter-1, A, B).
70
71%% Thanks to Mikael Pettersson and Tobias Lindahl (HiPE).
72
73bif_calls(Config) when is_list(Config) ->
74    {'EXIT',{badarith,_}} = (catch bad_arith(2.0, 1.7)),
75    {'EXIT',{badarith,_}} = (catch bad_arith_again(2.0, [])),
76    {'EXIT',{badarith,_}} = (catch bad_arith_xor(2.0, [])),
77    {'EXIT',{badarith,_}} = (catch bad_arith_hd(2.0, [])),
78    {'EXIT',{badarith,_}} = (catch bad_negate(2.0, 1.7)),
79    ok.
80
81bad_arith(X, Y) when is_float(X) ->
82    X1 = X * 1.7e+308,
83    X2 = X1 + 1.0,
84    Y1 = Y * 2,					%Calls erts_mixed_times/2.
85						%(A BIF call.)
86    {X2, Y1}.
87
88bad_arith_xor(X, Y) when is_float(X) ->
89    X1 = X * 1.7e+308,
90    Y1 = Y xor true,				%A failing BIF call.
91    {X1 + 1.0, Y1}.
92
93bad_arith_hd(X, Y) when is_float(X) ->
94    X1 = X * 1.7e+308,
95    Y1 = hd(Y),					%A failing BIF call.
96    {X1 + 1.0, Y1}.
97
98bad_arith_again(X, Y) when is_float(X) ->
99    X1 = X * 1.7e+308,
100    Y1 = element(1, Y),				%A failing BIF call.
101    {X1 + 1.0, Y1}.
102
103bad_negate(X, Y) when is_float(X) ->
104    X1 = X * 1.7e+308,
105    X2 = X1 + 1.0,
106    Y1 = -Y,					%BIF call.
107    {X2, Y1}.
108
109%% Some math functions are not implemented on all platforms.
110-define(OPTIONAL(Expected, Expr),
111	try
112	    Expected = Expr
113	catch
114	    error:undef -> ok
115	end).
116
117math_functions(Config) when is_list(Config) ->
118    %% Mostly silly coverage.
119    0.0 = math:tan(0),
120    0.0 = math:atan2(0, 1),
121    0.0 = math:sinh(0),
122    1.0 = math:cosh(0),
123    0.0 = math:tanh(0),
124    1.0 = math:log2(2),
125    1.0 = math:log10(10),
126    -1.0 = math:cos(math:pi()),
127    1.0 = math:exp(0),
128    1.0 = math:pow(math:pi(), 0),
129    0.0 = math:log(1),
130    0.0 = math:asin(0),
131    0.0 = math:acos(1),
132    ?OPTIONAL(0.0, math:asinh(0)),
133    ?OPTIONAL(0.0, math:acosh(1)),
134    ?OPTIONAL(0.0, math:atanh(0)),
135    ?OPTIONAL(0.0, math:erf(0)),
136    ?OPTIONAL(1.0, math:erfc(0)),
137
138    0.0 = math:tan(id(0)),
139    0.0 = math:atan2(id(0), 1),
140    0.0 = math:sinh(id(0)),
141    1.0 = math:cosh(id(0)),
142    0.0 = math:tanh(id(0)),
143    1.0 = math:log2(id(2)),
144    1.0 = math:log10(id(10)),
145    1.0 = math:exp(id(0)),
146    0.0 = math:log(id(1)),
147    0.0 = math:asin(id(0)),
148    0.0 = math:acos(id(1)),
149    ?OPTIONAL(0.0, math:asinh(id(0))),
150    ?OPTIONAL(0.0, math:acosh(id(1))),
151    ?OPTIONAL(0.0, math:atanh(id(0))),
152    ?OPTIONAL(0.0, math:erf(id(0))),
153    ?OPTIONAL(1.0, math:erfc(id(0))),
154
155    5.0 = math:floor(5.6),
156    6.0 = math:ceil(5.6),
157    5.0 = math:floor(id(5.4)),
158    6.0 = math:ceil(id(5.4)),
159
160    0.0 = math:fmod(42, 42),
161    0.25 = math:fmod(1, 0.75),
162    -1.0 = math:fmod(-4.0, 1.5),
163    -0.375 = math:fmod(-3.0, -0.875),
164    0.125 = math:fmod(8.125, -4),
165    {'EXIT',{badarith,_}} = (catch math:fmod(5.0, 0.0)),
166
167    %% Only for coverage (of beam_type.erl).
168    {'EXIT',{undef,_}} = (catch math:fnurfla(0)),
169    {'EXIT',{undef,_}} = (catch math:fnurfla(0, 0)),
170    {'EXIT',{badarg,_}} = (catch float(kalle)),
171    {'EXIT',{badarith,_}} = (catch name/1),
172    ok.
173
174mixed_float_and_int(Config) when is_list(Config) ->
175    129.0 = pc(77, 23, 5),
176    ok.
177
178pc(Cov, NotCov, X) ->
179    round(Cov/(Cov+NotCov)*100) + 42 + 2.0*X.
180
181subtract_number_type(Config) when is_list(Config) ->
182    120 = fact(5).
183
184fact(N) ->
185    fact(N, 1).
186
187fact(0, P) -> P;
188fact(1, P) -> P;
189fact(N, P) -> fact(N-1, P*N).
190
191float_followed_by_guard(Config) when is_list(Config) ->
192    true = ffbg_1(5, 1),
193    false = ffbg_1(1, 5),
194    ok.
195
196ffbg_1(A, B0) ->
197    %% This is a non-guard block followed by a *guard block* that starts with a
198    %% floating point operation, and the compiler erroneously assumed that it
199    %% was safe to skip fcheckerror because the next block started with a float
200    %% op.
201    B = id(B0) / 1.0,
202    if
203        A - B > 0.0 -> true;
204        A - B =< 0.0 -> false
205    end.
206
207id(I) -> I.
208
209