1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.numbers.field;
18
19 import org.apache.commons.numbers.core.NativeOperators;
20
21
22
23
24
25 public final class FP64 extends Number
26 implements NativeOperators<FP64>,
27 Comparable<FP64> {
28 private static final long serialVersionUID = 1L;
29
30
31 private static final FP64 ZERO = new FP64(0);
32
33 private static final FP64 ONE = new FP64(1);
34
35 private final double value;
36
37
38
39
40 private FP64(double value) {
41 this.value = value;
42 }
43
44
45
46
47
48
49
50 public static FP64 of(double value) {
51 return new FP64(value);
52 }
53
54
55 @Override
56 public FP64 add(FP64 a) {
57 return new FP64(value + a.value);
58 }
59
60
61 @Override
62 public FP64 negate() {
63 return new FP64(-value);
64 }
65
66
67 @Override
68 public FP64 multiply(FP64 a) {
69 return new FP64(value * a.value);
70 }
71
72
73 @Override
74 public FP64 reciprocal() {
75 return new FP64(1 / value);
76 }
77
78
79 @Override
80 public FP64 subtract(FP64 a) {
81 return new FP64(value - a.value);
82 }
83
84
85 @Override
86 public FP64 divide(FP64 a) {
87 return new FP64(value / a.value);
88 }
89
90
91 @Override
92 public FP64 multiply(int n) {
93 return new FP64(value * n);
94 }
95
96
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
107 @Override
108 public boolean equals(Object other) {
109 if (other instanceof FP64) {
110 final FP64 o = (FP64) other;
111
112 return Double.doubleToLongBits(value + 0.0) == Double.doubleToLongBits(o.value + 0.0);
113 }
114 return false;
115 }
116
117
118 @Override
119 public int hashCode() {
120
121 return Double.hashCode(value + 0.0);
122 }
123
124
125 @Override
126 public String toString() {
127 return Double.toString(value);
128 }
129
130
131 @Override
132 public double doubleValue() {
133 return value;
134 }
135
136 @Override
137 public float floatValue() {
138 return (float) value;
139 }
140
141 @Override
142 public int intValue() {
143 return (int) value;
144 }
145
146 @Override
147 public long longValue() {
148 return (long) value;
149 }
150
151 @Override
152 public byte byteValue() {
153 return (byte) value;
154 }
155
156
157 @Override
158 public int compareTo(FP64 other) {
159 return Double.compare(value, other.value);
160 }
161
162
163 @Override
164 public FP64 zero() {
165 return ZERO;
166 }
167
168
169 @Override
170 public boolean isZero() {
171 return value == 0.0;
172 }
173
174
175 @Override
176 public FP64 one() {
177 return ONE;
178 }
179
180
181 @Override
182 public boolean isOne() {
183 return value == 1.0;
184 }
185 }