FP64.java

  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. import org.apache.commons.numbers.core.NativeOperators;

  19. /**
  20.  * Wraps a {@code double} value in order to be used as a field
  21.  * element.
  22.  */
  23. public final class FP64 extends Number
  24.     implements NativeOperators<FP64>,
  25.                Comparable<FP64> {
  26.     private static final long serialVersionUID = 1L;

  27.     /** Additive neutral. */
  28.     private static final FP64 ZERO = new FP64(0);
  29.     /** Multiplicative neutral. */
  30.     private static final FP64 ONE = new FP64(1);
  31.     /** Value. */
  32.     private final double value;

  33.     /**
  34.      * @param value Value.
  35.      */
  36.     private FP64(double value) {
  37.         this.value = value;
  38.     }

  39.     /**
  40.      * Factory.
  41.      *
  42.      * @param value Value.
  43.      * @return a new instance.
  44.      */
  45.     public static FP64 of(double value) {
  46.         return new FP64(value);
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public FP64 add(FP64 a) {
  51.         return new FP64(value + a.value);
  52.     }

  53.     /** {@inheritDoc} */
  54.     @Override
  55.     public FP64 negate() {
  56.         return new FP64(-value);
  57.     }

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     public FP64 multiply(FP64 a) {
  61.         return new FP64(value * a.value);
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     public FP64 reciprocal() {
  66.         return new FP64(1 / value);
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public FP64 subtract(FP64 a) {
  71.         return new FP64(value - a.value);
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public FP64 divide(FP64 a) {
  76.         return new FP64(value / a.value);
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public FP64 multiply(int n) {
  81.         return new FP64(value * n);
  82.     }

  83.     /** {@inheritDoc} */
  84.     @Override
  85.     public FP64 pow(int n) {
  86.         if (n == 0) {
  87.             return ONE;
  88.         }

  89.         return new FP64(Math.pow(value, n));
  90.     }

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     public boolean equals(Object other) {
  94.         if (other instanceof FP64) {
  95.             final FP64 o = (FP64) other;
  96.             // Allow -0.0 to equal 0.0
  97.             return Double.doubleToLongBits(value + 0.0) == Double.doubleToLongBits(o.value + 0.0);
  98.         }
  99.         return false;
  100.     }

  101.     /** {@inheritDoc} */
  102.     @Override
  103.     public int hashCode() {
  104.         // Same hash code for -0.0 and 0.0
  105.         return Double.hashCode(value + 0.0);
  106.     }

  107.     /** {@inheritDoc} */
  108.     @Override
  109.     public String toString() {
  110.         return Double.toString(value);
  111.     }

  112.     /** {@inheritDoc} */
  113.     @Override
  114.     public double doubleValue() {
  115.         return value;
  116.     }
  117.     /** {@inheritDoc} */
  118.     @Override
  119.     public float floatValue() {
  120.         return (float) value;
  121.     }
  122.     /** {@inheritDoc} */
  123.     @Override
  124.     public int intValue() {
  125.         return (int) value;
  126.     }
  127.     /** {@inheritDoc} */
  128.     @Override
  129.     public long longValue() {
  130.         return (long) value;
  131.     }
  132.     /** {@inheritDoc} */
  133.     @Override
  134.     public byte byteValue() {
  135.         return (byte) value;
  136.     }

  137.     /** {@inheritDoc} */
  138.     @Override
  139.     public int compareTo(FP64 other) {
  140.         return Double.compare(value, other.value);
  141.     }

  142.     /** {@inheritDoc} */
  143.     @Override
  144.     public FP64 zero() {
  145.         return ZERO;
  146.     }

  147.     /** {@inheritDoc} */
  148.     @Override
  149.     public boolean isZero() {
  150.         return value == 0.0;
  151.     }

  152.     /** {@inheritDoc} */
  153.     @Override
  154.     public FP64 one() {
  155.         return ONE;
  156.     }

  157.     /** {@inheritDoc} */
  158.     @Override
  159.     public boolean isOne() {
  160.         return value == 1.0;
  161.     }
  162. }