|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 2026, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package java.lang; |
| 27 | + |
| 28 | +/** |
| 29 | + * Indicates a type supports the basic binary arithmetic operations of |
| 30 | + * addition, subtraction, multiplication, (optionally) division, and |
| 31 | + * (optionally) remainder, ({@code +}, {@code -}, {@code *}, {@code |
| 32 | + * /}, {@code %}, respectively), as well as (optionally) negation (unary |
| 33 | + * {@code -}), and participates in operator overloading of those |
| 34 | + * operators. |
| 35 | + * |
| 36 | + * <p>The intention of this interface is to enable types that |
| 37 | + * customarily support numerical notions of addition, subtraction, |
| 38 | + * multiplication and division to enjoy operator overloading syntax |
| 39 | + * even if the underlying algebraic properties do not hold because of |
| 40 | + * limitations in approximation. This includes <dfn>algebraic |
| 41 | + * fields</dfn> and field-like numbers as well as <dfn>algebraic |
| 42 | + * rings</dfn> and ring-links numbers. |
| 43 | + * |
| 44 | + * <p>For example, mathematical integers form a ring and, including |
| 45 | + * Euclidean division support, integers support the operations in |
| 46 | + * this interface. Fields also support the operations in |
| 47 | + * question. Commonly used fields include rational numbers, real |
| 48 | + * numbers, and complex numbers. A field has a set of values and |
| 49 | + * operations on those values. The operations have various properties |
| 50 | + * known as the <dfn>field axioms</dfn>. These include associativity |
| 51 | + * of addition and multiplication, commutativity of addition and |
| 52 | + * multiplication, and multiplication distributing over |
| 53 | + * addition. Fields can be {@linkplain Orderable ordered} (rational |
| 54 | + * numbers, real numbers) or unordered (complex numbers). |
| 55 | + * |
| 56 | + * <p>Types used to approximate a field, such as a floating-point type |
| 57 | + * used to approximate real numbers, will both approximate the set of |
| 58 | + * values of the field and the set of properties over the supported |
| 59 | + * operations. In particular, properties like associativity of |
| 60 | + * addition are <em>not</em> expected to hold for a floating-point |
| 61 | + * type. |
| 62 | + * |
| 63 | + * @apiNote |
| 64 | + * A particular numerical type may support returning a number of that |
| 65 | + * type for all arguments to add, subtract, multiply, and (possibly) |
| 66 | + * divide. An operation having that property is said to be |
| 67 | + * <dfn>closed</dfn> over that operation. For example, built-in {@code |
| 68 | + * int} arithmetic is closed over add, subtract, and multiply, but is |
| 69 | + * <em>not</em> closed under divide since an {@code |
| 70 | + * ArithmeticException} is thrown on a zero divisor. Built-in |
| 71 | + * arithmetic on {@code float} and {@code double} is closed under all |
| 72 | + * of add, subtract, multiply, and divide, with infinities and NaN |
| 73 | + * (not-a-number) being returned for cases that would otherwise be |
| 74 | + * exceptional. |
| 75 | + * |
| 76 | + * <p>A given numerical type implementing this interface may or may |
| 77 | + * not be closed under any particular operation. If it is <em>not</em> |
| 78 | + * closed, the conditions where an exception is thrown should be |
| 79 | + * documented with the expected outcome being throwing an {@code |
| 80 | + * ArithmeticException} rather than returning a value. |
| 81 | + * |
| 82 | + * <p>Future work: consider interactions with / support from {@link |
| 83 | + * java.util.Formatter} and numerical types. |
| 84 | + * |
| 85 | + * @param <NT> The numerical type |
| 86 | + * @see Orderable |
| 87 | + */ |
| 88 | +public interface Numerical<NT> { |
| 89 | + /** |
| 90 | + * Addition operation, binary operator "{@code +}". |
| 91 | + * |
| 92 | + * @param addend the first operand |
| 93 | + * @param augend the second operand |
| 94 | + * @return the sum of the operands |
| 95 | + * @throws ArithmeticException if the numerical type does not |
| 96 | + * allow adding the operands in question |
| 97 | + */ |
| 98 | + NT add(NT addend, NT augend); |
| 99 | + |
| 100 | + /** |
| 101 | + * Subtraction operation, binary operator "{@code -}". |
| 102 | + * |
| 103 | + * @implSpec |
| 104 | + * The default implementation returns the sum of the first |
| 105 | + * operand with the negation of the second operand. |
| 106 | + * |
| 107 | + * @param minuend the first operand |
| 108 | + * @param subtrahend the second operand |
| 109 | + * @return the difference of the operands |
| 110 | + * @throws ArithmeticException if the numerical type does not |
| 111 | + * allow subtracting the operands in question |
| 112 | + */ |
| 113 | + default NT subtract(NT minuend, NT subtrahend) { |
| 114 | + return this.add(minuend, this.negate(subtrahend)); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Multiplication operation, binary operator "{@code *}". |
| 119 | + * |
| 120 | + * @param multiplier the first operand |
| 121 | + * @param multiplicand the second operand |
| 122 | + * @return the product of the operands |
| 123 | + * @throws ArithmeticException if the numerical type does not |
| 124 | + * allow multiplying the operands in question |
| 125 | + */ |
| 126 | + NT multiply(NT multiplier, NT multiplicand); |
| 127 | + |
| 128 | + /** |
| 129 | + * Division operation, binary operator "{@code /}". |
| 130 | + * |
| 131 | + * @apiNote |
| 132 | + * Numerical types can have different policies regarding how |
| 133 | + * divisors equal to zero are handled. Many types will throw an |
| 134 | + * {@code ArithmeticException} in those cases. However, other |
| 135 | + * types like {@linkplain StandardFloatingPoint floating-point |
| 136 | + * types} can return a special value like NaN (not-a-number). |
| 137 | + * |
| 138 | + * @throws ArithmeticException if the divisor is zero and zero |
| 139 | + * divisors are not allowed |
| 140 | + * @throws UnsupportedOperationException if division is not supported |
| 141 | + * @param dividend the first operand |
| 142 | + * @param divisor the second operand |
| 143 | + * @return the quotient of the operands |
| 144 | + */ |
| 145 | + NT divide(NT dividend, NT divisor); |
| 146 | + |
| 147 | + /** |
| 148 | + * Remainder operation, binary operator "{@code %}". |
| 149 | + * |
| 150 | + * @apiNote |
| 151 | + * Numerical types can have different policies regarding how |
| 152 | + * divisors equal to zero are handled. Many types will throw an |
| 153 | + * {@code ArithmeticException} in those cases. However, other |
| 154 | + * types like {@linkplain StandardFloatingPoint floating-point |
| 155 | + * types} can return a special value like NaN (not-a-number). |
| 156 | + * |
| 157 | + * @throws ArithmeticException if the divisor is zero and zero |
| 158 | + * divisors are not allowed |
| 159 | + * @throws UnsupportedOperationException if remainder is not supported |
| 160 | + * @param dividend the first operand |
| 161 | + * @param divisor the second operand |
| 162 | + * @return the quotient of the operands |
| 163 | + */ |
| 164 | + NT remainder(NT dividend, NT divisor); |
| 165 | + |
| 166 | + /** |
| 167 | + * Unary plus operation, unary operator "{@code +}". |
| 168 | + * |
| 169 | + * @apiNote |
| 170 | + * It this needed? Default to returning this/operand? Or just to |
| 171 | + * be be no-op not recognized for overloading? |
| 172 | + * |
| 173 | + * @implSpec |
| 174 | + * The default implementation returns the operand. |
| 175 | + * |
| 176 | + * @param operand the operand |
| 177 | + * @return unary plus of the operand |
| 178 | + */ |
| 179 | + default NT plus(NT operand) { |
| 180 | + return operand; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Negation operation, unary operator "{@code -}". |
| 185 | + * |
| 186 | + * @throws UnsupportedOperationException if negation is not supported |
| 187 | + * @param operand the operand |
| 188 | + * @return the negation of the operand |
| 189 | + * @throws ArithmeticException if the numerical type does not |
| 190 | + * allow negating the operand in question |
| 191 | + */ |
| 192 | + NT negate(NT operand); |
| 193 | +} |
0 commit comments