< prev index next >

src/java.base/share/classes/java/lang/Math.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 1474,1503 **** */ public static double ulp(double d) { int exp = getExponent(d); switch(exp) { ! case DoubleConsts.MAX_EXPONENT+1: // NaN or infinity return Math.abs(d); ! case DoubleConsts.MIN_EXPONENT-1: // zero or subnormal return Double.MIN_VALUE; default: ! assert exp <= DoubleConsts.MAX_EXPONENT && exp >= DoubleConsts.MIN_EXPONENT; // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x)) exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1); ! if (exp >= DoubleConsts.MIN_EXPONENT) { return powerOfTwoD(exp); } else { // return a subnormal result; left shift integer // representation of Double.MIN_VALUE appropriate // number of positions return Double.longBitsToDouble(1L << ! (exp - (DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) )); } } } /** --- 1474,1503 ---- */ public static double ulp(double d) { int exp = getExponent(d); switch(exp) { ! case Double.MAX_EXPONENT + 1: // NaN or infinity return Math.abs(d); ! case Double.MIN_EXPONENT - 1: // zero or subnormal return Double.MIN_VALUE; default: ! assert exp <= Double.MAX_EXPONENT && exp >= Double.MIN_EXPONENT; // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x)) exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1); ! if (exp >= Double.MIN_EXPONENT) { return powerOfTwoD(exp); } else { // return a subnormal result; left shift integer // representation of Double.MIN_VALUE appropriate // number of positions return Double.longBitsToDouble(1L << ! (exp - (Double.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) )); } } } /**
*** 1525,1554 **** */ public static float ulp(float f) { int exp = getExponent(f); switch(exp) { ! case FloatConsts.MAX_EXPONENT+1: // NaN or infinity return Math.abs(f); ! case FloatConsts.MIN_EXPONENT-1: // zero or subnormal ! return FloatConsts.MIN_VALUE; default: ! assert exp <= FloatConsts.MAX_EXPONENT && exp >= FloatConsts.MIN_EXPONENT; // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x)) exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1); ! if (exp >= FloatConsts.MIN_EXPONENT) { return powerOfTwoF(exp); ! } ! else { // return a subnormal result; left shift integer // representation of FloatConsts.MIN_VALUE appropriate // number of positions return Float.intBitsToFloat(1 << ! (exp - (FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) )); } } } /** --- 1525,1553 ---- */ public static float ulp(float f) { int exp = getExponent(f); switch(exp) { ! case Float.MAX_EXPONENT+1: // NaN or infinity return Math.abs(f); ! case Float.MIN_EXPONENT-1: // zero or subnormal ! return Float.MIN_VALUE; default: ! assert exp <= Float.MAX_EXPONENT && exp >= Float.MIN_EXPONENT; // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x)) exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1); ! if (exp >= Float.MIN_EXPONENT) { return powerOfTwoF(exp); ! } else { // return a subnormal result; left shift integer // representation of FloatConsts.MIN_VALUE appropriate // number of positions return Float.intBitsToFloat(1 << ! (exp - (Float.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) )); } } } /**
*** 2274,2284 **** // magnitude of a power of two so large that scaling a finite // nonzero value by it would be guaranteed to over or // underflow; due to rounding, scaling down takes an // additional power of two which is reflected here ! final int MAX_SCALE = DoubleConsts.MAX_EXPONENT + -DoubleConsts.MIN_EXPONENT + DoubleConsts.SIGNIFICAND_WIDTH + 1; int exp_adjust = 0; int scale_increment = 0; double exp_delta = Double.NaN; --- 2273,2283 ---- // magnitude of a power of two so large that scaling a finite // nonzero value by it would be guaranteed to over or // underflow; due to rounding, scaling down takes an // additional power of two which is reflected here ! final int MAX_SCALE = Double.MAX_EXPONENT + -Double.MIN_EXPONENT + DoubleConsts.SIGNIFICAND_WIDTH + 1; int exp_adjust = 0; int scale_increment = 0; double exp_delta = Double.NaN;
*** 2343,2353 **** public static float scalb(float f, int scaleFactor) { // magnitude of a power of two so large that scaling a finite // nonzero value by it would be guaranteed to over or // underflow; due to rounding, scaling down takes an // additional power of two which is reflected here ! final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT + FloatConsts.SIGNIFICAND_WIDTH + 1; // Make sure scaling factor is in a reasonable range scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE); --- 2342,2352 ---- public static float scalb(float f, int scaleFactor) { // magnitude of a power of two so large that scaling a finite // nonzero value by it would be guaranteed to over or // underflow; due to rounding, scaling down takes an // additional power of two which is reflected here ! final int MAX_SCALE = Float.MAX_EXPONENT + -Float.MIN_EXPONENT + FloatConsts.SIGNIFICAND_WIDTH + 1; // Make sure scaling factor is in a reasonable range scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);
*** 2369,2389 **** /** * Returns a floating-point power of two in the normal range. */ static double powerOfTwoD(int n) { ! assert(n >= DoubleConsts.MIN_EXPONENT && n <= DoubleConsts.MAX_EXPONENT); return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) << (DoubleConsts.SIGNIFICAND_WIDTH-1)) & DoubleConsts.EXP_BIT_MASK); } /** * Returns a floating-point power of two in the normal range. */ static float powerOfTwoF(int n) { ! assert(n >= FloatConsts.MIN_EXPONENT && n <= FloatConsts.MAX_EXPONENT); return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) << (FloatConsts.SIGNIFICAND_WIDTH-1)) & FloatConsts.EXP_BIT_MASK); } } --- 2368,2388 ---- /** * Returns a floating-point power of two in the normal range. */ static double powerOfTwoD(int n) { ! assert(n >= Double.MIN_EXPONENT && n <= Double.MAX_EXPONENT); return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) << (DoubleConsts.SIGNIFICAND_WIDTH-1)) & DoubleConsts.EXP_BIT_MASK); } /** * Returns a floating-point power of two in the normal range. */ static float powerOfTwoF(int n) { ! assert(n >= Float.MIN_EXPONENT && n <= Float.MAX_EXPONENT); return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) << (FloatConsts.SIGNIFICAND_WIDTH-1)) & FloatConsts.EXP_BIT_MASK); } }
< prev index next >