001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math3.util;
018    
019    import org.apache.commons.math3.Field;
020    import org.apache.commons.math3.FieldElement;
021    
022    /**
023     * This class wraps a {@code double} value in an object. It is similar to the
024     * standard class {@link Double}, while also implementing the
025     * {@link FieldElement} interface.
026     *
027     * @since 3.1
028     * @version $Id: Decimal64.java 1306177 2012-03-28 05:40:46Z celestin $
029     */
030    public class Decimal64 extends Number implements FieldElement<Decimal64>,
031    Comparable<Decimal64> {
032    
033        /** The constant value of {@code 0d} as a {@code Decimal64}. */
034        public static final Decimal64 ZERO;
035    
036        /** The constant value of {@code 1d} as a {@code Decimal64}. */
037        public static final Decimal64 ONE;
038    
039        /**
040         * The constant value of {@link Double#NEGATIVE_INFINITY} as a
041         * {@code Decimal64}.
042         */
043        public static final Decimal64 NEGATIVE_INFINITY;
044    
045        /**
046         * The constant value of {@link Double#POSITIVE_INFINITY} as a
047         * {@code Decimal64}.
048         */
049        public static final Decimal64 POSITIVE_INFINITY;
050    
051        /** The constant value of {@link Double#NaN} as a {@code Decimal64}. */
052        public static final Decimal64 NAN;
053    
054        /** */
055        private static final long serialVersionUID = 20120227L;
056    
057        static {
058            ZERO = new Decimal64(0d);
059            ONE = new Decimal64(1d);
060            NEGATIVE_INFINITY = new Decimal64(Double.NEGATIVE_INFINITY);
061            POSITIVE_INFINITY = new Decimal64(Double.POSITIVE_INFINITY);
062            NAN = new Decimal64(Double.NaN);
063        }
064    
065        /** The primitive {@code double} value of this object. */
066        private final double value;
067    
068        /**
069         * Creates a new instance of this class.
070         *
071         * @param x the primitive {@code double} value of the object to be created
072         */
073        public Decimal64(final double x) {
074            this.value = x;
075        }
076    
077        /*
078         * Methods from the FieldElement interface.
079         */
080    
081        /** {@inheritDoc} */
082        public Field<Decimal64> getField() {
083            return Decimal64Field.getInstance();
084        }
085    
086        /**
087         * {@inheritDoc}
088         *
089         * The current implementation strictly enforces
090         * {@code this.add(a).equals(new Decimal64(this.doubleValue()
091         * + a.doubleValue()))}.
092         */
093        public Decimal64 add(final Decimal64 a) {
094            return new Decimal64(this.value + a.value);
095        }
096    
097        /**
098         * {@inheritDoc}
099         *
100         * The current implementation strictly enforces
101         * {@code this.subtract(a).equals(new Decimal64(this.doubleValue()
102         * - a.doubleValue()))}.
103         */
104        public Decimal64 subtract(final Decimal64 a) {
105            return new Decimal64(this.value - a.value);
106        }
107    
108        /**
109         * {@inheritDoc}
110         *
111         * The current implementation strictly enforces
112         * {@code this.negate().equals(new Decimal64(-this.doubleValue()))}.
113         */
114        public Decimal64 negate() {
115            return new Decimal64(-this.value);
116        }
117    
118        /**
119         * {@inheritDoc}
120         *
121         * The current implementation strictly enforces
122         * {@code this.multiply(a).equals(new Decimal64(this.doubleValue()
123         * * a.doubleValue()))}.
124         */
125        public Decimal64 multiply(final Decimal64 a) {
126            return new Decimal64(this.value * a.value);
127        }
128    
129        /**
130         * {@inheritDoc}
131         *
132         * The current implementation strictly enforces
133         * {@code this.multiply(n).equals(new Decimal64(n * this.doubleValue()))}.
134         */
135        public Decimal64 multiply(final int n) {
136            return new Decimal64(n * this.value);
137        }
138    
139        /**
140         * {@inheritDoc}
141         *
142         * The current implementation strictly enforces
143         * {@code this.divide(a).equals(new Decimal64(this.doubleValue()
144         * / a.doubleValue()))}.
145         *
146         */
147        public Decimal64 divide(final Decimal64 a) {
148            return new Decimal64(this.value / a.value);
149        }
150    
151        /**
152         * {@inheritDoc}
153         *
154         * The current implementation strictly enforces
155         * {@code this.reciprocal().equals(new Decimal64(1.0
156         * / this.doubleValue()))}.
157         */
158        public Decimal64 reciprocal() {
159            return new Decimal64(1.0 / this.value);
160        }
161    
162        /*
163         * Methods from the Number abstract class
164         */
165    
166        /**
167         * {@inheritDoc}
168         *
169         * The current implementation performs casting to a {@code byte}.
170         */
171        @Override
172        public byte byteValue() {
173            return (byte) value;
174        }
175    
176        /**
177         * {@inheritDoc}
178         *
179         * The current implementation performs casting to a {@code short}.
180         */
181        @Override
182        public short shortValue() {
183            return (short) value;
184        }
185    
186        /**
187         * {@inheritDoc}
188         *
189         * The current implementation performs casting to a {@code int}.
190         */
191        @Override
192        public int intValue() {
193            return (int) value;
194        }
195    
196        /**
197         * {@inheritDoc}
198         *
199         * The current implementation performs casting to a {@code long}.
200         */
201        @Override
202        public long longValue() {
203            return (long) value;
204        }
205    
206        /**
207         * {@inheritDoc}
208         *
209         * The current implementation performs casting to a {@code float}.
210         */
211        @Override
212        public float floatValue() {
213            return (float) value;
214        }
215    
216        /** {@inheritDoc} */
217        @Override
218        public double doubleValue() {
219            return value;
220        }
221    
222        /*
223         * Methods from the Comparable interface.
224         */
225    
226        /**
227         * {@inheritDoc}
228         *
229         * The current implementation returns the same value as
230         * <center> {@code new Double(this.doubleValue()).compareTo(new
231         * Double(o.doubleValue()))} </center>
232         *
233         * @see Double#compareTo(Double)
234         */
235        public int compareTo(final Decimal64 o) {
236            return Double.compare(this.value, o.value);
237        }
238    
239        /*
240         * Methods from the Object abstract class.
241         */
242    
243        /** {@inheritDoc} */
244        @Override
245        public boolean equals(final Object obj) {
246            if (obj instanceof Decimal64) {
247                final Decimal64 that = (Decimal64) obj;
248                return Double.doubleToLongBits(this.value) == Double
249                        .doubleToLongBits(that.value);
250            }
251            return false;
252        }
253    
254        /**
255         * {@inheritDoc}
256         *
257         * The current implementation returns the same value as
258         * {@code new Double(this.doubleValue()).hashCode()}
259         *
260         * @see Double#hashCode()
261         */
262        @Override
263        public int hashCode() {
264            long v = Double.doubleToLongBits(value);
265            return (int) (v ^ (v >>> 32));
266        }
267    
268        /**
269         * {@inheritDoc}
270         *
271         * The returned {@code String} is equal to
272         * {@code Double.toString(this.doubleValue())}
273         *
274         * @see Double#toString(double)
275         */
276        @Override
277        public String toString() {
278            return Double.toString(value);
279        }
280    
281        /*
282         * Methods inspired by the Double class.
283         */
284    
285        /**
286         * Returns {@code true} if {@code this} double precision number is infinite
287         * ({@link Double#POSITIVE_INFINITY} or {@link Double#NEGATIVE_INFINITY}).
288         *
289         * @return {@code true} if {@code this} number is infinite
290         */
291        public boolean isInfinite() {
292            return Double.isInfinite(value);
293        }
294    
295        /**
296         * Returns {@code true} if {@code this} double precision number is
297         * Not-a-Number ({@code NaN}), false otherwise.
298         *
299         * @return {@code true} if {@code this} is {@code NaN}
300         */
301        public boolean isNaN() {
302            return Double.isNaN(value);
303        }
304    }