Skip to content

Commit

Permalink
Fix float equality
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Jun 19, 2024
1 parent 1816b2b commit f6fb0a6
Showing 1 changed file with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public static BFloatSubType createFloatSubType(boolean allowed, Double[] values)
return NOTHING;
}
}
Arrays.sort(values);
return new BFloatSubType(new FloatSubTypeData(allowed, values));
}

Expand Down Expand Up @@ -128,7 +127,43 @@ static final class FloatSubTypeData extends EnumerableSubtypeData<Double> implem

private FloatSubTypeData(boolean allowed, Double[] values) {
this.allowed = allowed;
this.values = values;
this.values = filteredValues(values);
}

private static Double[] filteredValues(Double[] values) {
for (int i = 0; i < values.length; i++) {
values[i] = canon(values[i]);
}
if (values.length < 2) {
return values;
}
Arrays.sort(values);
Double[] buffer = new Double[values.length];
buffer[0] = values[0];
int bufferLen = 1;
for (int i = 1; i < values.length; i++) {
Double value = values[i];
Double prevValue = values[i - 1];
if (isSame(value, prevValue)) {
continue;

Check warning on line 148 in bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/semtype/BFloatSubType.java

View check run for this annotation

Codecov / codecov/patch

bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/semtype/BFloatSubType.java#L148

Added line #L148 was not covered by tests
}
buffer[bufferLen++] = value;
}
return Arrays.copyOf(buffer, bufferLen);
}

private static Double canon(Double d) {
if (d.equals(0.0) || d.equals(-0.0)) {
return 0.0;
}
return d;
}

private static boolean isSame(double f1, double f2) {
if (Double.isNaN(f1)) {
return Double.isNaN(f2);

Check warning on line 164 in bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/semtype/BFloatSubType.java

View check run for this annotation

Codecov / codecov/patch

bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/semtype/BFloatSubType.java#L164

Added line #L164 was not covered by tests
}
return f1 == f2;
}

@Override
Expand Down

0 comments on commit f6fb0a6

Please sign in to comment.