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

revert switch to fuzzy equals #1315

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cdm/core/src/main/java/ucar/nc2/util/Misc.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ public static boolean nearlyEquals(float a, float b) {
return nearlyEquals(a, b, defaultMaxRelativeDiffFloat);
}

/** AbsoluteDifference is less than maxAbsDiff. */
public static boolean nearlyEquals(float a, float b, float maxAbsDiff) {
return DoubleMath.fuzzyEquals(a, b, maxAbsDiff);
/** RelativeDifference is less than maxRelDiff. */
public static boolean nearlyEquals(float a, float b, float maxRelDiff) {
return relativeDifference(a, b) < maxRelDiff;
}

/** AbsoluteDifference is less than {@link #defaultMaxRelativeDiffDouble}. */
public static boolean nearlyEquals(double a, double b) {
return nearlyEquals(a, b, defaultMaxRelativeDiffDouble);
}

/** AbsoluteDifference is less than maxAbsDiff. */
public static boolean nearlyEquals(double a, double b, double maxAbsDiff) {
return DoubleMath.fuzzyEquals(a, b, maxAbsDiff);
/** RelativeDifference is less than maxRelDiff. */
public static boolean nearlyEquals(double a, double b, double maxRelDiff) {
return relativeDifference(a, b) < maxRelDiff;
}

/** AbsoluteDifference is less than maxAbsDiff. */
Expand Down
17 changes: 17 additions & 0 deletions cdm/core/src/test/java/ucar/nc2/filter/TestEnhancements.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.google.common.truth.Truth.assertThat;

import java.io.IOException;
import java.util.Arrays;

public class TestEnhancements {

Expand Down Expand Up @@ -75,6 +76,13 @@ public static void setUp() throws IOException, InvalidRangeException {
.addAttribute(new Attribute(CDM.FILL_VALUE, SIGNED_SCALED_FILL_VALUE))
.addAttribute(new Attribute(CDM.MISSING_VALUE, SIGNED_SCALED_MISSING_VALUE));

// short data with small scale and offsets
Array smallVals = Array.factory(DataType.FLOAT, new int[] {dataLen}, missingData);

builder.addVariable("smallVals", DataType.FLOAT, "dim").addAttribute(new Attribute(CDM.SCALE_FACTOR, 1e-12))
.addAttribute(new Attribute(CDM.ADD_OFFSET, 1e-12)).addAttribute(new Attribute(CDM.FILL_VALUE, 110));


// write data
NetcdfFormatWriter writer = builder.build();
writer.write(writer.findVariable("signedVar"), new int[1], signedData);
Expand All @@ -85,6 +93,7 @@ public static void setUp() throws IOException, InvalidRangeException {
writer.write(writer.findVariable("validMinMax"), new int[1], missingDataArray);
writer.write(writer.findVariable("validRange"), new int[1], missingDataArray);
writer.write(writer.findVariable("enhanceAll"), new int[1], enhanceAllArray);
writer.write(writer.findVariable("smallVals"), new int[1], smallVals);
writer.close();
ncd = NetcdfDatasets.openDataset(filePath);
}
Expand Down Expand Up @@ -152,4 +161,12 @@ public void testCombinedEnhancements() throws IOException {
Array data = v.read();
assertThat((double[]) data.copyTo1DJavaArray()).isEqualTo(expected);
}

@Test
public void testConvertMissingWithSmallScaleAndOffset() throws IOException {
Variable v = ncd.findVariable("smallVals");
Array data = v.read();
assertThat(Double.isNaN(data.getDouble(2))).isTrue();
assertThat(Double.isNaN(data.getDouble(1))).isFalse();
}
}
Loading