/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics.internal.math;

import static java.lang.Double.isInfinite;
import static java.lang.Double.isNaN;
import static java.util.Objects.requireNonNull;
import static org.jenetics.internal.util.Equality.eq;

import org.jenetics.internal.util.Equality;
import org.jenetics.internal.util.Hash;

/**
 * This class implements the the
 * <a href="http://en.wikipedia.org/wiki/Kahan_summation_algorithm">Kahan
 * summation algorithm</a>, which significantly reduces the numerical error when
 * adding double values.
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @since 3.0
 * @version 3.0 &mdash; <em>$Date: 2014-07-10 $</em>
 */
public final class DoubleAdder
	extends Number
	implements Comparable<DoubleAdder>
{
	private static final long serialVersionUID = 1L;

	private double _sum = 0.0;
	private double _simpleSum = 0.0;
	private double _compensation = 0.0;

	/**
	 * Create a new adder with the given default {@code value}.
	 *
	 * @param value the initial {@code value} of this adder.
	 */
	public DoubleAdder(final double value) {
		add(value);
	}

	/**
	 * Create a new adder with the initial value of {@code 0.0}.
	 */
	public DoubleAdder() {
	}

	/**
	 * Reset the adder to the initial value of {@code 0.0}.
	 *
	 * @return {@code this} adder, for command chaining
	 */
	private DoubleAdder reset() {
		_sum = 0.0;
		_simpleSum = 0.0;
		_compensation = 0.0;
		return this;
	}

	/**
	 * Set the adder to the given {@code value}.
	 *
	 * @param value the new adder value
	 * @return {@code this} adder, for command chaining
	 */
	public DoubleAdder set(final double value) {
		return reset().add(value);
	}

	/**
	 * Set the adder to the given {@code value}.
	 *
	 * @param value the new adder value
	 * @return {@code this} adder, for command chaining
	 * @throws java.lang.NullPointerException if the given {@code value} is
	 *         {@code null}
	 */
	public DoubleAdder set(final DoubleAdder value) {
		return reset().add(requireNonNull(value));
	}

	/**
	 * Add the given {@code value} to this adder, using the
	 * <a href="http://en.wikipedia.org/wiki/Kahan_summation_algorithm">Kahan
	 * summation algorithm</a>
	 *
	 * @param value the {@code value} to add
	 * @return {@code this} adder, for command chaining
	 */
	public DoubleAdder add(final double value) {
		addWithCompensation(value);
		_simpleSum += value;
		return this;
	}

	private void addWithCompensation(final double value) {
		final double y = value - _compensation;
		final double t = _sum + y;
		_compensation = (t - _sum) - y;
		_sum = t;
	}

	/**
	 * Add the given {@code value} to this adder, using the
	 * <a href="http://en.wikipedia.org/wiki/Kahan_summation_algorithm">Kahan
	 * summation algorithm</a>
	 *
	 * @param value the {@code value} to add
	 * @return {@code this} adder, for command chaining
	 * @throws java.lang.NullPointerException if the given {@code value} is
	 *         {@code null}
	 */
	public DoubleAdder add(final DoubleAdder value) {
		addWithCompensation(value._sum);
		addWithCompensation(value._compensation);
		_simpleSum += value._simpleSum;
		return this;
	}

	public double value() {
		final double result =  _sum + _compensation;
		return isNaN(result) && isInfinite(_simpleSum) ? _simpleSum : result;
	}

	@Override
	public int intValue() {
		return (int)value();
	}

	@Override
	public long longValue() {
		return (long)value();
	}

	@Override
	public float floatValue() {
		return (float)value();
	}

	@Override
	public double doubleValue() {
		return value();
	}

	@Override
	public int compareTo(final DoubleAdder other) {
		return Double.compare(doubleValue(), other.doubleValue());
	}

	@Override
	public int hashCode() {
		return Hash.of(DoubleAdder.class).and(doubleValue()).value();
	}

	@Override
	public boolean equals(final Object obj) {
		return Equality.of(this, obj).test(adder ->
			eq(doubleValue(), adder.doubleValue())
		);
	}

	@Override
	public String toString() {
		return Double.toString(doubleValue());
	}

}

@@ -1401,12 +1401,12 @@
 14-0
-7-10
+9-03
  $%3C/
@@ -3864,16 +3864,463 @@
 is;%0A%09%7D%0A%0A
+%09/**%0A%09 * Add the given %7B@code value%7D to this adder, using the%0A%09 * %3Ca href=%22http://en.wikipedia.org/wiki/Kahan_summation_algorithm%22%3EKahan%0A%09 * summation algorithm%3C/a%3E%0A%09 *%0A%09 * @param other the %7B@code value%7D to add%0A%09 * @return %7B@code this%7D adder, for command chaining%0A%09 * @throws java.lang.NullPointerException if the given %7B@code value%7D is%0A%09 *         %7B@code null%7D%0A%09 */%0A%09public DoubleAdder combine(final DoubleAdder other) %7B%0A%09%09return add(other);%0A%09%7D%0A%0A
 %09public 

Add 'DoubleAdder.combine' method.