Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix testing tools NaN comparison and correct new failing tests. #4794

Merged
merged 11 commits into from
Nov 16, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,26 @@ private boolean addChunk(DoubleChunk<? extends Values> values, long destination,
if (forceNanResult || nonNullCount <= 1) {
resultColumn.set(destination, Double.NaN);
} else {
final double variance = (newSum2 - newSum * newSum / nonNullCount) / (nonNullCount - 1);
resultColumn.set(destination, std ? Math.sqrt(variance) : variance);
// If the sum has reach +/-Infinity, we are stuck with NaN forever.
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved
if (Double.isInfinite(newSum) || Double.isInfinite(newSum2)) {
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved
resultColumn.set(destination, Double.NaN);
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

// Perform the calculation in a way that minimizes the impact of FP error.
final double eps = Math.ulp(newSum2);
final double vs2bar = newSum * (newSum / nonNullCount);
final double delta = newSum2 - vs2bar;
final double rel_eps = delta / eps;

// Return zero when the variance is leq the FP error.
final double variance = Math.abs(rel_eps) > 1.0 ? delta / (nonNullCount - 1) : 0.0;
if (variance < 0.0) {
// Negative variance can only be due to FP error.
resultColumn.set(destination, 0.0);
} else {
resultColumn.set(destination, std ? Math.sqrt(variance) : variance);
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved
}
}
return true;
} else if (forceNanResult || (nonNullCounter.getCountUnsafe(destination) <= 1)) {
Expand Down Expand Up @@ -150,8 +168,27 @@ private boolean removeChunk(DoubleChunk<? extends Values> values, long destinati
resultColumn.set(destination, Double.NaN);
return true;
}
final double variance = (newSum2 - newSum * newSum / totalNormalCount) / (totalNormalCount - 1);
resultColumn.set(destination, std ? Math.sqrt(variance) : variance);

// If the sum has reach +/-Infinity, we are stuck with NaN forever.
if (Double.isInfinite(newSum) || Double.isInfinite(newSum2)) {
resultColumn.set(destination, Double.NaN);
return true;
}

// Perform the calculation in a way that minimizes the impact of FP error.
final double eps = Math.ulp(newSum2);
final double vs2bar = newSum * (newSum / totalNormalCount);
final double delta = newSum2 - vs2bar;
final double rel_eps = delta / eps;

// Return zero when the variance is leq the FP error.
final double variance = Math.abs(rel_eps) > 1.0 ? delta / (totalNormalCount - 1) : 0.0;
if (variance < 0.0) {
// Negative variance can only be due to FP error.
resultColumn.set(destination, 0.0);
} else {
resultColumn.set(destination, std ? Math.sqrt(variance) : variance);
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,26 @@ private boolean addChunk(FloatChunk<? extends Values> values, long destination,
if (forceNanResult || nonNullCount <= 1) {
resultColumn.set(destination, Double.NaN);
} else {
final double variance = (newSum2 - newSum * newSum / nonNullCount) / (nonNullCount - 1);
resultColumn.set(destination, std ? Math.sqrt(variance) : variance);
// If the sum has reach +/-Infinity, we are stuck with NaN forever.
if (Double.isInfinite(newSum) || Double.isInfinite(newSum2)) {
resultColumn.set(destination, Double.NaN);
return true;
}

// Perform the calculation in a way that minimizes the impact of FP error.
final double eps = Math.ulp(newSum2);
final double vs2bar = newSum * (newSum / nonNullCount);
final double delta = newSum2 - vs2bar;
final double rel_eps = delta / eps;

// Return zero when the variance is leq the FP error.
final double variance = Math.abs(rel_eps) > 1.0 ? delta / (nonNullCount - 1) : 0.0;
if (variance < 0.0) {
// Negative variance can only be due to FP error.
resultColumn.set(destination, 0.0);
} else {
resultColumn.set(destination, std ? Math.sqrt(variance) : variance);
}
}
return true;
} else if (forceNanResult || (nonNullCounter.getCountUnsafe(destination) <= 1)) {
Expand Down Expand Up @@ -145,8 +163,27 @@ private boolean removeChunk(FloatChunk<? extends Values> values, long destinatio
resultColumn.set(destination, Double.NaN);
return true;
}
final double variance = (newSum2 - newSum * newSum / totalNormalCount) / (totalNormalCount - 1);
resultColumn.set(destination, std ? Math.sqrt(variance) : variance);

// If the sum has reach +/-Infinity, we are stuck with NaN forever.
if (Double.isInfinite(newSum) || Double.isInfinite(newSum2)) {
resultColumn.set(destination, Double.NaN);
return true;
}

// Perform the calculation in a way that minimizes the impact of FP error.
final double eps = Math.ulp(newSum2);
final double vs2bar = newSum * (newSum / totalNormalCount);
final double delta = newSum2 - vs2bar;
final double rel_eps = delta / eps;

// Return zero when the variance is leq the FP error.
final double variance = Math.abs(rel_eps) > 1.0 ? delta / (totalNormalCount - 1) : 0.0;
if (variance < 0.0) {
// Negative variance can only be due to FP error.
resultColumn.set(destination, 0.0);
} else {
resultColumn.set(destination, std ? Math.sqrt(variance) : variance);
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public void writeToOutputChunk(int outIdx) {
outputValues.set(outIdx, NULL_DOUBLE);
} else {
final int count = byteWindowValues.size() - nullCount;
outputValues.set(outIdx, curVal / (double)count);
if (count == 0) {
outputValues.set(outIdx, Double.NaN);
} else {
outputValues.set(outIdx, curVal / (double)count);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ public void writeToOutputChunk(int outIdx) {
outputValues.set(outIdx, NULL_DOUBLE);
} else {
final int count = charWindowValues.size() - nullCount;
outputValues.set(outIdx, curVal / (double)count);
if (count == 0) {
outputValues.set(outIdx, Double.NaN);
} else {
outputValues.set(outIdx, curVal / (double)count);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ public void writeToOutputChunk(int outIdx) {
outputValues.set(outIdx, NULL_DOUBLE);
} else {
final int count = aggSum.size() - nullCount;
outputValues.set(outIdx, aggSum.evaluate() / (double)count);
if (count == 0) {
outputValues.set(outIdx, Double.NaN);
} else {
outputValues.set(outIdx, aggSum.evaluate() / (double)count);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ public void writeToOutputChunk(int outIdx) {
outputValues.set(outIdx, NULL_DOUBLE);
} else {
final int count = aggSum.size() - nullCount;
outputValues.set(outIdx, aggSum.evaluate() / (double)count);
if (count == 0) {
outputValues.set(outIdx, Double.NaN);
} else {
outputValues.set(outIdx, aggSum.evaluate() / (double)count);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ public void writeToOutputChunk(int outIdx) {
outputValues.set(outIdx, NULL_DOUBLE);
} else {
final int count = intWindowValues.size() - nullCount;
outputValues.set(outIdx, curVal / (double)count);
if (count == 0) {
outputValues.set(outIdx, Double.NaN);
} else {
outputValues.set(outIdx, curVal / (double)count);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ public void writeToOutputChunk(int outIdx) {
outputValues.set(outIdx, NULL_DOUBLE);
} else {
final int count = longWindowValues.size() - nullCount;
outputValues.set(outIdx, curVal / (double)count);
if (count == 0) {
outputValues.set(outIdx, Double.NaN);
} else {
outputValues.set(outIdx, curVal / (double)count);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ public void writeToOutputChunk(int outIdx) {
outputValues.set(outIdx, NULL_DOUBLE);
} else {
final int count = shortWindowValues.size() - nullCount;
outputValues.set(outIdx, curVal / (double)count);
if (count == 0) {
outputValues.set(outIdx, Double.NaN);
} else {
outputValues.set(outIdx, curVal / (double)count);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ---------------------------------------------------------------------------------------------------------------------
* AUTO-GENERATED CLASS - DO NOT EDIT MANUALLY - for any changes edit CharRollingProductOperator and regenerate
* AUTO-GENERATED CLASS - DO NOT EDIT MANUALLY - for any changes edit FloatRollingProductOperator and regenerate
* ---------------------------------------------------------------------------------------------------------------------
*/
package io.deephaven.engine.table.impl.updateby.rollingproduct;
Expand Down Expand Up @@ -30,6 +30,7 @@ protected class Context extends BaseDoubleUpdateByOperator.Context {
protected AggregatingDoubleRingBuffer buffer;

private int zeroCount;
private int nanCount;

protected Context(final int affectedChunkSize, final int influencerChunkSize) {
super(affectedChunkSize);
Expand All @@ -48,6 +49,7 @@ protected Context(final int affectedChunkSize, final int influencerChunkSize) {
},
true);
zeroCount = 0;
nanCount = 0;
}

@Override
Expand Down Expand Up @@ -76,6 +78,8 @@ public void push(int pos, int count) {
buffer.addUnsafe(val);
if (val == 0) {
zeroCount++;
} else if (Double.isNaN(val)) {
nanCount++;
}
}
}
Expand All @@ -90,6 +94,8 @@ public void pop(int count) {

if (val == NULL_DOUBLE) {
nullCount--;
} else if (Double.isNaN(val)) {
--nanCount;
} else if (val == 0) {
--zeroCount;
}
Expand All @@ -101,14 +107,19 @@ public void writeToOutputChunk(int outIdx) {
if (buffer.size() == nullCount) {
outputValues.set(outIdx, NULL_DOUBLE);
} else {
outputValues.set(outIdx, zeroCount > 0 ? 0.0 : buffer.evaluate());
if (nanCount > 0) {
outputValues.set(outIdx, Double.NaN);
} else {
outputValues.set(outIdx, zeroCount > 0 ? 0.0 : buffer.evaluate());
}
}
}

@Override
public void reset() {
super.reset();
zeroCount = 0;
nanCount = 0;
buffer.clear();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/*
* ---------------------------------------------------------------------------------------------------------------------
* AUTO-GENERATED CLASS - DO NOT EDIT MANUALLY - for any changes edit CharRollingProductOperator and regenerate
* ---------------------------------------------------------------------------------------------------------------------
*/
package io.deephaven.engine.table.impl.updateby.rollingproduct;

import io.deephaven.base.ringbuffer.AggregatingDoubleRingBuffer;
Expand Down Expand Up @@ -30,6 +25,7 @@ protected class Context extends BaseDoubleUpdateByOperator.Context {
protected AggregatingDoubleRingBuffer buffer;

private int zeroCount;
private int nanCount;

protected Context(final int affectedChunkSize, final int influencerChunkSize) {
super(affectedChunkSize);
Expand All @@ -48,6 +44,7 @@ protected Context(final int affectedChunkSize, final int influencerChunkSize) {
},
true);
zeroCount = 0;
nanCount = 0;
}

@Override
Expand Down Expand Up @@ -76,6 +73,8 @@ public void push(int pos, int count) {
buffer.addUnsafe(val);
if (val == 0) {
zeroCount++;
} else if (Double.isNaN(val)) {
nanCount++;
}
}
}
Expand All @@ -90,6 +89,8 @@ public void pop(int count) {

if (val == NULL_DOUBLE) {
nullCount--;
} else if (Double.isNaN(val)) {
--nanCount;
} else if (val == 0) {
--zeroCount;
}
Expand All @@ -101,14 +102,19 @@ public void writeToOutputChunk(int outIdx) {
if (buffer.size() == nullCount) {
outputValues.set(outIdx, NULL_DOUBLE);
} else {
outputValues.set(outIdx, zeroCount > 0 ? 0.0 : buffer.evaluate());
if (nanCount > 0) {
outputValues.set(outIdx, Double.NaN);
} else {
outputValues.set(outIdx, zeroCount > 0 ? 0.0 : buffer.evaluate());
}
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Override
public void reset() {
super.reset();
zeroCount = 0;
nanCount = 0;
buffer.clear();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ public void writeToOutputChunk(int outIdx) {
final double valueSquareSum = valueSquareBuffer.evaluate();
final double valueSum = valueBuffer.evaluate();

if (Double.isNaN(valueSquareSum) || Double.isNaN(valueSum)) {
if (Double.isNaN(valueSquareSum)
|| Double.isNaN(valueSum)
|| Double.isInfinite(valueSquareSum)
|| Double.isInfinite(valueSum)) {
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved
outputValues.set(outIdx, Double.NaN);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ public void writeToOutputChunk(int outIdx) {
final double valueSquareSum = valueSquareBuffer.evaluate();
final double valueSum = valueBuffer.evaluate();

if (Double.isNaN(valueSquareSum) || Double.isNaN(valueSum)) {
if (Double.isNaN(valueSquareSum)
|| Double.isNaN(valueSum)
|| Double.isInfinite(valueSquareSum)
|| Double.isInfinite(valueSum)) {
outputValues.set(outIdx, Double.NaN);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ public void writeToOutputChunk(int outIdx) {
final double valueSquareSum = valueSquareBuffer.evaluate();
final double valueSum = valueBuffer.evaluate();

if (Double.isNaN(valueSquareSum) || Double.isNaN(valueSum)) {
if (Double.isNaN(valueSquareSum)
|| Double.isNaN(valueSum)
|| Double.isInfinite(valueSquareSum)
|| Double.isInfinite(valueSum)) {
outputValues.set(outIdx, Double.NaN);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ public void writeToOutputChunk(int outIdx) {
final double valueSquareSum = valueSquareBuffer.evaluate();
final double valueSum = valueBuffer.evaluate();

if (Double.isNaN(valueSquareSum) || Double.isNaN(valueSum)) {
if (Double.isNaN(valueSquareSum)
|| Double.isNaN(valueSum)
|| Double.isInfinite(valueSquareSum)
|| Double.isInfinite(valueSum)) {
outputValues.set(outIdx, Double.NaN);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ public void writeToOutputChunk(int outIdx) {
final double valueSquareSum = valueSquareBuffer.evaluate();
final double valueSum = valueBuffer.evaluate();

if (Double.isNaN(valueSquareSum) || Double.isNaN(valueSum)) {
if (Double.isNaN(valueSquareSum)
|| Double.isNaN(valueSum)
|| Double.isInfinite(valueSquareSum)
|| Double.isInfinite(valueSum)) {
outputValues.set(outIdx, Double.NaN);
return;
}
Expand Down
Loading
Loading