View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.numbers.field;
18  
19  import org.apache.commons.numbers.core.NativeOperators;
20  
21  /**
22   * Wraps a {@code double} value in order to be used as a field
23   * element.
24   */
25  public final class FP64 extends Number
26      implements NativeOperators<FP64>,
27                 Comparable<FP64> {
28      private static final long serialVersionUID = 1L;
29  
30      /** Additive neutral. */
31      private static final FP64 ZERO = new FP64(0);
32      /** Multiplicative neutral. */
33      private static final FP64 ONE = new FP64(1);
34      /** Value. */
35      private final double value;
36  
37      /**
38       * @param value Value.
39       */
40      private FP64(double value) {
41          this.value = value;
42      }
43  
44      /**
45       * Factory.
46       *
47       * @param value Value.
48       * @return a new instance.
49       */
50      public static FP64 of(double value) {
51          return new FP64(value);
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public FP64 add(FP64 a) {
57          return new FP64(value + a.value);
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public FP64 negate() {
63          return new FP64(-value);
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public FP64 multiply(FP64 a) {
69          return new FP64(value * a.value);
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public FP64 reciprocal() {
75          return new FP64(1 / value);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public FP64 subtract(FP64 a) {
81          return new FP64(value - a.value);
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public FP64 divide(FP64 a) {
87          return new FP64(value / a.value);
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      public FP64 multiply(int n) {
93          return new FP64(value * n);
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public FP64 pow(int n) {
99          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 }