All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.it.unimi.dsi.util.KahanSummation Maven / Gradle / Ivy

/*
 * Copyright (C) 2011-2020 Sebastiano Vigna
 *
 *  This library is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as published by the Free
 *  Software Foundation; either version 3 of the License, or (at your option)
 *  any later version.
 *
 *  This library is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, see .
 *
 */

package it.unimi.dsi.util;

/** Kahan's
 * summation algorithm encapsulated in an object.  */

public class KahanSummation {
	/** The current value of the sum. */
	private double value;
	/** The current correction. */
	private double c;

	/** Adds a value.
	 * @param v the value to be added to the sum.
	 */
	public void add(final double v) {
		final double y = v - c;
		final double t = value + y;
		c = (t - value) - y;
		value = t;
	}

	/** Returns the sum computed so far.
	 * @return the sum computed so far.
	 */
	public double value() {
		return value;
	}

	/** Resets the current value and correction to zero. */
	public void reset() {
		value = c = 0;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy