Coverage Summary for Class: DoubleUtils (com.google.common.math)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| DoubleUtils | 0% (0/1) | 0% (0/8) | 0% (0/30) |
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 @ElementTypesAreNonnullByDefault 37 final class DoubleUtils { 38 private DoubleUtils() {} 39 40 static double nextDown(double d) { 41 return -Math.nextUp(-d); 42 } 43 44 // The mask for the significand, according to the {@link 45 // Double#doubleToRawLongBits(double)} spec. 46 static final long SIGNIFICAND_MASK = 0x000fffffffffffffL; 47 48 // The mask for the exponent, according to the {@link 49 // Double#doubleToRawLongBits(double)} spec. 50 static final long EXPONENT_MASK = 0x7ff0000000000000L; 51 52 // The mask for the sign, according to the {@link 53 // Double#doubleToRawLongBits(double)} spec. 54 static final long SIGN_MASK = 0x8000000000000000L; 55 56 static final int SIGNIFICAND_BITS = 52; 57 58 static final int EXPONENT_BIAS = 1023; 59 60 /** The implicit 1 bit that is omitted in significands of normal doubles. */ 61 static final long IMPLICIT_BIT = SIGNIFICAND_MASK + 1; 62 63 static long getSignificand(double d) { 64 checkArgument(isFinite(d), "not a normal value"); 65 int exponent = getExponent(d); 66 long bits = doubleToRawLongBits(d); 67 bits &= SIGNIFICAND_MASK; 68 return (exponent == MIN_EXPONENT - 1) ? bits << 1 : bits | IMPLICIT_BIT; 69 } 70 71 static boolean isFinite(double d) { 72 return getExponent(d) <= MAX_EXPONENT; 73 } 74 75 static boolean isNormal(double d) { 76 return getExponent(d) >= MIN_EXPONENT; 77 } 78 79 /* 80 * Returns x scaled by a power of 2 such that it is in the range [1, 2). Assumes x is positive, 81 * normal, and finite. 82 */ 83 static double scaleNormalize(double x) { 84 long significand = doubleToRawLongBits(x) & SIGNIFICAND_MASK; 85 return longBitsToDouble(significand | ONE_BITS); 86 } 87 88 static double bigToDouble(BigInteger x) { 89 // This is an extremely fast implementation of BigInteger.doubleValue(). JDK patch pending. 90 BigInteger absX = x.abs(); 91 int exponent = absX.bitLength() - 1; 92 // exponent == floor(log2(abs(x))) 93 if (exponent < Long.SIZE - 1) { 94 return x.longValue(); 95 } else if (exponent > MAX_EXPONENT) { 96 return x.signum() * POSITIVE_INFINITY; 97 } 98 99 /* 100 * We need the top SIGNIFICAND_BITS + 1 bits, including the "implicit" one bit. To make rounding 101 * easier, we pick out the top SIGNIFICAND_BITS + 2 bits, so we have one to help us round up or 102 * down. twiceSignifFloor will contain the top SIGNIFICAND_BITS + 2 bits, and signifFloor the 103 * top SIGNIFICAND_BITS + 1. 104 * 105 * It helps to consider the real number signif = absX * 2^(SIGNIFICAND_BITS - exponent). 106 */ 107 int shift = exponent - SIGNIFICAND_BITS - 1; 108 long twiceSignifFloor = absX.shiftRight(shift).longValue(); 109 long signifFloor = twiceSignifFloor >> 1; 110 signifFloor &= SIGNIFICAND_MASK; // remove the implied bit 111 112 /* 113 * We round up if either the fractional part of signif is strictly greater than 0.5 (which is 114 * true if the 0.5 bit is set and any lower bit is set), or if the fractional part of signif is 115 * >= 0.5 and signifFloor is odd (which is true if both the 0.5 bit and the 1 bit are set). 116 */ 117 boolean increment = 118 (twiceSignifFloor & 1) != 0 && ((signifFloor & 1) != 0 || absX.getLowestSetBit() < shift); 119 long signifRounded = increment ? signifFloor + 1 : signifFloor; 120 long bits = (long) (exponent + EXPONENT_BIAS) << SIGNIFICAND_BITS; 121 bits += signifRounded; 122 /* 123 * If signifRounded == 2^53, we'd need to set all of the significand bits to zero and add 1 to 124 * the exponent. This is exactly the behavior we get from just adding signifRounded to bits 125 * directly. If the exponent is MAX_DOUBLE_EXPONENT, we round up (correctly) to 126 * Double.POSITIVE_INFINITY. 127 */ 128 bits |= x.signum() & SIGN_MASK; 129 return longBitsToDouble(bits); 130 } 131 132 /** Returns its argument if it is non-negative, zero if it is negative. */ 133 static double ensureNonNegative(double value) { 134 checkArgument(!isNaN(value)); 135 return Math.max(value, 0.0); 136 } 137 138 @VisibleForTesting static final long ONE_BITS = 0x3ff0000000000000L; 139 }