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  import org.apache.commons.numbers.core.Precision;
21  
22  /**
23   * Wraps a {@code double} value in order to be used as a field
24   * element.
25   */
26  public final class FP64 extends Number
27      implements NativeOperators<FP64>,
28                 Comparable<FP64> {
29      private static final long serialVersionUID = 1L;
30  
31      /** Additive neutral. */
32      private static final FP64 ZERO = new FP64(0);
33      /** Multiplicative neutral. */
34      private static final FP64 ONE = new FP64(1);
35      /** Value. */
36      private final double value;
37  
38      /**
39       * @param value Value.
40       */
41      private FP64(double value) {
42          this.value = value;
43      }
44  
45      /**
46       * Factory.
47       *
48       * @param value Value.
49       * @return a new instance.
50       */
51      public static FP64 of(double value) {
52          return new FP64(value);
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public FP64 add(FP64 a) {
58          return new FP64(value + a.value);
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public FP64 negate() {
64          return new FP64(-value);
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public FP64 multiply(FP64 a) {
70          return new FP64(value * a.value);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public FP64 reciprocal() {
76          return new FP64(1 / value);
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public FP64 subtract(FP64 a) {
82          return new FP64(value - a.value);
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public FP64 divide(FP64 a) {
88          return new FP64(value / a.value);
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public FP64 multiply(int n) {
94          return new FP64(value * n);
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public FP64 pow(int n) {
100         if (n == 0) {
101             return ONE;
102         }
103 
104         return new FP64(Math.pow(value, n));
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public boolean equals(Object other) {
110         if (other instanceof FP64) {
111             final FP64 o = (FP64) other;
112             return Precision.equals(value, o.value, 1);
113         }
114         return false;
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public int hashCode() {
120         return Double.hashCode(value);
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public String toString() {
126         return Double.toString(value);
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public double doubleValue() {
132         return value;
133     }
134     /** {@inheritDoc} */
135     @Override
136     public float floatValue() {
137         return (float) value;
138     }
139     /** {@inheritDoc} */
140     @Override
141     public int intValue() {
142         return (int) value;
143     }
144     /** {@inheritDoc} */
145     @Override
146     public long longValue() {
147         return (long) value;
148     }
149     /** {@inheritDoc} */
150     @Override
151     public byte byteValue() {
152         return (byte) value;
153     }
154 
155     /** {@inheritDoc} */
156     @Override
157     public int compareTo(FP64 other) {
158         return Double.compare(value, other.value);
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public FP64 zero() {
164         return ZERO;
165     }
166 
167     /** {@inheritDoc} */
168     @Override
169     public FP64 one() {
170         return ONE;
171     }
172 }