Files
mg4/app/src/main/java/com/evsuite/abrp/KahanSum.java
T

39 lines
991 B
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.evsuite.abrp;
/**
* Neumaier–Kahan compensated summation.
*
* Consumption is integrated from thousands of sub-watt-hour slices per drive. A plain
* {@code sum += x} loses the low bits of every one of them, and the error is one-signed,
* so it accumulates rather than cancelling. Ported from DriveHub_Dort, where this is what
* keeps the lifetime counter honest over months.
*/
final class KahanSum {
private double sum;
private double compensation;
void add(double input) {
double y = input - compensation;
double t = sum + y;
compensation = (t - sum) - y;
sum = t;
}
double get() {
return sum;
}
/** Clears the total and the compensation term. */
void reset() {
sum = 0d;
compensation = 0d;
}
/** Seeds the total from persistent storage; the compensation term starts clean. */
void setTotal(double total) {
sum = total;
compensation = 0d;
}
}