private static Number mathOperationWithPromote(final Function<NumberHelper, BiFunction<Number, Number, Number>> mathFunction, final boolean forceFloatingPoint, final Number a, final Number b) {
        if (null == a || null == b) return a;
        NumberInfo numberInfo = getHighestCommonNumberInfo(forceFloatingPoint, a, b);
        Number result = 0;
        while (true) {
            try {
                final Class<? extends Number> clazz = determineNumberClass(numberInfo.getBits(), numberInfo.getFp());
                final NumberHelper helper = getHelper(clazz);
                result = mathFunction.apply(helper).apply(a, b);
                if (result instanceof BigInteger || result instanceof BigDecimal)
                {
                    return result;
                }
                if (Double.isInfinite(result.doubleValue()))
                {
                    throw new ArithmeticException("Floating point overflow detected");
                }
                return result;
            } catch (ArithmeticException exception) {
                if (!numberInfo.getFp() && numberInfo.getBits() >= 64) {
                    throw exception;
                } else if (numberInfo.getFp() && numberInfo.getBits() >= 64) {
                    return result;
                }
                numberInfo.promoteBits();
            }
        }
    }
@author Daniel Kuppitz (http://gremlin.guru)
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/NumberHelper.java