Debian Codegraph @

gcc/testsuite/gfortran.dg/ieee/dec_math_1.f90

raw
1 ! { dg-do run }
2 ! { dg-additional-options "-cpp -std=gnu" }
3 !
4 ! Test values for degree-valued trigonometric intrinsics.
5 !
6 ! Run under ieee/ as
7 ! use ieee_arithmetic
8 ! (used for result checking) is not available on all platforms)
9
10 module dec_math_5
11
12
13 ! Use the highest precision available.
14 ! Note however that if both __GFC_REAL_10__ and __GFC_REAL_16__ are defined,
15 ! the size of real(16) is actually that of REAL(10) (80 bits) in which case
16 ! we should not over-estimate the precision available, or the test will fail.
17 #if defined(__GFC_REAL_10__)
18 integer, parameter :: real_kind = 10
19 real(real_kind), parameter :: eps = 5e-11_10
20
21 real(real_kind), parameter :: pi_2 = 1.57079632679489656_10
22 real(real_kind), parameter :: pi = 3.14159265358979312_10
23 real(real_kind), parameter :: tau = 6.28318530717958623_10
24
25 #elif defined(__GFC_REAL_16__)
26 integer, parameter :: real_kind = 16
27 real(real_kind), parameter :: eps = 5e-16_16
28
29 real(real_kind), parameter :: pi_2 = 1.5707963267948966192313216916397514_16
30 real(real_kind), parameter :: pi = 3.1415926535897932384626433832795_16
31 real(real_kind), parameter :: tau = 6.28318530717958647692528676655900559_16
32
33 #else
34 integer, parameter :: real_kind = 8
35 real(real_kind), parameter :: eps = 5e-10_8
36
37 real(real_kind), parameter :: pi_2 = 1.57079632679490_8
38 real(real_kind), parameter :: pi = 3.14159265358979_8
39 real(real_kind), parameter :: tau = 6.28318530717959_8
40
41 #endif
42
43 ! Important angles in canonical form.
44
45 integer, parameter :: nangle = 16
46
47 real(real_kind), dimension(nangle), parameter :: degrees = (/ &
48 0, & ! 180 * 0
49 30, & ! 180 * 1/6
50 45, & ! 180 * 1/4
51 60, & ! 180 * 1/3
52 90, & ! 180 * 1/2
53 120, & ! 180 * 2/3
54 135, & ! 180 * 3/4
55 150, & ! 180 * 5/6
56 180, & ! 180
57 210, & ! 180 * 7/6
58 225, & ! 180 * 5/4
59 240, & ! 180 * 4/3
60 270, & ! 180 * 3/2
61 300, & ! 180 * 5/3
62 315, & ! 180 * 7/4
63 330 & ! 180 * 11/6
64 /)
65
66 real(real_kind), dimension(nangle), parameter :: radians = (/ &
67 #ifdef __GFC_REAL_10__
68 0.000000000000000000_10, & ! pi * 0
69 0.523598775598298873_10, & ! pi * 1/6
70 0.785398163397448310_10, & ! pi * 1/4
71 1.047197551196597750_10, & ! pi * 1/3
72 1.570796326794896620_10, & ! pi * 1/2
73 2.094395102393195490_10, & ! pi * 2/3
74 2.356194490192344930_10, & ! pi * 3/4
75 2.617993877991494370_10, & ! pi * 5/6
76 3.141592653589793240_10, & ! pi
77 3.665191429188092110_10, & ! pi * 7/6
78 3.926990816987241550_10, & ! pi * 5/4
79 4.188790204786390980_10, & ! pi * 4/3
80 4.712388980384689860_10, & ! pi * 3/2
81 5.235987755982988730_10, & ! pi * 5/3
82 5.497787143782138170_10, & ! pi * 7/4
83 5.759586531581287600_10 & ! pi * 11/6
84
85 #elif defined(__GFC_REAL_16__)
86 0.000000000000000000000000000000000_16, & ! pi * 0
87 0.523598775598298873077107230546584_16, & ! pi * 1/6
88 0.785398163397448309615660845819876_16, & ! pi * 1/4
89 1.047197551196597746154214461093170_16, & ! pi * 1/3
90 1.570796326794896619231321691639750_16, & ! pi * 1/2
91 2.094395102393195492308428922186330_16, & ! pi * 2/3
92 2.356194490192344928846982537459630_16, & ! pi * 3/4
93 2.617993877991494365385536152732920_16, & ! pi * 5/6
94 3.141592653589793238462643383279500_16, & ! pi
95 3.665191429188092111539750613826090_16, & ! pi * 7/6
96 3.926990816987241548078304229099380_16, & ! pi * 5/4
97 4.188790204786390984616857844372670_16, & ! pi * 4/3
98 4.712388980384689857693965074919250_16, & ! pi * 3/2
99 5.235987755982988730771072305465840_16, & ! pi * 5/3
100 5.497787143782138167309625920739130_16, & ! pi * 7/4
101 5.759586531581287603848179536012420_16 & ! pi * 11/6
102
103 #else
104 0.000000000000000_8, & ! pi * 0
105 0.523598775598299_8, & ! pi * 1/6
106 0.785398163397448_8, & ! pi * 1/4
107 1.047197551196600_8, & ! pi * 1/3
108 1.570796326794900_8, & ! pi * 1/2
109 2.094395102393200_8, & ! pi * 2/3
110 2.356194490192340_8, & ! pi * 3/4
111 2.617993877991490_8, & ! pi * 5/6
112 3.141592653589790_8, & ! pi
113 3.665191429188090_8, & ! pi * 7/6
114 3.926990816987240_8, & ! pi * 5/4
115 4.188790204786390_8, & ! pi * 4/3
116 4.712388980384690_8, & ! pi * 3/2
117 5.235987755982990_8, & ! pi * 5/3
118 5.497787143782140_8, & ! pi * 7/4
119 5.759586531581290_8 & ! pi * 11/6
120 #endif
121 /)
122
123 ! sind, cosd, tand, cotand
124
125 ! Ensure precision degrades minimally for large values.
126 integer, parameter :: nphase = 5
127
128 integer, dimension(nphase), parameter :: phases = (/ &
129 0, 1, 5, 100, 10000 &
130 /)
131
132 contains
133
134 subroutine compare(strl, xl_in, xl_out, strr, xr_in, xr_out, eps)
135 use ieee_arithmetic
136 implicit none
137 character(*), intent(in) :: strl, strr
138 real(real_kind), intent(in) :: xl_in, xl_out, xr_in, xr_out, eps
139
140 if ((ieee_is_nan(xl_out) .neqv. ieee_is_nan(xr_out)) &
141 .or. (ieee_is_finite(xl_out) .neqv. ieee_is_finite(xr_out)) &
142 .or. (abs(xl_out - xr_out) .gt. eps)) then
143 write (*, 100) strl, "(", xl_in, "): ", xl_out
144 write (*, 100) strr, "(", xr_in, "): ", xr_out
145
146 if ((ieee_is_nan(xl_out) .eqv. ieee_is_nan(xr_out)) &
147 .and. ieee_is_finite(xl_out) .and. ieee_is_finite(xr_out)) then
148 write (*, 300) "|xl - xr| = ", abs(xl_out - xr_out)
149 write (*, 300) " > eps = ", eps
150 endif
151
152 call abort()
153 endif
154
155 #ifdef __GFC_REAL_16__
156 100 format((A8,A,F34.30,A,F34.30,F34.30))
157 200 format((A12,F34.30))
158 !500 format((A8,A,G34.29,A,G34.29,G34.29))
159 #elif defined(__GFC_REAL_10__)
160 100 format((A8,A,F21.17,A,F21.17,F21.17))
161 200 format((A12,F21.17))
162 !500 format((A8,A,G21.16,A,G21.16,G21.16))
163 #else
164 100 format((A8,A,F18.14,A,F18.14,F18.14))
165 200 format((A12,F18.14))
166 !500 format((A8,A,G18.13,A,G18.13,G18.13))
167 #endif
168 300 format((A12,G8.2))
169 endsubroutine
170
171 endmodule
172
173 use dec_math_5
174 use ieee_arithmetic
175 implicit none
176
177 integer :: phase_index, angle_index, phase
178 real(real_kind) :: deg_in, deg_out, deg_out2, rad_in, rad_out
179
180 ! Try every value in degrees, and make sure they are correct compared to the
181 ! corresponding radian function.
182
183 do phase_index = 1, size(phases)
184 phase = phases(phase_index)
185
186 do angle_index = 1, size(degrees)
187 ! eqv to degrees(angle_index) modulo 360
188 deg_in = degrees(angle_index) + phase * 360
189 rad_in = radians(angle_index) + phase * tau
190
191 ! sind vs. sin
192 deg_out = sind(deg_in)
193 rad_out = sin(rad_in)
194 call compare("sind", deg_in, deg_out, "sin", rad_in, rad_out, eps)
195
196 ! cosd vs. cos
197 deg_out = cosd(deg_in)
198 rad_out = cos(rad_in)
199 call compare("cosd", deg_in, deg_out, "cos", rad_in, rad_out, eps)
200
201 ! tand vs. tan
202 deg_out = tand(deg_in)
203 rad_out = tan(rad_in)
204 if ( ieee_is_finite(deg_out) ) then
205 call compare("tand", deg_in, deg_out, "tan", rad_in, rad_out, eps)
206 endif
207
208 ! cotand vs. cotan
209 deg_out = cotand(deg_in)
210 rad_out = cotan(rad_in)
211
212 ! Skip comparing infinity, because cotan does not return infinity
213 if ( ieee_is_finite(deg_out) ) then
214 call compare("cotand", deg_in, deg_out, "cotan", rad_in, rad_out, eps)
215
216 ! cotand vs. tand
217 deg_out = cotand(deg_in)
218 deg_out2 = -tand(deg_in + 90)
219
220 call compare("cotand", deg_in, deg_out, "-tand+90", deg_in, deg_out2, eps)
221 deg_out2 = 1 / tand(deg_in)
222 call compare("cotand", deg_in, deg_out, "1/tand", deg_in, deg_out2, eps)
223 endif
224
225 enddo
226
227
228 enddo
229
230
231 end