From 2e8529df9b56affa7a2a6f76e55d9c41816eb612 Mon Sep 17 00:00:00 2001 From: bestfeng1020 <31398465+bestfeng1020@users.noreply.github.com> Date: Tue, 13 Aug 2024 11:23:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E8=AE=A1=E7=AE=97=E5=B7=A5=E5=85=B7):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dfloat=E4=B8=8Eint=E8=AE=A1=E7=AE=97=E7=B2=BE?= =?UTF-8?q?=E5=BA=A6=E4=B8=A2=E5=A4=B1=E9=97=AE=E9=A2=98=20(#25)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reactor/ql/utils/CalculateUtils.java | 31 ++++++++++++++++--- .../reactor/ql/utils/CalculateUtilsTest.java | 1 + 2 files changed, 27 insertions(+), 5 deletions(-) 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);