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 6274390 27 * @summary Verify {float, double}Value methods work with condensed representation 28 */ 29 import java.math.*; 30 31 public class FloatDoubleValueTests { 32 private static final long two2the24 = 1L<<23; 33 private static final long two2the53 = 1L<<52; 34 35 // Largest long that fits exactly in a float 36 private static final long maxFltLong = (long)(Integer.MAX_VALUE & ~(0xff)); 37 38 // Largest long that fits exactly in a double 39 private static final long maxDblLong = Long.MAX_VALUE & ~(0x7ffL); 40 41 static void testDoubleValue0(long i, BigDecimal bd) { 42 if (bd.doubleValue() != i || 43 bd.longValue() != i) 44 throw new RuntimeException("Unexpected equality failure for " + 45 i + "\t" + bd); 46 } 47 48 static void testFloatValue0(long i, BigDecimal bd) { 49 if (bd.floatValue() != i || 50 bd.longValue() != i) 51 throw new RuntimeException("Unexpected equality failure for " + 52 i + "\t" + bd); 53 } 54 55 static void checkFloat(BigDecimal bd, float f) { 56 float fbd = bd.floatValue(); 57 if (f != fbd ) { 58 String message = String.format("Bad conversion:"+ 59 "got %g (%a)\texpected %g (%a)", 60 f, f, fbd, fbd); 61 throw new RuntimeException(message); 62 } 63 } 64 65 static void checkDouble(BigDecimal bd, double d) { 66 double dbd = bd.doubleValue(); 67 if (d != dbd ) { 68 String message = String.format("Bad conversion:"+ 69 "got %g (%a)\texpected %g (%a)", 70 d, d, dbd, dbd); 71 throw new RuntimeException(message); 72 } 73 } 74 75 // Test integral values that will convert exactly to both float 76 // and double. 77 static void testFloatDoubleValue() { 78 long longValues[] = { 79 0, 80 1, 81 2, 82 83 two2the24-1, 84 two2the24, 85 two2the24+1, 86 139 checkFloat(bd2, -(float)i); 140 } 141 142 checkDouble(new BigDecimal(Long.MIN_VALUE), (double)Long.MIN_VALUE); 143 checkDouble(new BigDecimal(Long.MAX_VALUE), (double)Long.MAX_VALUE); 144 } 145 146 static void testFloatValue() { 147 // Now check values that should not convert the same in float 148 for(long i = maxFltLong; i <= Integer.MAX_VALUE; i++) { 149 BigDecimal bd1 = new BigDecimal(i); 150 BigDecimal bd2 = new BigDecimal(-i); 151 checkFloat(bd1, (float)i); 152 checkFloat(bd2, -(float)i); 153 154 testDoubleValue0( i, bd1); 155 testDoubleValue0(-i, bd2); 156 } 157 } 158 159 public static void main(String[] args) throws Exception { 160 testFloatDoubleValue(); 161 testDoubleValue(); 162 testFloatValue(); 163 } 164 } | 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 6274390 27 * @summary Verify {float, double}Value methods work with condensed representation 28 * @run main FloatDoubleValueTests 29 * @run main/othervm -XX:+AggressiveOpts FloatDoubleValueTests 30 */ 31 import java.math.*; 32 33 public class FloatDoubleValueTests { 34 private static final long two2the24 = 1L<<23; 35 private static final long two2the53 = 1L<<52; 36 37 // Largest long that fits exactly in a float 38 private static final long maxFltLong = (long)(Integer.MAX_VALUE & ~(0xff)); 39 40 // Largest long that fits exactly in a double 41 private static final long maxDblLong = Long.MAX_VALUE & ~(0x7ffL); 42 43 static void testDoubleValue0(long i, BigDecimal bd) { 44 if (bd.doubleValue() != i || 45 bd.longValue() != i) 46 throw new RuntimeException("Unexpected equality failure for " + 47 i + "\t" + bd); 48 } 49 50 static void testFloatValue0(long i, BigDecimal bd) { 51 if (bd.floatValue() != i || 52 bd.longValue() != i) 53 throw new RuntimeException("Unexpected equality failure for " + 54 i + "\t" + bd); 55 } 56 57 static void checkFloat(BigDecimal bd, float f) { 58 float fbd = bd.floatValue(); 59 if (f != fbd ) { 60 String message = String.format("Bad conversion:"+ 61 "got %g (%a)\texpected %g (%a)", 62 f, f, fbd, fbd); 63 throw new RuntimeException(message); 64 } 65 } 66 67 static void checkDouble(BigDecimal bd, double d) { 68 double dbd = bd.doubleValue(); 69 70 if (d != dbd ) { 71 String message = String.format("Bad conversion:"+ 72 "got %g (%a)\texpected %g (%a)", 73 d, d, dbd, dbd); 74 throw new RuntimeException(message); 75 } 76 } 77 78 // Test integral values that will convert exactly to both float 79 // and double. 80 static void testFloatDoubleValue() { 81 long longValues[] = { 82 0, 83 1, 84 2, 85 86 two2the24-1, 87 two2the24, 88 two2the24+1, 89 142 checkFloat(bd2, -(float)i); 143 } 144 145 checkDouble(new BigDecimal(Long.MIN_VALUE), (double)Long.MIN_VALUE); 146 checkDouble(new BigDecimal(Long.MAX_VALUE), (double)Long.MAX_VALUE); 147 } 148 149 static void testFloatValue() { 150 // Now check values that should not convert the same in float 151 for(long i = maxFltLong; i <= Integer.MAX_VALUE; i++) { 152 BigDecimal bd1 = new BigDecimal(i); 153 BigDecimal bd2 = new BigDecimal(-i); 154 checkFloat(bd1, (float)i); 155 checkFloat(bd2, -(float)i); 156 157 testDoubleValue0( i, bd1); 158 testDoubleValue0(-i, bd2); 159 } 160 } 161 162 static void testFloatValue1() { 163 checkFloat(new BigDecimal("85070591730234615847396907784232501249"), 8.507059e+37f); 164 checkFloat(new BigDecimal("7784232501249e12"), 7.7842326e24f); 165 checkFloat(new BigDecimal("907784232501249e-12"),907.78424f); 166 checkFloat(new BigDecimal("7784e8"),7.7839997e11f); 167 checkFloat(new BigDecimal("9077e-8"),9.077e-5f); 168 169 } 170 171 static void testDoubleValue1() { 172 checkDouble(new BigDecimal("85070591730234615847396907784232501249"), 8.507059173023462e37); 173 checkDouble(new BigDecimal("7784232501249e12"), 7.784232501249e24); 174 checkDouble(new BigDecimal("907784232501249e-12"), 907.784232501249); 175 checkDouble(new BigDecimal("7784e8"), 7.784e11); 176 checkDouble(new BigDecimal("9077e-8"), 9.077e-5); 177 178 } 179 180 public static void main(String[] args) throws Exception { 181 testFloatDoubleValue(); 182 testDoubleValue(); 183 testFloatValue(); 184 testFloatValue1(); 185 testDoubleValue1(); 186 } 187 } |