Division implements Scalar<T> { @Override public T value(final Field<T> field) { final FieldMultiplication<T> mult = field.multiplication(); final T inverse = mult.inverse(field.actual(this.divisor)); return mult.multiply(field.actual(this.dividend), inverse); } Division(final Scalar<T> first, final Scalar<T> second); Scalar<T> first(); Scalar<T> second(); @Override T value(final Field<T> field);  }
@Test public void divisionDelegatesToFieldMultiplication() { final MkMultiplication<Object> mult = new MkMultiplication<>(new Object()); final SpyField<Object> field = new SpyField<>( new MkField<Object>(new Object(), mult) ); new Division<>( new Scalar.Default<>(new Object()), new Scalar.Default<>(new Object()) ).value(field); MatcherAssert.assertThat( field.calls().multiplicationed(), Matchers.is(true) ); MatcherAssert.assertThat( mult.inverted(), Matchers.is(true) ); MatcherAssert.assertThat( mult.multiplied(), Matchers.greaterThan(0) ); }
@Test public void throwsExceptionWhenDivideByZero() { this.thrown.expect(IllegalArgumentException.class); final Scalar<Double> first = new Scalar.Default<>(Math.random()); final Scalar<Double> second = new AddIdentity<>(); new Division<>(first, second).value(new Decimal()); }