001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.numbers.field; 018 019import org.apache.commons.numbers.core.NativeOperators; 020import org.apache.commons.numbers.core.Precision; 021 022/** 023 * Wraps a {@code double} value in order to be used as a field 024 * element. 025 */ 026public final class FP64 extends Number 027 implements NativeOperators<FP64>, 028 Comparable<FP64> { 029 private static final long serialVersionUID = 1L; 030 031 /** Additive neutral. */ 032 private static final FP64 ZERO = new FP64(0); 033 /** Multiplicative neutral. */ 034 private static final FP64 ONE = new FP64(1); 035 /** Value. */ 036 private final double value; 037 038 /** 039 * @param value Value. 040 */ 041 private FP64(double value) { 042 this.value = value; 043 } 044 045 /** 046 * Factory. 047 * 048 * @param value Value. 049 * @return a new instance. 050 */ 051 public static FP64 of(double value) { 052 return new FP64(value); 053 } 054 055 /** {@inheritDoc} */ 056 @Override 057 public FP64 add(FP64 a) { 058 return new FP64(value + a.value); 059 } 060 061 /** {@inheritDoc} */ 062 @Override 063 public FP64 negate() { 064 return new FP64(-value); 065 } 066 067 /** {@inheritDoc} */ 068 @Override 069 public FP64 multiply(FP64 a) { 070 return new FP64(value * a.value); 071 } 072 073 /** {@inheritDoc} */ 074 @Override 075 public FP64 reciprocal() { 076 return new FP64(1 / value); 077 } 078 079 /** {@inheritDoc} */ 080 @Override 081 public FP64 subtract(FP64 a) { 082 return new FP64(value - a.value); 083 } 084 085 /** {@inheritDoc} */ 086 @Override 087 public FP64 divide(FP64 a) { 088 return new FP64(value / a.value); 089 } 090 091 /** {@inheritDoc} */ 092 @Override 093 public FP64 multiply(int n) { 094 return new FP64(value * n); 095 } 096 097 /** {@inheritDoc} */ 098 @Override 099 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}