CubeRootTests.java revision 809
809N/A * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. 809N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 809N/A * This code is free software; you can redistribute it and/or modify it 809N/A * under the terms of the GNU General Public License version 2 only, as 809N/A * published by the Free Software Foundation. 809N/A * This code is distributed in the hope that it will be useful, but WITHOUT 809N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 809N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 809N/A * version 2 for more details (a copy is included in the LICENSE file that 809N/A * accompanied this code). 809N/A * You should have received a copy of the GNU General Public License version 809N/A * 2 along with this work; if not, write to the Free Software Foundation, 809N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 809N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 809N/A * CA 95054 USA or visit www.sun.com if you need additional information or 809N/A * @summary Tests for {Math, StrictMath}.cbrt 809N/A * @author Joseph D. Darcy 809N/A // Initialize shared random number generator 809N/A // Test integer perfect cubes less than 2^53. 809N/A for(
int i =
0; i <=
208063; i++) {
809N/A // Test cbrt(2^(3n)) = 2^n. 809N/A // Test cbrt(2^(-3n)) = 2^-n. 809N/A // Test random perfect cubes. Create double values with 809N/A // modest exponents but only have at most the 17 most 809N/A // significant bits in the significand set; 17*3 = 51, which 809N/A // is less than the number of bits in a double's significand. 809N/A for(
int i =
0; i <
100; i++) {
809N/A // Take 16 bits since the 17th bit is implicit in the 809N/A // Directly test quality of implementation properties of cbrt 809N/A // for values that aren't perfect cubes. Verify returned 809N/A // result meets the 1 ulp test. That is, we want to verify 809N/A // that for positive x > 1, 809N/A // if (err1=x - y^3 ) < 0, abs((y_pp^3 -x )) < err1 809N/A // if (err1=x - y^3 ) > 0, abs((y_mm^3 -x )) < err1 809N/A // where y_mm and y_pp are the next smaller and next larger 809N/A // floating-point value to y. In other words, if y^3 is too 809N/A // big, making y larger does not improve the result; likewise, 809N/A // if y^3 is too small, making y smaller does not improve the 809N/A // ...-----|--?--|--?--|-----... Where is the true result? 809N/A // The returned value y should be one of the floating-point 809N/A // values braketing the true result. However, given y, a 809N/A // priori we don't know if the true result falls in [y_mm, y] 809N/A // or [y, y_pp]. The above test looks at the error in x-y^3 809N/A // to determine which region the true result is in; e.g. if 809N/A // y^3 is smaller than x, the true result should be in [y, 809N/A // y_pp]. Therefore, it would be an error for y_mm to be a 809N/A // closer approximation to x^(1/3). In this case, it is 809N/A // permissible, although not ideal, for y_pp^3 to be a closer 809N/A // approximation to x^(1/3) than y^3. 809N/A // We will use pow(y,3) to compute y^3. Although pow is not 809N/A // correctly rounded, StrictMath.pow should have at most 1 ulp 809N/A // error. For y > 1, pow(y_mm,3) and pow(y_pp,3) will differ 809N/A // from pow(y,3) by more than one ulp so the comparision of 809N/A // errors should still be valid. 809N/A for(
int i =
0; i <
1000; i++) {
809N/A y1 +
"is not as good as adjacent value.");
809N/A y2 +
"is not as good as adjacent value.");
809N/A // Test monotonicity properites near perfect cubes; test two 809N/A // numbers before and two numbers after; i.e. for 809N/A // {nextDown(nextDown(pc)), 809N/A // test that cbrt(pcNeighbors[i]) <= cbrt(pcNeighbors[i+1]) 809N/A // Test near cbrt(2^(3n)) = 2^n. 809N/A // Test near cbrt(2^(-3n)) = 2^-n.