[
https://issues.apache.org/jira/browse/TINKERPOP-3166?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18017756#comment-18017756
]
ASF GitHub Bot commented on TINKERPOP-3166:
-------------------------------------------
Cole-Greer commented on code in PR #3153:
URL: https://github.com/apache/tinkerpop/pull/3153#discussion_r2317038257
##########
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/NumberHelper.java:
##########
@@ -687,26 +689,69 @@ public static Integer compare(final Number a, final
Number b) {
* @throws IllegalArgumentException if the specified numeric type is
unsupported
*/
public static Number coerceTo(final Number a, final Class<? extends
Number> clazz) {
+ try {
+ return performConversion(a, clazz);
+ } catch (ArithmeticException e) {
+ // return as-is since it didn't fit the type we wanted to coerce to
+ 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<? extends Number> clazz = numberToken.getType();
+ return performConversion(a, clazz);
+ }
+
+ /**
+ * Core conversion logic.
+ * Throws ArithmeticException when conversion would overflow.
+ */
+ private static Number performConversion(final Number a, final Class<?
extends Number> clazz) {
if (a.getClass().equals(clazz)) {
return a;
- } else if (clazz.equals(Integer.class)) {
- if (a.longValue() >= Integer.MIN_VALUE && a.longValue() <=
Integer.MAX_VALUE) {
+ }
+ if (clazz.equals(Integer.class)) {
+ Long val = longValue(a, clazz);
+ if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) {
return a.intValue();
}
} else if (clazz.equals(Long.class)) {
- return a.longValue();
+ return longValue(a, clazz);
} else if (clazz.equals(Float.class)) {
+ // BigDecimal or BigInteger to double can overflow into Infinity,
we want to prevent this
+ if (isInfinityOrNaN(a)) {
+ return a.floatValue();
+ }
if (a.doubleValue() >= -Float.MAX_VALUE && a.doubleValue() <=
Float.MAX_VALUE) {
return a.floatValue();
}
Review Comment:
I find it a bit odd that we throw exceptions for out of range float
conversions instead of letting it go to `(float) Infinity`. I don't want to
block this work if I'm the only one who feels this way (as this is a small edge
case), but I believe pushing to infinity is more aligned with IEEE 754.
> 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)