Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ public Long[] executeSimple(Object[][] params) {
Functions.registerFunction(new SimpleJavaFunction<Number>("round", new Parameter[] {new Parameter<>("n", DefaultClasses.NUMBER, true, null), new Parameter<>("d", DefaultClasses.NUMBER, true, new SimpleLiteral<Number>(0, false))}, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
if (params[0][0] instanceof Long)
return new Long[] {(Long) params[0][0]};
if (params[0][0] instanceof Long longValue)
return new Long[] {longValue};
double value = ((Number) params[0][0]).doubleValue();
int placement = ((Number) params[1][0]).intValue();
if (!Double.isFinite(value))
return new Double[] {value};

double placementDouble = ((Number) params[1][0]).doubleValue();
if (!Double.isFinite(placementDouble) || placementDouble >= Integer.MAX_VALUE || placementDouble <= Integer.MIN_VALUE)
return new Double[] {Double.NaN};
int placement = (int) placementDouble;
if (placement == 0)
return new Long[] {Math2.round(value)};
if (placement >= 0) {
Expand Down
11 changes: 11 additions & 0 deletions src/test/skript/tests/regressions/8085-rounding nan.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test "rounding nan":
assert isNaN(round(NaN, 1)) is true with "rounding NaN should return NaN"
assert isNaN(round(NaN)) is true with "rounding NaN should return NaN"
assert isNaN(round(NaN, NaN)) is true with "rounding NaN with NaN should return NaN"
assert isNaN(round(1.1, NaN)) is true with "rounding 1.1 with NaN should return NaN"

assert round(positive infinity) is positive infinity with "rounding positive infinity should return positive infinity"
assert round(negative infinity) is negative infinity with "rounding negative infinity should return negative infinity"
assert isNaN(round(1.1, positive infinity)) is true with "rounding 1.1 to positive infinity should return positive infinity"
assert isNaN(round(1.1, negative infinity)) is true with "rounding 1.1 to negative infinity should return negative infinity"
assert isNaN(round(1.1, 2147483648)) is true with "rounding 1.1 to too large a place should return NaN"