[ https://issues.apache.org/jira/browse/TINKERPOP-3166?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18009357#comment-18009357 ]
ASF GitHub Bot commented on TINKERPOP-3166: ------------------------------------------- Cole-Greer commented on code in PR #3153: URL: https://github.com/apache/tinkerpop/pull/3153#discussion_r2226308237 ########## gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/NumberHelper.java: ########## @@ -721,6 +723,86 @@ public static Number coerceTo(final Number a, final Class<? extends Number> claz return a; } + /** + * Casts the given number to the specified numeric type if it can fit into it. + * Otherwise, throw. + * + * @param a the number to be cast + * @param numberToken the number token denoting the desired type to cast + * @return the number cast to the specified type + * @throws IllegalArgumentException if the specified numeric type is unsupported + * @throws ArithmeticException if the number overflows + */ + public static Number castTo(final Number a, final N numberToken) { + Class<?> clazz = numberToken.getType(); + if (a.getClass().equals(clazz)) { + return a; + } else if (clazz.equals(Integer.class)) { + Long val = getLong(a, numberToken); + if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) { + return a.intValue(); + } + } else if (clazz.equals(Long.class)) { + return getLong(a, numberToken); + } else if (clazz.equals(Float.class)) { + // BigDecimal to double will overflow into Infinity, we want to throw instead of passing through + if (!a.getClass().equals(BigDecimal.class) && + (Double.isInfinite(a.doubleValue()) || Double.isNaN(a.doubleValue()))) { + return a.floatValue(); + } + if (a.doubleValue() >= -Float.MAX_VALUE && a.doubleValue() <= Float.MAX_VALUE) { + return a.floatValue(); + } + } else if (clazz.equals(Double.class)) { + // BigDecimal to double will overflow into Infinity, we want to throw instead of passing through + if (!a.getClass().equals(BigDecimal.class) && + (Double.isInfinite(a.doubleValue()) || Double.isNaN(a.doubleValue()))) { + return a.doubleValue(); + } + if (!Double.isInfinite(a.doubleValue())) { + // float losses precision, use string intermediate + return a.getClass().equals(Float.class) ? Double.parseDouble(a.toString()) : a.doubleValue(); + } + } else if (clazz.equals(Byte.class)) { + Long val = getLong(a, numberToken); + if (val >= Byte.MIN_VALUE && val <= Byte.MAX_VALUE) { + return a.byteValue(); + } + } else if (clazz.equals(Short.class)) { + Long val = getLong(a, numberToken); + if (val >= Short.MIN_VALUE && val <= Short.MAX_VALUE) { + return a.shortValue(); + } + } else if (clazz.equals(BigInteger.class)) { + return NumberHelper.bigIntegerValue(a); + } else if (clazz.equals(BigDecimal.class)) { + // float losses precision, use string intermediate + return a.getClass().equals(Float.class) ? new BigDecimal(a.toString()) : NumberHelper.bigDecimalValue(a); + } else { + throw new IllegalArgumentException("Unsupported number type token: " + numberToken); + } + + throw new ArithmeticException(String.format("Can't convert number of type %s to %s due to overflow.", + a.getClass().getSimpleName(), numberToken)); + } + + private static Long getLong(final Number num, final 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)); + } + 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(); Review Comment: We should also use [BigDecimal.longValueExact()](https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#longValueExact--) as `BigDecimal.longValue()` may result in some big numbers appearing small: ``` new BigDecimal(Long.MAX_VALUE+"1").longValue() ==>-9 gremlin> g.inject(new BigDecimal(Long.MAX_VALUE+"1")).asNumber(N.nshort) ==>-9 ``` > Add number conversion step asNumber() > ------------------------------------- > > Key: TINKERPOP-3166 > URL: https://issues.apache.org/jira/browse/TINKERPOP-3166 > Project: TinkerPop > Issue Type: Improvement > Components: language > Affects Versions: 3.8.0 > Reporter: Yang Xia > Priority: Major > > Given the addition of the {{asString()}} and {{asDate()}} steps in the 3.7 > line, it should also be helpful to add an {{asNumber()}} step that does > numerical casting/conversions. > The current idea is for the {{asNumber()}} step to convert the incoming > traverser to the nearest parsable type (e.g. int or double) if no argument is > provided, or to the desired numerical type, based on a number token > ({{{}N{}}}) provided. Like the {{asDate()}} step, it will not be scoped (for > now, scopes can be added in the future). > Some conjured examples: > {code:java} > gremlin> g.inject(5).asNumber() > ==> 5 // parses to int > gremlin> g.inject(5.123f).asNumber() > ==> 5.123 > gremlin> g.inject(5.43).asNumber(N.int) > ==> 5 {code} > More details can be found in the [proposal > doc|https://github.com/apache/tinkerpop/blob/master/docs/src/dev/future/proposal-asnumber-step-6.asciidoc]. > -- This message was sent by Atlassian Jira (v8.20.10#820010)