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

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

Go to download

The DSI utilities are a mish mash of classes accumulated during the last ten years in projects developed at the DSI (Dipartimento di Scienze dell'Informazione, i.e., Information Sciences Department), now DI (Dipartimento di Informatica, i.e., Informatics Department), of the Universita` degli Studi di Milano.

There is a newer version: 2.7.3
Show newest version
package it.unimi.dsi.util;

/*
 * Copyright (C) 2011-2016 Sebastiano Vigna
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 3 of the License, or (at your option)
 *  any later version.
 *
 *  This program 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 General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see .
 */

/** 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 - 2025 Weber Informatics LLC | Privacy Policy