Skip to content

Commit

Permalink
revert switch to fuzzy equals
Browse files Browse the repository at this point in the history
  • Loading branch information
Hailey Johnson committed Mar 12, 2024
1 parent a2c7146 commit 92d1d83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cdm/core/src/main/java/ucar/nc2/util/Misc.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public static boolean nearlyEquals(float a, float b) {
}

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

/** AbsoluteDifference is less than {@link #defaultMaxRelativeDiffDouble}. */
Expand All @@ -107,8 +107,8 @@ public static boolean nearlyEquals(double a, double b) {
}

/** AbsoluteDifference is less than maxAbsDiff. */
public static boolean nearlyEquals(double a, double b, double maxAbsDiff) {
return DoubleMath.fuzzyEquals(a, b, maxAbsDiff);
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,14 @@ 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 +94,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 +162,11 @@ 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();
}
}

0 comments on commit 92d1d83

Please sign in to comment.