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.numbers.field;
018
019import org.apache.commons.numbers.core.NativeOperators;
020
021/**
022 * Wraps a {@code double} value in order to be used as a field
023 * element.
024 */
025public final class FP64 extends Number
026    implements NativeOperators<FP64>,
027               Comparable<FP64> {
028    private static final long serialVersionUID = 1L;
029
030    /** Additive neutral. */
031    private static final FP64 ZERO = new FP64(0);
032    /** Multiplicative neutral. */
033    private static final FP64 ONE = new FP64(1);
034    /** Value. */
035    private final double value;
036
037    /**
038     * @param value Value.
039     */
040    private FP64(double value) {
041        this.value = value;
042    }
043
044    /**
045     * Factory.
046     *
047     * @param value Value.
048     * @return a new instance.
049     */
050    public static FP64 of(double value) {
051        return new FP64(value);
052    }
053
054    /** {@inheritDoc} */
055    @Override
056    public FP64 add(FP64 a) {
057        return new FP64(value + a.value);
058    }
059
060    /** {@inheritDoc} */
061    @Override
062    public FP64 negate() {
063        return new FP64(-value);
064    }
065
066    /** {@inheritDoc} */
067    @Override
068    public FP64 multiply(FP64 a) {
069        return new FP64(value * a.value);
070    }
071
072    /** {@inheritDoc} */
073    @Override
074    public FP64 reciprocal() {
075        return new FP64(1 / value);
076    }
077
078    /** {@inheritDoc} */
079    @Override
080    public FP64 subtract(FP64 a) {
081        return new FP64(value - a.value);
082    }
083
084    /** {@inheritDoc} */
085    @Override
086    public FP64 divide(FP64 a) {
087        return new FP64(value / a.value);
088    }
089
090    /** {@inheritDoc} */
091    @Override
092    public FP64 multiply(int n) {
093        return new FP64(value * n);
094    }
095
096    /** {@inheritDoc} */
097    @Override
098    public FP64 pow(int n) {
099        if (n == 0) {
100            return ONE;
101        }
102
103        return new FP64(Math.pow(value, n));
104    }
105
106    /** {@inheritDoc} */
107    @Override
108    public boolean equals(Object other) {
109        if (other instanceof FP64) {
110            final FP64 o = (FP64) other;
111            // Allow -0.0 to equal 0.0
112            return Double.doubleToLongBits(value + 0.0) == Double.doubleToLongBits(o.value + 0.0);
113        }
114        return false;
115    }
116
117    /** {@inheritDoc} */
118    @Override
119    public int hashCode() {
120        // Same hash code for -0.0 and 0.0
121        return Double.hashCode(value + 0.0);
122    }
123
124    /** {@inheritDoc} */
125    @Override
126    public String toString() {
127        return Double.toString(value);
128    }
129
130    /** {@inheritDoc} */
131    @Override
132    public double doubleValue() {
133        return value;
134    }
135    /** {@inheritDoc} */
136    @Override
137    public float floatValue() {
138        return (float) value;
139    }
140    /** {@inheritDoc} */
141    @Override
142    public int intValue() {
143        return (int) value;
144    }
145    /** {@inheritDoc} */
146    @Override
147    public long longValue() {
148        return (long) value;
149    }
150    /** {@inheritDoc} */
151    @Override
152    public byte byteValue() {
153        return (byte) value;
154    }
155
156    /** {@inheritDoc} */
157    @Override
158    public int compareTo(FP64 other) {
159        return Double.compare(value, other.value);
160    }
161
162    /** {@inheritDoc} */
163    @Override
164    public FP64 zero() {
165        return ZERO;
166    }
167
168    /** {@inheritDoc} */
169    @Override
170    public boolean isZero() {
171        return value == 0.0;
172    }
173
174    /** {@inheritDoc} */
175    @Override
176    public FP64 one() {
177        return ONE;
178    }
179
180    /** {@inheritDoc} */
181    @Override
182    public boolean isOne() {
183        return value == 1.0;
184    }
185}