Path: blob/master/test/jdk/java/math/BigDecimal/FloatDoubleValueTests.java
41149 views
/*1* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6274390 708297126* @summary Verify {float, double}Value methods work with condensed representation27* @run main FloatDoubleValueTests28* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+EliminateAutoBox -XX:AutoBoxCacheMax=20000 FloatDoubleValueTests29*/30import java.math.*;3132public class FloatDoubleValueTests {33private static final long two2the24 = 1L<<23;34private static final long two2the53 = 1L<<52;3536// Largest long that fits exactly in a float37private static final long maxFltLong = (long)(Integer.MAX_VALUE & ~(0xff));3839// Largest long that fits exactly in a double40private static final long maxDblLong = Long.MAX_VALUE & ~(0x7ffL);4142static void testDoubleValue0(long i, BigDecimal bd) {43if (bd.doubleValue() != i ||44bd.longValue() != i)45throw new RuntimeException("Unexpected equality failure for " +46i + "\t" + bd);47}4849static void testFloatValue0(long i, BigDecimal bd) {50if (bd.floatValue() != i ||51bd.longValue() != i)52throw new RuntimeException("Unexpected equality failure for " +53i + "\t" + bd);54}5556static void checkFloat(BigDecimal bd, float f) {57float fbd = bd.floatValue();58if (f != fbd ) {59String message = String.format("Bad conversion:"+60"got %g (%a)\texpected %g (%a)",61f, f, fbd, fbd);62throw new RuntimeException(message);63}64}6566static void checkDouble(BigDecimal bd, double d) {67double dbd = bd.doubleValue();6869if (d != dbd ) {70String message = String.format("Bad conversion:"+71"got %g (%a)\texpected %g (%a)",72d, d, dbd, dbd);73throw new RuntimeException(message);74}75}7677// Test integral values that will convert exactly to both float78// and double.79static void testFloatDoubleValue() {80long longValues[] = {81Long.MIN_VALUE, // -2^63820,831,842,8586two2the24-1,87two2the24,88two2the24+1,8990maxFltLong-1,91maxFltLong,92maxFltLong+1,93};9495for(long i : longValues) {96BigDecimal bd1 = new BigDecimal(i);97BigDecimal bd2 = new BigDecimal(-i);9899testDoubleValue0( i, bd1);100testDoubleValue0(-i, bd2);101102testFloatValue0( i, bd1);103testFloatValue0(-i, bd2);104}105106}107108static void testDoubleValue() {109long longValues[] = {110Integer.MAX_VALUE-1,111Integer.MAX_VALUE,112(long)Integer.MAX_VALUE+1,113114two2the53-1,115two2the53,116two2the53+1,117118maxDblLong,119};120121// Test integral values that will convert exactly to double122// but not float.123for(long i : longValues) {124BigDecimal bd1 = new BigDecimal(i);125BigDecimal bd2 = new BigDecimal(-i);126127testDoubleValue0( i, bd1);128testDoubleValue0(-i, bd2);129130checkFloat(bd1, (float)i);131checkFloat(bd2, -(float)i);132}133134// Now check values that should not convert the same in double135for(long i = maxDblLong; i < Long.MAX_VALUE; i++) {136BigDecimal bd1 = new BigDecimal(i);137BigDecimal bd2 = new BigDecimal(-i);138checkDouble(bd1, (double)i);139checkDouble(bd2, -(double)i);140141checkFloat(bd1, (float)i);142checkFloat(bd2, -(float)i);143}144145checkDouble(new BigDecimal(Long.MIN_VALUE), (double)Long.MIN_VALUE);146checkDouble(new BigDecimal(Long.MAX_VALUE), (double)Long.MAX_VALUE);147}148149static void testFloatValue() {150// Now check values that should not convert the same in float151for(long i = maxFltLong; i <= Integer.MAX_VALUE; i++) {152BigDecimal bd1 = new BigDecimal(i);153BigDecimal bd2 = new BigDecimal(-i);154checkFloat(bd1, (float)i);155checkFloat(bd2, -(float)i);156157testDoubleValue0( i, bd1);158testDoubleValue0(-i, bd2);159}160}161162static void testFloatValue1() {163checkFloat(new BigDecimal("85070591730234615847396907784232501249"), 8.507059e+37f);164checkFloat(new BigDecimal("7784232501249e12"), 7.7842326e24f);165checkFloat(new BigDecimal("907784232501249e-12"),907.78424f);166checkFloat(new BigDecimal("7784e8"),7.7839997e11f);167checkFloat(new BigDecimal("9077e-8"),9.077e-5f);168169}170171static void testDoubleValue1() {172checkDouble(new BigDecimal("85070591730234615847396907784232501249"), 8.507059173023462e37);173checkDouble(new BigDecimal("7784232501249e12"), 7.784232501249e24);174checkDouble(new BigDecimal("907784232501249e-12"), 907.784232501249);175checkDouble(new BigDecimal("7784e8"), 7.784e11);176checkDouble(new BigDecimal("9077e-8"), 9.077e-5);177178}179180public static void main(String[] args) throws Exception {181testFloatDoubleValue();182testDoubleValue();183testFloatValue();184testFloatValue1();185testDoubleValue1();186}187}188189190