1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.math; 16 17 import static com.google.common.base.Preconditions.checkArgument; 18 import static java.lang.Double.MAX_EXPONENT; 19 import static java.lang.Double.MIN_EXPONENT; 20 import static java.lang.Double.POSITIVE_INFINITY; 21 import static java.lang.Double.doubleToRawLongBits; 22 import static java.lang.Double.isNaN; 23 import static java.lang.Double.longBitsToDouble; 24 import static java.lang.Math.getExponent; 25 26 import com.google.common.annotations.GwtIncompatible; 27 import com.google.common.annotations.VisibleForTesting; 28 import java.math.BigInteger; 29 30 /** 31 * Utilities for {@code double} primitives. 32 * 33 * @author Louis Wasserman 34 */ 35 @GwtIncompatible 36 final class DoubleUtils { DoubleUtils()37 private DoubleUtils() {} 38 nextDown(double d)39 static double nextDown(double d) { 40 return -Math.nextUp(-d); 41 } 42 43 // The mask for the significand, according to the {@link 44 // Double#doubleToRawLongBits(double)} spec. 45 static final long SIGNIFICAND_MASK = 0x000fffffffffffffL; 46 47 // The mask for the exponent, according to the {@link 48 // Double#doubleToRawLongBits(double)} spec. 49 static final long EXPONENT_MASK = 0x7ff0000000000000L; 50 51 // The mask for the sign, according to the {@link 52 // Double#doubleToRawLongBits(double)} spec. 53 static final long SIGN_MASK = 0x8000000000000000L; 54 55 static final int SIGNIFICAND_BITS = 52; 56 57 static final int EXPONENT_BIAS = 1023; 58 59 /** The implicit 1 bit that is omitted in significands of normal doubles. */ 60 static final long IMPLICIT_BIT = SIGNIFICAND_MASK + 1; 61 getSignificand(double d)62 static long getSignificand(double d) { 63 checkArgument(isFinite(d), "not a normal value"); 64 int exponent = getExponent(d); 65 long bits = doubleToRawLongBits(d); 66 bits &= SIGNIFICAND_MASK; 67 return (exponent == MIN_EXPONENT - 1) ? bits << 1 : bits | IMPLICIT_BIT; 68 } 69 isFinite(double d)70 static boolean isFinite(double d) { 71 return getExponent(d) <= MAX_EXPONENT; 72 } 73 isNormal(double d)74 static boolean isNormal(double d) { 75 return getExponent(d) >= MIN_EXPONENT; 76 } 77 78 /* 79 * Returns x scaled by a power of 2 such that it is in the range [1, 2). Assumes x is positive, 80 * normal, and finite. 81 */ scaleNormalize(double x)82 static double scaleNormalize(double x) { 83 long significand = doubleToRawLongBits(x) & SIGNIFICAND_MASK; 84 return longBitsToDouble(significand | ONE_BITS); 85 } 86 bigToDouble(BigInteger x)87 static double bigToDouble(BigInteger x) { 88 // This is an extremely fast implementation of BigInteger.doubleValue(). JDK patch pending. 89 BigInteger absX = x.abs(); 90 int exponent = absX.bitLength() - 1; 91 // exponent == floor(log2(abs(x))) 92 if (exponent < Long.SIZE - 1) { 93 return x.longValue(); 94 } else if (exponent > MAX_EXPONENT) { 95 return x.signum() * POSITIVE_INFINITY; 96 } 97 98 /* 99 * We need the top SIGNIFICAND_BITS + 1 bits, including the "implicit" one bit. To make rounding 100 * easier, we pick out the top SIGNIFICAND_BITS + 2 bits, so we have one to help us round up or 101 * down. twiceSignifFloor will contain the top SIGNIFICAND_BITS + 2 bits, and signifFloor the 102 * top SIGNIFICAND_BITS + 1. 103 * 104 * It helps to consider the real number signif = absX * 2^(SIGNIFICAND_BITS - exponent). 105 */ 106 int shift = exponent - SIGNIFICAND_BITS - 1; 107 long twiceSignifFloor = absX.shiftRight(shift).longValue(); 108 long signifFloor = twiceSignifFloor >> 1; 109 signifFloor &= SIGNIFICAND_MASK; // remove the implied bit 110 111 /* 112 * We round up if either the fractional part of signif is strictly greater than 0.5 (which is 113 * true if the 0.5 bit is set and any lower bit is set), or if the fractional part of signif is 114 * >= 0.5 and signifFloor is odd (which is true if both the 0.5 bit and the 1 bit are set). 115 */ 116 boolean increment = 117 (twiceSignifFloor & 1) != 0 && ((signifFloor & 1) != 0 || absX.getLowestSetBit() < shift); 118 long signifRounded = increment ? signifFloor + 1 : signifFloor; 119 long bits = (long) (exponent + EXPONENT_BIAS) << SIGNIFICAND_BITS; 120 bits += signifRounded; 121 /* 122 * If signifRounded == 2^53, we'd need to set all of the significand bits to zero and add 1 to 123 * the exponent. This is exactly the behavior we get from just adding signifRounded to bits 124 * directly. If the exponent is MAX_DOUBLE_EXPONENT, we round up (correctly) to 125 * Double.POSITIVE_INFINITY. 126 */ 127 bits |= x.signum() & SIGN_MASK; 128 return longBitsToDouble(bits); 129 } 130 131 /** Returns its argument if it is non-negative, zero if it is negative. */ ensureNonNegative(double value)132 static double ensureNonNegative(double value) { 133 checkArgument(!isNaN(value)); 134 return Math.max(value, 0.0); 135 } 136 137 @VisibleForTesting static final long ONE_BITS = 0x3ff0000000000000L; 138 } 139