1 /*
2  * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package jdk.internal.math;
27 
28 import static java.lang.Double.MIN_EXPONENT;
29 import static java.lang.Double.PRECISION;
30 import static java.lang.Double.SIZE;
31 
32 /**
33  * This class contains additional constants documenting limits of the
34  * {@code double} type.
35  *
36  * @author Joseph D. Darcy
37  */
38 
39 public class DoubleConsts {
40     /**
41      * Don't let anyone instantiate this class.
42      */
DoubleConsts()43     private DoubleConsts() {}
44 
45     public static final double POSITIVE_INFINITY = java.lang.Double.POSITIVE_INFINITY;
46     public static final double NEGATIVE_INFINITY = java.lang.Double.NEGATIVE_INFINITY;
47     public static final double NaN = java.lang.Double.NaN;
48     public static final double MAX_VALUE = java.lang.Double.MAX_VALUE;
49     public static final double MIN_VALUE = java.lang.Double.MIN_VALUE;
50 
51     /**
52      * A constant holding the smallest positive normal value of type
53      * <code>double</code>, 2<sup>-1022</sup>.  It is equal to the
54      * value returned by
55      * <code>Double.longBitsToDouble(0x0010000000000000L)</code>.
56      *
57      * @since 1.5
58      */
59     public static final double  MIN_NORMAL      = 2.2250738585072014E-308;
60 
61 
62     /**
63      * The number of logical bits in the significand of a
64      * {@code double} number, including the implicit bit.
65      */
66     public static final int SIGNIFICAND_WIDTH = PRECISION;
67 
68     /**
69      * Maximum exponent a finite <code>double</code> number may have.
70      * It is equal to the value returned by
71      * <code>Math.ilogb(Double.MAX_VALUE)</code>.
72      */
73     public static final int     MAX_EXPONENT    = 1023;
74 
75     /**
76      * Minimum exponent a normalized <code>double</code> number may
77      * have.  It is equal to the value returned by
78      * <code>Math.ilogb(Double.MIN_NORMAL)</code>.
79      */
80     public static final int     MIN_EXPONENT    = -1022;
81 
82     /**
83      * The exponent the smallest positive {@code double}
84      * subnormal value would have if it could be normalized..
85      */
86     public static final int MIN_SUB_EXPONENT =
87             MIN_EXPONENT - (SIGNIFICAND_WIDTH - 1); // -1074
88 
89     /**
90      * Bias used in representing a {@code double} exponent.
91      */
92     public static final int EXP_BIAS =
93             (1 << (SIZE - SIGNIFICAND_WIDTH - 1)) - 1; // 1023
94 
95     /**
96      * Bit mask to isolate the sign bit of a {@code double}.
97      */
98     public static final long SIGN_BIT_MASK = 1L << (SIZE - 1);
99 
100     /**
101      * Bit mask to isolate the exponent field of a {@code double}.
102      */
103     public static final long EXP_BIT_MASK =
104             ((1L << (SIZE - SIGNIFICAND_WIDTH)) - 1) << (SIGNIFICAND_WIDTH - 1);
105 
106     /**
107      * Bit mask to isolate the significand field of a {@code double}.
108      */
109     public static final long SIGNIF_BIT_MASK = (1L << (SIGNIFICAND_WIDTH - 1)) - 1;
110 
111     /**
112      * Bit mask to isolate the magnitude bits (combined exponent and
113      * significand fields) of a {@code double}.
114      */
115     public static final long MAG_BIT_MASK = EXP_BIT_MASK | SIGNIF_BIT_MASK;
116 
117     static {
118         // verify bit masks cover all bit positions and that the bit
119         // masks are non-overlapping
120         assert(((SIGN_BIT_MASK | EXP_BIT_MASK | SIGNIF_BIT_MASK) == ~0L) &&
121                (((SIGN_BIT_MASK & EXP_BIT_MASK) == 0L) &&
122                 ((SIGN_BIT_MASK & SIGNIF_BIT_MASK) == 0L) &&
123                 ((EXP_BIT_MASK & SIGNIF_BIT_MASK) == 0L)) &&
124                 ((SIGN_BIT_MASK | MAG_BIT_MASK) == ~0L));
125     }
126 }
127