This is an automated email from the ASF dual-hosted git repository.

xiazcy pushed a commit to branch type-enum-poc
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit 66294ec8dd9c527a87673c566dd051bd1c7c3e80
Author: xiazcy <[email protected]>
AuthorDate: Thu Jul 17 14:33:08 2025 -0700

    update to throw when casting infinity and nan to whole numbers
---
 .../org/apache/tinkerpop/gremlin/util/NumberHelper.java     |  6 ++++++
 .../process/traversal/step/map/AsNumberStepTest.java        | 13 ++++++++++---
 .../org/apache/tinkerpop/gremlin/util/NumberHelperTest.java |  2 +-
 3 files changed, 17 insertions(+), 4 deletions(-)

diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/NumberHelper.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/NumberHelper.java
index 137156fbcc..b31499a4ad 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/NumberHelper.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/NumberHelper.java
@@ -787,6 +787,12 @@ public final class NumberHelper {
     }
 
     private static Long getLong(Number num, N numberToken) {
+        // Explicitly throw when converting floating point infinity and NaN to 
whole numbers
+        if (Double.isNaN(num.doubleValue())) {
+            throw new ArithmeticException(String.format("Can't convert NaN to 
%s.", numberToken));
+        } else if (Double.isInfinite(num.doubleValue())) {
+            throw new ArithmeticException(String.format("Can't convert 
floating point infinity to %s.", numberToken));
+        }
         try {
             return num.getClass().equals(BigInteger.class) ? ((BigInteger) 
num).longValueExact() : num.longValue();
         } catch (ArithmeticException ae) {
diff --git 
a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/AsNumberStepTest.java
 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/AsNumberStepTest.java
index eb1f9547d8..1eef3b829b 100644
--- 
a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/AsNumberStepTest.java
+++ 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/AsNumberStepTest.java
@@ -188,8 +188,6 @@ public class AsNumberStepTest extends StepTest {
         assertEquals(Float.POSITIVE_INFINITY, 
__.__(Double.POSITIVE_INFINITY).asNumber(N.nfloat).next());
         assertEquals(Float.NEGATIVE_INFINITY, 
__.__(Double.NEGATIVE_INFINITY).asNumber(N.nfloat).next());
         assertEquals(Float.NaN, __.__(Double.NaN).asNumber(N.nfloat).next());
-        assertEquals(Long.MAX_VALUE, 
__.__(Double.POSITIVE_INFINITY).asNumber(N.nlong).next());
-        assertEquals(0, __.__(Double.NaN).asNumber(N.nint).next());
     }
 
     @Test
@@ -231,6 +229,16 @@ public class AsNumberStepTest extends StepTest {
 
 // ===== OVERFLOW EXCEPTION TESTS =====
 
+    @Test(expected = ArithmeticException.class)
+    public void shouldThrowCastExceptionConvertingInfinityToWholeNumber() {
+        __.__(Double.POSITIVE_INFINITY).asNumber(N.nlong).next();
+    }
+
+    @Test(expected = ArithmeticException.class)
+    public void shouldThrowCastExceptionConvertingNaNToWholeNumber() {
+        __.__(Double.NaN).asNumber(N.nint).next();
+    }
+
     @Test(expected = ArithmeticException.class)
     public void shouldThrowOverflowExceptionWhenStringByteOverflowsPositive() {
         __.__("128").asNumber(N.nbyte).next();
@@ -239,7 +247,6 @@ public class AsNumberStepTest extends StepTest {
     @Test(expected = ArithmeticException.class)
     public void shouldThrowOverflowExceptionWhenStringByteOverflowsNegative() {
         __.__("-129").asNumber(N.nbyte).next();
-
     }
 
     @Test(expected = ArithmeticException.class)
diff --git 
a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/util/NumberHelperTest.java
 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/util/NumberHelperTest.java
index ef88db95ce..20772da0c9 100644
--- 
a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/util/NumberHelperTest.java
+++ 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/util/NumberHelperTest.java
@@ -727,7 +727,7 @@ public class NumberHelperTest {
     @Test
     public void shouldCastToConvertToShort() {
         final Integer value = 42;
-        assertEquals(Short.valueOf((short) 42), NumberHelper.castTo(value, 
N.nbyte));
+        assertEquals(Short.valueOf((short) 42), NumberHelper.castTo(value, 
N.nshort));
     }
 
     @Test

Reply via email to