NUMBERS-51: Class "FP64" ("double" with "Field" API).


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/de85ffc2
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/de85ffc2
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/de85ffc2

Branch: refs/heads/feature__NUMBERS-51__field
Commit: de85ffc2843e43e0c0e426151a995d0c7dc77673
Parents: 6d14b16
Author: Gilles Sadowski <gil...@harfang.homelinux.org>
Authored: Tue Jan 30 16:49:41 2018 +0100
Committer: Gilles Sadowski <gil...@harfang.homelinux.org>
Committed: Tue Jan 30 16:49:41 2018 +0100

----------------------------------------------------------------------
 .../org/apache/commons/numbers/field/FP64.java  | 127 +++++++++++++++++++
 .../apache/commons/numbers/field/FP64Field.java |  39 ++++++
 2 files changed, 166 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/de85ffc2/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FP64.java
----------------------------------------------------------------------
diff --git 
a/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FP64.java
 
b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FP64.java
new file mode 100644
index 0000000..1bb2097
--- /dev/null
+++ 
b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FP64.java
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.numbers.field;
+
+import org.apache.commons.numbers.core.NativeOperators;
+import org.apache.commons.numbers.core.Precision;
+
+/**
+ * Wraps a {@code double} value in order to be used as a field
+ * element.
+ */
+public class FP64 extends Number
+    implements NativeOperators<FP64> {
+    /** Value. */
+    private final double value;
+
+    /**
+     * @param Value value.
+     */
+    public FP64(double value) {
+        this.value = value;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public FP64 add(FP64 a) {
+        return new FP64(value + a.value);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public FP64 negate() {
+        return new FP64(-value);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public FP64 multiply(FP64 a) {
+        return new FP64(value * a.value);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public FP64 reciprocal() {
+        return new FP64(1 / value);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public FP64 subtract(FP64 a) {
+        return new FP64(value - a.value);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public FP64 divide(FP64 a) {
+        return new FP64(value / a.value);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public FP64 multiply(int n) {
+        return new FP64(value * n);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean equals(Object other) {
+        if (other instanceof FP64) {
+            final FP64 o = (FP64) other;
+            return Precision.equals(value, o.value, 1);
+        }
+        return false;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public int hashCode() {
+        return Double.valueOf(value).hashCode();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String toString() {
+        return Double.toString(value);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public double doubleValue() {
+        return value;
+    }
+    /** {@inheritDoc} */
+    @Override
+    public float floatValue() {
+        return (float) value;
+    }
+    /** {@inheritDoc} */
+    @Override
+    public int intValue() {
+        return (int) value;
+    }
+    /** {@inheritDoc} */
+    @Override
+    public long longValue() {
+        return (long) value;
+    }
+    /** {@inheritDoc} */
+    @Override
+    public byte byteValue() {
+        return (byte) value;
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/de85ffc2/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FP64Field.java
----------------------------------------------------------------------
diff --git 
a/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FP64Field.java
 
b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FP64Field.java
new file mode 100644
index 0000000..f63665c
--- /dev/null
+++ 
b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FP64Field.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.numbers.field;
+
+/**
+ * {@link Double} field.
+ */
+public class FP64Field extends AbstractField<FP64> {
+    /** 0d */
+    private static final FP64 ZERO = new FP64(Double.valueOf(0));
+    /** 1d */
+    private static final FP64 ONE = new FP64(Double.valueOf(1));
+
+    /** {@inheritDoc} */
+    @Override
+    public FP64 one() {
+        return ONE;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public FP64 zero() {
+        return ZERO;
+    }
+}

Reply via email to