809N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
809N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
809N/A *
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 *
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 *
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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
809N/A */
809N/A
809N/A/*
809N/A * @test
809N/A * @bug 6274390
809N/A * @summary Verify {float, double}Value methods work with condensed representation
4014N/A * @run main FloatDoubleValueTests
4014N/A * @run main/othervm -XX:+AggressiveOpts FloatDoubleValueTests
809N/A */
809N/Aimport java.math.*;
809N/A
809N/Apublic class FloatDoubleValueTests {
809N/A private static final long two2the24 = 1L<<23;
809N/A private static final long two2the53 = 1L<<52;
809N/A
809N/A // Largest long that fits exactly in a float
809N/A private static final long maxFltLong = (long)(Integer.MAX_VALUE & ~(0xff));
809N/A
809N/A // Largest long that fits exactly in a double
809N/A private static final long maxDblLong = Long.MAX_VALUE & ~(0x7ffL);
809N/A
809N/A static void testDoubleValue0(long i, BigDecimal bd) {
809N/A if (bd.doubleValue() != i ||
809N/A bd.longValue() != i)
809N/A throw new RuntimeException("Unexpected equality failure for " +
809N/A i + "\t" + bd);
809N/A }
809N/A
809N/A static void testFloatValue0(long i, BigDecimal bd) {
809N/A if (bd.floatValue() != i ||
809N/A bd.longValue() != i)
809N/A throw new RuntimeException("Unexpected equality failure for " +
809N/A i + "\t" + bd);
809N/A }
809N/A
809N/A static void checkFloat(BigDecimal bd, float f) {
809N/A float fbd = bd.floatValue();
809N/A if (f != fbd ) {
809N/A String message = String.format("Bad conversion:"+
809N/A "got %g (%a)\texpected %g (%a)",
809N/A f, f, fbd, fbd);
809N/A throw new RuntimeException(message);
809N/A }
809N/A }
809N/A
809N/A static void checkDouble(BigDecimal bd, double d) {
809N/A double dbd = bd.doubleValue();
4014N/A
809N/A if (d != dbd ) {
809N/A String message = String.format("Bad conversion:"+
809N/A "got %g (%a)\texpected %g (%a)",
809N/A d, d, dbd, dbd);
809N/A throw new RuntimeException(message);
809N/A }
809N/A }
809N/A
809N/A // Test integral values that will convert exactly to both float
809N/A // and double.
809N/A static void testFloatDoubleValue() {
809N/A long longValues[] = {
809N/A 0,
809N/A 1,
809N/A 2,
809N/A
809N/A two2the24-1,
809N/A two2the24,
809N/A two2the24+1,
809N/A
809N/A maxFltLong-1,
809N/A maxFltLong,
809N/A maxFltLong+1,
809N/A };
809N/A
809N/A for(long i : longValues) {
809N/A BigDecimal bd1 = new BigDecimal(i);
809N/A BigDecimal bd2 = new BigDecimal(-i);
809N/A
809N/A testDoubleValue0( i, bd1);
809N/A testDoubleValue0(-i, bd2);
809N/A
809N/A testFloatValue0( i, bd1);
809N/A testFloatValue0(-i, bd2);
809N/A }
809N/A
809N/A }
809N/A
809N/A static void testDoubleValue() {
809N/A long longValues[] = {
809N/A Integer.MAX_VALUE-1,
809N/A Integer.MAX_VALUE,
809N/A (long)Integer.MAX_VALUE+1,
809N/A
809N/A two2the53-1,
809N/A two2the53,
809N/A two2the53+1,
809N/A
809N/A maxDblLong,
809N/A };
809N/A
809N/A // Test integral values that will convert exactly to double
809N/A // but not float.
809N/A for(long i : longValues) {
809N/A BigDecimal bd1 = new BigDecimal(i);
809N/A BigDecimal bd2 = new BigDecimal(-i);
809N/A
809N/A testDoubleValue0( i, bd1);
809N/A testDoubleValue0(-i, bd2);
809N/A
809N/A checkFloat(bd1, (float)i);
809N/A checkFloat(bd2, -(float)i);
809N/A }
809N/A
809N/A // Now check values that should not convert the same in double
809N/A for(long i = maxDblLong; i < Long.MAX_VALUE; i++) {
809N/A BigDecimal bd1 = new BigDecimal(i);
809N/A BigDecimal bd2 = new BigDecimal(-i);
809N/A checkDouble(bd1, (double)i);
809N/A checkDouble(bd2, -(double)i);
809N/A
809N/A checkFloat(bd1, (float)i);
809N/A checkFloat(bd2, -(float)i);
809N/A }
809N/A
809N/A checkDouble(new BigDecimal(Long.MIN_VALUE), (double)Long.MIN_VALUE);
809N/A checkDouble(new BigDecimal(Long.MAX_VALUE), (double)Long.MAX_VALUE);
809N/A }
809N/A
809N/A static void testFloatValue() {
809N/A // Now check values that should not convert the same in float
809N/A for(long i = maxFltLong; i <= Integer.MAX_VALUE; i++) {
809N/A BigDecimal bd1 = new BigDecimal(i);
809N/A BigDecimal bd2 = new BigDecimal(-i);
809N/A checkFloat(bd1, (float)i);
809N/A checkFloat(bd2, -(float)i);
809N/A
809N/A testDoubleValue0( i, bd1);
809N/A testDoubleValue0(-i, bd2);
809N/A }
809N/A }
809N/A
4014N/A static void testFloatValue1() {
4014N/A checkFloat(new BigDecimal("85070591730234615847396907784232501249"), 8.507059e+37f);
4014N/A checkFloat(new BigDecimal("7784232501249e12"), 7.7842326e24f);
4014N/A checkFloat(new BigDecimal("907784232501249e-12"),907.78424f);
4014N/A checkFloat(new BigDecimal("7784e8"),7.7839997e11f);
4014N/A checkFloat(new BigDecimal("9077e-8"),9.077e-5f);
4014N/A
4014N/A }
4014N/A
4014N/A static void testDoubleValue1() {
4014N/A checkDouble(new BigDecimal("85070591730234615847396907784232501249"), 8.507059173023462e37);
4014N/A checkDouble(new BigDecimal("7784232501249e12"), 7.784232501249e24);
4014N/A checkDouble(new BigDecimal("907784232501249e-12"), 907.784232501249);
4014N/A checkDouble(new BigDecimal("7784e8"), 7.784e11);
4014N/A checkDouble(new BigDecimal("9077e-8"), 9.077e-5);
4014N/A
4014N/A }
4014N/A
809N/A public static void main(String[] args) throws Exception {
809N/A testFloatDoubleValue();
809N/A testDoubleValue();
809N/A testFloatValue();
4014N/A testFloatValue1();
4014N/A testDoubleValue1();
809N/A }
809N/A}