Skip to content

Commit

Permalink
Merge pull request #1315 from haileyajohnson/revert-nearly-equals
Browse files Browse the repository at this point in the history
revert switch to fuzzy equals
  • Loading branch information
haileyajohnson committed Mar 12, 2024
2 parents a2c7146 + 3bb2909 commit e57f760
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
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();
}
}

0 comments on commit e57f760

Please sign in to comment.