Alex Herbert created NUMBERS-139: ------------------------------------ Summary: Complex Multiplicative and Additive operators Key: NUMBERS-139 URL: https://issues.apache.org/jira/browse/NUMBERS-139 Project: Commons Numbers Issue Type: Improvement Components: complex Affects Versions: 1.0 Reporter: Alex Herbert Assignee: Alex Herbert
[ISO C99|http://www.open-std.org/JTC1/SC22/WG14/www/standards] Annex G specifies the multiplicative (G.5.1) and additive operators (G.5.2) in the case where the first or second argument is a real or an imaginary number. The following methods are already implemented: {code:java} multiply(Complex) multiply(double) divide(Complex) divide(double) add(Complex) add(double) subtract(Complex) subtract(double) {code} Add the following methods: {code:java} multiplyImaginary(double) divideImaginary(double) addImaginary(double) subtractImaginary(double) subtractFrom(double) subtractFromImaginary(double) {code} The final cases are important to maintain the negation of the sign of the component not involved in the computation. This computation: {noformat} 1 - (a + b i) = (1 - a) - b i {noformat} Is not correct for {{b=0}} using e.g.: {code:java} new Complex(1, 0).subtract(new Complex(3, 0)); {code} The result is (-2, 0) but it should be (-2, -0). Using the Complex involves zero subtraction for the imaginary part {{0 - 0}} but a special method would just use {{-0}} and negate the sign. Note: In ISO compliant C++ the use of a complex makes no special preservation of the sign of the negative: {code:c++} 1.0 - complex<double>(3, 0.0) == -2 + -0 i 1.0 - complex<double>(3, -0.0) == -2 + 0 i complex<double>(1, 0) - complex<double>(3, 0.0) == -2 + 0 i complex<double>(1, 0) - complex<double>(3, -0.0) == -2 + 0 i {code} The first case listed above is not possible with the current API. The method subtractFrom(Complex) would have an identical result as using subtract(Complex) and so adds no functional value to the API and is omitted. -- This message was sent by Atlassian Jira (v8.3.4#803005)