[
https://issues.apache.org/jira/browse/TINKERPOP-3166?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18010774#comment-18010774
]
ASF GitHub Bot commented on TINKERPOP-3166:
-------------------------------------------
andreachild commented on code in PR #3153:
URL: https://github.com/apache/tinkerpop/pull/3153#discussion_r2241309967
##########
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/NumberHelper.java:
##########
@@ -717,8 +764,30 @@ public static Number coerceTo(final Number a, final
Class<? extends Number> claz
throw new IllegalArgumentException("Unsupported numeric type: " +
clazz);
}
- // return as-is since it didn't fit the type we wanted to coerce to
- return a;
+ throw new ArithmeticException(String.format("Can't convert number of
type %s to %s due to overflow.",
+ a.getClass().getSimpleName(), clazz.getSimpleName()));
+ }
+
+ private static Long getLong(final Number num, final Class<? extends
Number> clazz) {
+ // 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.", clazz.getSimpleName()));
+ }
+ if (Double.isInfinite(num.doubleValue())) {
+ throw new ArithmeticException(String.format("Can't convert
floating point infinity to %s.", clazz.getSimpleName()));
+ }
+ String msg = String.format("Can't convert number of type %s to %s due
to overflow.",
+ num.getClass().getSimpleName(), clazz.getSimpleName());
+ if ((num.getClass().equals(Double.class) ||
num.getClass().equals(Float.class)) && (num.doubleValue()) > Long.MAX_VALUE) {
Review Comment:
Do we also need to check for underflow?
> 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)