diff --git a/src/main/java/org/jetlinks/reactor/ql/utils/CalculateUtils.java b/src/main/java/org/jetlinks/reactor/ql/utils/CalculateUtils.java index fbefab6..0b4cdb2 100644 --- a/src/main/java/org/jetlinks/reactor/ql/utils/CalculateUtils.java +++ b/src/main/java/org/jetlinks/reactor/ql/utils/CalculateUtils.java @@ -145,13 +145,17 @@ public static T calculate(Number left, if (right instanceof BigInteger) { return calculate((BigInteger) right, left, (r, l) -> opsForInteger.apply(l, r)); } - if(left instanceof Float && right instanceof Float){ - return opsForFloat.apply(left.floatValue(), right.floatValue()); - } - if (left instanceof Float || right instanceof Float || - left instanceof Double || right instanceof Double) { + if (left instanceof Double || right instanceof Double){ return opsForDouble.apply(left.doubleValue(), right.doubleValue()); } + if (left instanceof Float){ + return calculate((Float) left, right, opsForFloat, opsForDouble); + } + if (right instanceof Float){ + return calculate((Float) right, left, + (r, l) -> opsForFloat.apply(l, r), + (r, l) -> opsForDouble.apply(l, r)); + } return opsForLong.apply(left.longValue(), right.longValue()); } @@ -163,6 +167,15 @@ public static T calculate(BigDecimal left, Number right, if (right instanceof BigInteger) { return ops.apply(left, new BigDecimal((BigInteger) right)); } + if (right instanceof Float){ + return ops.apply(left, BigDecimal.valueOf(right.floatValue())); + } + if (right instanceof Integer){ + return ops.apply(left, BigDecimal.valueOf(right.intValue())); + } + if (right instanceof Long){ + return ops.apply(left, BigDecimal.valueOf(right.longValue())); + } return ops.apply(left, BigDecimal.valueOf(right.doubleValue())); } @@ -177,5 +190,13 @@ public static T calculate(BigInteger left, Number right, return ops.apply(left, BigInteger.valueOf(right.longValue())); } + public static T calculate(Float left, Number right, + BiFunction opsForFloat, + BiFunction opsForFloatDouble) { + if (right instanceof Float || right instanceof Integer) { + return opsForFloat.apply(left, right.floatValue()); + } + return opsForFloatDouble.apply(left.doubleValue(), right.doubleValue()); + } } diff --git a/src/test/java/org/jetlinks/reactor/ql/utils/CalculateUtilsTest.java b/src/test/java/org/jetlinks/reactor/ql/utils/CalculateUtilsTest.java index 64e5a78..b212c7c 100644 --- a/src/test/java/org/jetlinks/reactor/ql/utils/CalculateUtilsTest.java +++ b/src/test/java/org/jetlinks/reactor/ql/utils/CalculateUtilsTest.java @@ -54,6 +54,7 @@ void testSub() { @Test void testMultiply() { + assertEquals(CalculateUtils.multiply(2.1F, 10), 21F); assertEquals(CalculateUtils.multiply(2, 1), 2L); assertEquals(CalculateUtils.multiply(2F, 1F), 2F); assertEquals(CalculateUtils.multiply(2D, 1F), 2D);