Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Math/Log10Tests.java
38812 views
1
/*
2
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 4074599 4939441
27
* @summary Tests for {Math, StrictMath}.log10
28
* @author Joseph D. Darcy
29
*/
30
31
import sun.misc.DoubleConsts;
32
33
public class Log10Tests {
34
private Log10Tests(){}
35
36
static final double infinityD = Double.POSITIVE_INFINITY;
37
static final double NaNd = Double.NaN;
38
static final double LN_10 = StrictMath.log(10.0);
39
40
// Initialize shared random number generator
41
static java.util.Random rand = new java.util.Random(0L);
42
43
static int testLog10Case(double input, double expected) {
44
int failures=0;
45
46
failures+=Tests.test("Math.log10(double)", input,
47
Math.log10(input), expected);
48
49
failures+=Tests.test("StrictMath.log10(double)", input,
50
StrictMath.log10(input), expected);
51
52
return failures;
53
}
54
55
static int testLog10() {
56
int failures = 0;
57
58
double [][] testCases = {
59
{Double.NaN, NaNd},
60
{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
61
{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
62
{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
63
{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
64
{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
65
{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
66
{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
67
{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
68
{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
69
{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
70
{Double.NEGATIVE_INFINITY, NaNd},
71
{-8.0, NaNd},
72
{-1.0, NaNd},
73
{-DoubleConsts.MIN_NORMAL, NaNd},
74
{-Double.MIN_VALUE, NaNd},
75
{-0.0, -infinityD},
76
{+0.0, -infinityD},
77
{+1.0, 0.0},
78
{Double.POSITIVE_INFINITY, infinityD},
79
};
80
81
// Test special cases
82
for(int i = 0; i < testCases.length; i++) {
83
failures += testLog10Case(testCases[i][0],
84
testCases[i][1]);
85
}
86
87
// Test log10(10^n) == n for integer n; 10^n, n < 0 is not
88
// exactly representable as a floating-point value -- up to
89
// 10^22 can be represented exactly
90
double testCase = 1.0;
91
for(int i = 0; i < 23; i++) {
92
failures += testLog10Case(testCase, i);
93
testCase *= 10.0;
94
}
95
96
// Test for gross inaccuracy by comparing to log; should be
97
// within a few ulps of log(x)/log(10)
98
for(int i = 0; i < 10000; i++) {
99
double input = Double.longBitsToDouble(rand.nextLong());
100
if(! Double.isFinite(input))
101
continue; // avoid testing NaN and infinite values
102
else {
103
input = Math.abs(input);
104
105
double expected = StrictMath.log(input)/LN_10;
106
if( ! Double.isFinite(expected))
107
continue; // if log(input) overflowed, try again
108
else {
109
double result;
110
111
if( Math.abs(((result=Math.log10(input)) - expected)/Math.ulp(expected)) > 3) {
112
failures++;
113
System.err.println("For input " + input +
114
", Math.log10 was more than 3 ulps different from " +
115
"log(input)/log(10): log10(input) = " + result +
116
"\tlog(input)/log(10) = " + expected);
117
}
118
119
if( Math.abs(((result=StrictMath.log10(input)) - expected)/Math.ulp(expected)) > 3) {
120
failures++;
121
System.err.println("For input " + input +
122
", StrictMath.log10 was more than 3 ulps different from " +
123
"log(input)/log(10): log10(input) = " + result +
124
"\tlog(input)/log(10) = " + expected);
125
}
126
127
128
}
129
}
130
}
131
132
// Test for accuracy and monotonicity near log10(1.0). From
133
// the Taylor expansion of log,
134
// log10(1+z) ~= (z -(z^2)/2)/LN_10;
135
{
136
double neighbors[] = new double[40];
137
double neighborsStrict[] = new double[40];
138
double z = Double.NaN;
139
140
// Test inputs greater than 1.0.
141
neighbors[0] = Math.log10(1.0);
142
neighborsStrict[0] = StrictMath.log10(1.0);
143
144
double input[] = new double[40];
145
int half = input.length/2;
146
147
148
// Initialize input to the 40 consecutive double values
149
// "centered" at 1.0.
150
double up = Double.NaN;
151
double down = Double.NaN;
152
for(int i = 0; i < half; i++) {
153
if (i == 0) {
154
input[half] = 1.0;
155
up = Math.nextUp(1.0);
156
down = Math.nextDown(1.0);
157
} else {
158
input[half + i] = up;
159
input[half - i] = down;
160
up = Math.nextUp(up);
161
down = Math.nextDown(down);
162
}
163
}
164
input[0] = Math.nextDown(input[1]);
165
166
for(int i = 0; i < neighbors.length; i++) {
167
neighbors[i] = Math.log10(input[i]);
168
neighborsStrict[i] = StrictMath.log10(input[i]);
169
170
// Test accuracy.
171
z = input[i] - 1.0;
172
double expected = (z - (z*z)*0.5)/LN_10;
173
if ( Math.abs(neighbors[i] - expected ) > 3*Math.ulp(expected) ) {
174
failures++;
175
System.err.println("For input near 1.0 " + input[i] +
176
", Math.log10(1+z) was more than 3 ulps different from " +
177
"(z-(z^2)/2)/ln(10): log10(input) = " + neighbors[i] +
178
"\texpected about = " + expected);
179
}
180
181
if ( Math.abs(neighborsStrict[i] - expected ) > 3*Math.ulp(expected) ) {
182
failures++;
183
System.err.println("For input near 1.0 " + input[i] +
184
", StrictMath.log10(1+z) was more than 3 ulps different from " +
185
"(z-(z^2)/2)/ln(10): log10(input) = " + neighborsStrict[i] +
186
"\texpected about = " + expected);
187
}
188
189
// Test monotonicity
190
if( i > 0) {
191
if( neighbors[i-1] > neighbors[i] ) {
192
failures++;
193
System.err.println("Monotonicity failure for Math.log10 at " + input[i] +
194
" and prior value.");
195
}
196
197
if( neighborsStrict[i-1] > neighborsStrict[i] ) {
198
failures++;
199
System.err.println("Monotonicity failure for StrictMath.log10 at " + input[i] +
200
" and prior value.");
201
}
202
}
203
}
204
205
}
206
207
return failures;
208
}
209
210
public static void main(String argv[]) {
211
int failures = 0;
212
213
failures += testLog10();
214
215
if (failures > 0) {
216
System.err.println("Testing log10 incurred "
217
+ failures + " failures.");
218
throw new RuntimeException();
219
}
220
}
221
222
}
223
224