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 */
017package org.apache.commons.math4.util;
018
019import org.apache.commons.numbers.arrays.LinearCombination;
020import org.apache.commons.math4.Field;
021import org.apache.commons.math4.RealFieldElement;
022import org.apache.commons.math4.exception.DimensionMismatchException;
023
024/**
025 * This class wraps a {@code double} value in an object. It is similar to the
026 * standard class {@link Double}, while also implementing the
027 * {@link RealFieldElement} interface.
028 *
029 * @since 3.1
030 */
031public class Decimal64 extends Number
032                       implements RealFieldElement<Decimal64>, Comparable<Decimal64> {
033
034    /** The constant value of {@code 0d} as a {@code Decimal64}. */
035    public static final Decimal64 ZERO;
036
037    /** The constant value of {@code 1d} as a {@code Decimal64}. */
038    public static final Decimal64 ONE;
039
040    /**
041     * The constant value of {@link Double#NEGATIVE_INFINITY} as a
042     * {@code Decimal64}.
043     */
044    public static final Decimal64 NEGATIVE_INFINITY;
045
046    /**
047     * The constant value of {@link Double#POSITIVE_INFINITY} as a
048     * {@code Decimal64}.
049     */
050    public static final Decimal64 POSITIVE_INFINITY;
051
052    /** The constant value of {@link Double#NaN} as a {@code Decimal64}. */
053    public static final Decimal64 NAN;
054
055    /** */
056    private static final long serialVersionUID = 20120227L;
057
058    static {
059        ZERO = new Decimal64(0d);
060        ONE = new Decimal64(1d);
061        NEGATIVE_INFINITY = new Decimal64(Double.NEGATIVE_INFINITY);
062        POSITIVE_INFINITY = new Decimal64(Double.POSITIVE_INFINITY);
063        NAN = new Decimal64(Double.NaN);
064    }
065
066    /** The primitive {@code double} value of this object. */
067    private final double value;
068
069    /**
070     * Creates a new instance of this class.
071     *
072     * @param x the primitive {@code double} value of the object to be created
073     */
074    public Decimal64(final double x) {
075        this.value = x;
076    }
077
078    /*
079     * Methods from the FieldElement interface.
080     */
081
082    /** {@inheritDoc} */
083    @Override
084    public Field<Decimal64> getField() {
085        return Decimal64Field.getInstance();
086    }
087
088    /**
089     * {@inheritDoc}
090     *
091     * The current implementation strictly enforces
092     * {@code this.add(a).equals(new Decimal64(this.doubleValue()
093     * + a.doubleValue()))}.
094     */
095    @Override
096    public Decimal64 add(final Decimal64 a) {
097        return new Decimal64(this.value + a.value);
098    }
099
100    /**
101     * {@inheritDoc}
102     *
103     * The current implementation strictly enforces
104     * {@code this.subtract(a).equals(new Decimal64(this.doubleValue()
105     * - a.doubleValue()))}.
106     */
107    @Override
108    public Decimal64 subtract(final Decimal64 a) {
109        return new Decimal64(this.value - a.value);
110    }
111
112    /**
113     * {@inheritDoc}
114     *
115     * The current implementation strictly enforces
116     * {@code this.negate().equals(new Decimal64(-this.doubleValue()))}.
117     */
118    @Override
119    public Decimal64 negate() {
120        return new Decimal64(-this.value);
121    }
122
123    /**
124     * {@inheritDoc}
125     *
126     * The current implementation strictly enforces
127     * {@code this.multiply(a).equals(new Decimal64(this.doubleValue()
128     * * a.doubleValue()))}.
129     */
130    @Override
131    public Decimal64 multiply(final Decimal64 a) {
132        return new Decimal64(this.value * a.value);
133    }
134
135    /**
136     * {@inheritDoc}
137     *
138     * The current implementation strictly enforces
139     * {@code this.multiply(n).equals(new Decimal64(n * this.doubleValue()))}.
140     */
141    @Override
142    public Decimal64 multiply(final int n) {
143        return new Decimal64(n * this.value);
144    }
145
146    /**
147     * {@inheritDoc}
148     *
149     * The current implementation strictly enforces
150     * {@code this.divide(a).equals(new Decimal64(this.doubleValue()
151     * / a.doubleValue()))}.
152     *
153     */
154    @Override
155    public Decimal64 divide(final Decimal64 a) {
156        return new Decimal64(this.value / a.value);
157    }
158
159    /**
160     * {@inheritDoc}
161     *
162     * The current implementation strictly enforces
163     * {@code this.reciprocal().equals(new Decimal64(1.0
164     * / this.doubleValue()))}.
165     */
166    @Override
167    public Decimal64 reciprocal() {
168        return new Decimal64(1.0 / this.value);
169    }
170
171    /*
172     * Methods from the Number abstract class
173     */
174
175    /**
176     * {@inheritDoc}
177     *
178     * The current implementation performs casting to a {@code byte}.
179     */
180    @Override
181    public byte byteValue() {
182        return (byte) value;
183    }
184
185    /**
186     * {@inheritDoc}
187     *
188     * The current implementation performs casting to a {@code short}.
189     */
190    @Override
191    public short shortValue() {
192        return (short) value;
193    }
194
195    /**
196     * {@inheritDoc}
197     *
198     * The current implementation performs casting to a {@code int}.
199     */
200    @Override
201    public int intValue() {
202        return (int) value;
203    }
204
205    /**
206     * {@inheritDoc}
207     *
208     * The current implementation performs casting to a {@code long}.
209     */
210    @Override
211    public long longValue() {
212        return (long) value;
213    }
214
215    /**
216     * {@inheritDoc}
217     *
218     * The current implementation performs casting to a {@code float}.
219     */
220    @Override
221    public float floatValue() {
222        return (float) value;
223    }
224
225    /** {@inheritDoc} */
226    @Override
227    public double doubleValue() {
228        return value;
229    }
230
231    /*
232     * Methods from the Comparable interface.
233     */
234
235    /**
236     * {@inheritDoc}
237     *
238     * The current implementation returns the same value as
239     * <center> {@code new Double(this.doubleValue()).compareTo(new
240     * Double(o.doubleValue()))} </center>
241     *
242     * @see Double#compareTo(Double)
243     */
244    @Override
245    public int compareTo(final Decimal64 o) {
246        return Double.compare(this.value, o.value);
247    }
248
249    /*
250     * Methods from the Object abstract class.
251     */
252
253    /** {@inheritDoc} */
254    @Override
255    public boolean equals(final Object obj) {
256        if (obj instanceof Decimal64) {
257            final Decimal64 that = (Decimal64) obj;
258            return Double.doubleToLongBits(this.value) == Double
259                    .doubleToLongBits(that.value);
260        }
261        return false;
262    }
263
264    /**
265     * {@inheritDoc}
266     *
267     * The current implementation returns the same value as
268     * {@code new Double(this.doubleValue()).hashCode()}
269     *
270     * @see Double#hashCode()
271     */
272    @Override
273    public int hashCode() {
274        long v = Double.doubleToLongBits(value);
275        return (int) (v ^ (v >>> 32));
276    }
277
278    /**
279     * {@inheritDoc}
280     *
281     * The returned {@code String} is equal to
282     * {@code Double.toString(this.doubleValue())}
283     *
284     * @see Double#toString(double)
285     */
286    @Override
287    public String toString() {
288        return Double.toString(value);
289    }
290
291    /*
292     * Methods inspired by the Double class.
293     */
294
295    /**
296     * Returns {@code true} if {@code this} double precision number is infinite
297     * ({@link Double#POSITIVE_INFINITY} or {@link Double#NEGATIVE_INFINITY}).
298     *
299     * @return {@code true} if {@code this} number is infinite
300     */
301    public boolean isInfinite() {
302        return Double.isInfinite(value);
303    }
304
305    /**
306     * Returns {@code true} if {@code this} double precision number is
307     * Not-a-Number ({@code NaN}), false otherwise.
308     *
309     * @return {@code true} if {@code this} is {@code NaN}
310     */
311    public boolean isNaN() {
312        return Double.isNaN(value);
313    }
314
315    /** {@inheritDoc}
316     * @since 3.2
317     */
318    @Override
319    public double getReal() {
320        return value;
321    }
322
323    /** {@inheritDoc}
324     * @since 3.2
325     */
326    @Override
327    public Decimal64 add(final double a) {
328        return new Decimal64(value + a);
329    }
330
331    /** {@inheritDoc}
332     * @since 3.2
333     */
334    @Override
335    public Decimal64 subtract(final double a) {
336        return new Decimal64(value - a);
337    }
338
339    /** {@inheritDoc}
340     * @since 3.2
341     */
342    @Override
343    public Decimal64 multiply(final double a) {
344        return new Decimal64(value * a);
345    }
346
347    /** {@inheritDoc}
348     * @since 3.2
349     */
350    @Override
351    public Decimal64 divide(final double a) {
352        return new Decimal64(value / a);
353    }
354
355    /** {@inheritDoc}
356     * @since 3.2
357     */
358    @Override
359    public Decimal64 remainder(final double a) {
360        return new Decimal64(FastMath.IEEEremainder(value, a));
361    }
362
363    /** {@inheritDoc}
364     * @since 3.2
365     */
366    @Override
367    public Decimal64 remainder(final Decimal64 a) {
368        return new Decimal64(FastMath.IEEEremainder(value, a.value));
369    }
370
371    /** {@inheritDoc}
372     * @since 3.2
373     */
374    @Override
375    public Decimal64 abs() {
376        return new Decimal64(FastMath.abs(value));
377    }
378
379    /** {@inheritDoc}
380     * @since 3.2
381     */
382    @Override
383    public Decimal64 ceil() {
384        return new Decimal64(FastMath.ceil(value));
385    }
386
387    /** {@inheritDoc}
388     * @since 3.2
389     */
390    @Override
391    public Decimal64 floor() {
392        return new Decimal64(FastMath.floor(value));
393    }
394
395    /** {@inheritDoc}
396     * @since 3.2
397     */
398    @Override
399    public Decimal64 rint() {
400        return new Decimal64(FastMath.rint(value));
401    }
402
403    /** {@inheritDoc}
404     * @since 3.2
405     */
406    @Override
407    public long round() {
408        return FastMath.round(value);
409    }
410
411    /** {@inheritDoc}
412     * @since 3.2
413     */
414    @Override
415    public Decimal64 signum() {
416        return new Decimal64(FastMath.signum(value));
417    }
418
419    /** {@inheritDoc}
420     * @since 3.2
421     */
422    @Override
423    public Decimal64 copySign(final Decimal64 sign) {
424        return new Decimal64(FastMath.copySign(value, sign.value));
425    }
426
427    /** {@inheritDoc}
428     * @since 3.2
429     */
430    @Override
431    public Decimal64 copySign(final double sign) {
432        return new Decimal64(FastMath.copySign(value, sign));
433    }
434
435    /** {@inheritDoc}
436     * @since 3.2
437     */
438    @Override
439    public Decimal64 scalb(final int n) {
440        return new Decimal64(FastMath.scalb(value, n));
441    }
442
443    /** {@inheritDoc}
444     * @since 3.2
445     */
446    @Override
447    public Decimal64 hypot(final Decimal64 y) {
448        return new Decimal64(FastMath.hypot(value, y.value));
449    }
450
451    /** {@inheritDoc}
452     * @since 3.2
453     */
454    @Override
455    public Decimal64 sqrt() {
456        return new Decimal64(FastMath.sqrt(value));
457    }
458
459    /** {@inheritDoc}
460     * @since 3.2
461     */
462    @Override
463    public Decimal64 cbrt() {
464        return new Decimal64(FastMath.cbrt(value));
465    }
466
467    /** {@inheritDoc}
468     * @since 3.2
469     */
470    @Override
471    public Decimal64 rootN(final int n) {
472        if (value < 0) {
473            return new Decimal64(-FastMath.pow(-value, 1.0 / n));
474        } else {
475            return new Decimal64(FastMath.pow(value, 1.0 / n));
476        }
477    }
478
479    /** {@inheritDoc}
480     * @since 3.2
481     */
482    @Override
483    public Decimal64 pow(final double p) {
484        return new Decimal64(FastMath.pow(value, p));
485    }
486
487    /** {@inheritDoc}
488     * @since 3.2
489     */
490    @Override
491    public Decimal64 pow(final int n) {
492        return new Decimal64(FastMath.pow(value, n));
493    }
494
495    /** {@inheritDoc}
496     * @since 3.2
497     */
498    @Override
499    public Decimal64 pow(final Decimal64 e) {
500        return new Decimal64(FastMath.pow(value, e.value));
501    }
502
503    /** {@inheritDoc}
504     * @since 3.2
505     */
506    @Override
507    public Decimal64 exp() {
508        return new Decimal64(FastMath.exp(value));
509    }
510
511    /** {@inheritDoc}
512     * @since 3.2
513     */
514    @Override
515    public Decimal64 expm1() {
516        return new Decimal64(FastMath.expm1(value));
517    }
518
519    /** {@inheritDoc}
520     * @since 3.2
521     */
522    @Override
523    public Decimal64 log() {
524        return new Decimal64(FastMath.log(value));
525    }
526
527    /** {@inheritDoc}
528     * @since 3.2
529     */
530    @Override
531    public Decimal64 log1p() {
532        return new Decimal64(FastMath.log1p(value));
533    }
534
535    /** Base 10 logarithm.
536     * @return base 10 logarithm of the instance
537     * @since 3.2
538     */
539    @Override
540    public Decimal64 log10() {
541        return new Decimal64(FastMath.log10(value));
542    }
543
544    /** {@inheritDoc}
545     * @since 3.2
546     */
547    @Override
548    public Decimal64 cos() {
549        return new Decimal64(FastMath.cos(value));
550    }
551
552    /** {@inheritDoc}
553     * @since 3.2
554     */
555    @Override
556    public Decimal64 sin() {
557        return new Decimal64(FastMath.sin(value));
558    }
559
560    /** {@inheritDoc}
561     * @since 3.2
562     */
563    @Override
564    public Decimal64 tan() {
565        return new Decimal64(FastMath.tan(value));
566    }
567
568    /** {@inheritDoc}
569     * @since 3.2
570     */
571    @Override
572    public Decimal64 acos() {
573        return new Decimal64(FastMath.acos(value));
574    }
575
576    /** {@inheritDoc}
577     * @since 3.2
578     */
579    @Override
580    public Decimal64 asin() {
581        return new Decimal64(FastMath.asin(value));
582    }
583
584    /** {@inheritDoc}
585     * @since 3.2
586     */
587    @Override
588    public Decimal64 atan() {
589        return new Decimal64(FastMath.atan(value));
590    }
591
592    /** {@inheritDoc}
593     * @since 3.2
594     */
595    @Override
596    public Decimal64 atan2(final Decimal64 x) {
597        return new Decimal64(FastMath.atan2(value, x.value));
598    }
599
600    /** {@inheritDoc}
601     * @since 3.2
602     */
603    @Override
604    public Decimal64 cosh() {
605        return new Decimal64(FastMath.cosh(value));
606    }
607
608    /** {@inheritDoc}
609     * @since 3.2
610     */
611    @Override
612    public Decimal64 sinh() {
613        return new Decimal64(FastMath.sinh(value));
614    }
615
616    /** {@inheritDoc}
617     * @since 3.2
618     */
619    @Override
620    public Decimal64 tanh() {
621        return new Decimal64(FastMath.tanh(value));
622    }
623
624    /** {@inheritDoc}
625     * @since 3.2
626     */
627    @Override
628    public Decimal64 acosh() {
629        return new Decimal64(FastMath.acosh(value));
630    }
631
632    /** {@inheritDoc}
633     * @since 3.2
634     */
635    @Override
636    public Decimal64 asinh() {
637        return new Decimal64(FastMath.asinh(value));
638    }
639
640    /** {@inheritDoc}
641     * @since 3.2
642     */
643    @Override
644    public Decimal64 atanh() {
645        return new Decimal64(FastMath.atanh(value));
646    }
647
648    /** {@inheritDoc}
649     * @since 3.2
650     */
651    @Override
652    public Decimal64 linearCombination(final Decimal64[] a, final Decimal64[] b)
653        throws DimensionMismatchException {
654        if (a.length != b.length) {
655            throw new DimensionMismatchException(a.length, b.length);
656        }
657        final double[] aDouble = new double[a.length];
658        final double[] bDouble = new double[b.length];
659        for (int i = 0; i < a.length; ++i) {
660            aDouble[i] = a[i].value;
661            bDouble[i] = b[i].value;
662        }
663        return new Decimal64(LinearCombination.value(aDouble, bDouble));
664    }
665
666    /** {@inheritDoc}
667     * @since 3.2
668     */
669    @Override
670    public Decimal64 linearCombination(final double[] a, final Decimal64[] b)
671        throws DimensionMismatchException {
672        if (a.length != b.length) {
673            throw new DimensionMismatchException(a.length, b.length);
674        }
675        final double[] bDouble = new double[b.length];
676        for (int i = 0; i < a.length; ++i) {
677            bDouble[i] = b[i].value;
678        }
679        return new Decimal64(LinearCombination.value(a, bDouble));
680    }
681
682    /** {@inheritDoc}
683     * @since 3.2
684     */
685    @Override
686    public Decimal64 linearCombination(final Decimal64 a1, final Decimal64 b1,
687                                       final Decimal64 a2, final Decimal64 b2) {
688        return new Decimal64(LinearCombination.value(a1.value, b1.value,
689                                                          a2.value, b2.value));
690    }
691
692    /** {@inheritDoc}
693     * @since 3.2
694     */
695    @Override
696    public Decimal64 linearCombination(final double a1, final Decimal64 b1,
697                                       final double a2, final Decimal64 b2) {
698        return new Decimal64(LinearCombination.value(a1, b1.value,
699                                                          a2, b2.value));
700    }
701
702    /** {@inheritDoc}
703     * @since 3.2
704     */
705    @Override
706    public Decimal64 linearCombination(final Decimal64 a1, final Decimal64 b1,
707                                       final Decimal64 a2, final Decimal64 b2,
708                                       final Decimal64 a3, final Decimal64 b3) {
709        return new Decimal64(LinearCombination.value(a1.value, b1.value,
710                                                          a2.value, b2.value,
711                                                          a3.value, b3.value));
712    }
713
714    /** {@inheritDoc}
715     * @since 3.2
716     */
717    @Override
718    public Decimal64 linearCombination(final double a1, final Decimal64 b1,
719                                       final double a2, final Decimal64 b2,
720                                       final double a3, final Decimal64 b3) {
721        return new Decimal64(LinearCombination.value(a1, b1.value,
722                                                          a2, b2.value,
723                                                          a3, b3.value));
724    }
725
726    /** {@inheritDoc}
727     * @since 3.2
728     */
729    @Override
730    public Decimal64 linearCombination(final Decimal64 a1, final Decimal64 b1,
731                                       final Decimal64 a2, final Decimal64 b2,
732                                       final Decimal64 a3, final Decimal64 b3,
733                                       final Decimal64 a4, final Decimal64 b4) {
734        return new Decimal64(LinearCombination.value(a1.value, b1.value,
735                                                          a2.value, b2.value,
736                                                          a3.value, b3.value,
737                                                          a4.value, b4.value));
738    }
739
740    /** {@inheritDoc}
741     * @since 3.2
742     */
743    @Override
744    public Decimal64 linearCombination(final double a1, final Decimal64 b1,
745                                       final double a2, final Decimal64 b2,
746                                       final double a3, final Decimal64 b3,
747                                       final double a4, final Decimal64 b4) {
748        return new Decimal64(LinearCombination.value(a1, b1.value,
749                                                          a2, b2.value,
750                                                          a3, b3.value,
751                                                          a4, b4.value));
752    }
753
754}