Skip to content

Commit

Permalink
refactor: 数字计算增加float操作
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Aug 1, 2024
1 parent 6e20b0e commit 75c84ff
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/org/jetlinks/reactor/ql/utils/CalculateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static Number bitAnd(Number left, Number right) {
return calculate(left, right,
(l, r) -> l & r,
(l, r) -> l.longValue() & r.longValue(),
(l, r) -> l.longValue() & r.longValue(),
(l, r) -> l.toBigInteger().and(r.toBigInteger()),
BigInteger::and);
}
Expand All @@ -18,6 +19,7 @@ public static Number bitOr(Number left, Number right) {
return calculate(left, right,
(l, r) -> l | r,
(l, r) -> l.longValue() | r.longValue(),
(l, r) -> l.longValue() | r.longValue(),
(l, r) -> l.toBigInteger().or(r.toBigInteger()),
BigInteger::or);
}
Expand All @@ -26,6 +28,7 @@ public static Number bitMutex(Number left, Number right) {
return calculate(left, right,
(l, r) -> l ^ r,
(l, r) -> l.longValue() ^ r.longValue(),
(l, r) -> l.longValue() ^ r.longValue(),
(l, r) -> l.toBigInteger().xor(r.toBigInteger()),
BigInteger::xor);
}
Expand All @@ -36,6 +39,7 @@ public static int bitCount(Number left) {
left, left,
(l, r) -> Long.bitCount(l),
(l, r) -> Long.bitCount(l.longValue()),
(l, r) -> Long.bitCount(l.longValue()),
(l, r) -> l.toBigInteger().bitCount(),
(l, r) -> l.bitCount());
}
Expand All @@ -46,6 +50,7 @@ public static Number leftShift(Number left, Number right) {
left, right,
(l, r) -> l << r,
(l, r) -> l.longValue() << r.longValue(),
(l, r) -> l.longValue() << r.longValue(),
(l, r) -> l.toBigInteger().shiftLeft(r.intValue()),
(l, r) -> l.shiftLeft(r.intValue()));
}
Expand All @@ -60,6 +65,7 @@ public static Number rightShift(Number left, Number right) {
left, right,
(l, r) -> l >> r,
(l, r) -> l.longValue() >> r.longValue(),
(l, r) -> l.longValue() >> r.longValue(),
(l, r) -> l.toBigInteger().shiftRight(r.intValue()),
(l, r) -> l.shiftRight(r.intValue()));
}
Expand All @@ -68,13 +74,15 @@ public static Number bitNot(Number left) {
return calculate(left, left,
(l, r) -> ~l,
(l, r) -> ~l.longValue(),
(l, r) -> ~l.longValue(),
(l, r) -> l.toBigInteger().not(),
(l, r) -> l.not());
}

public static Number mod(Number left, Number right) {

return calculate(left, right,
(l, r) -> l % r,
(l, r) -> l % r,
(l, r) -> l % r,
BigDecimal::remainder,
Expand All @@ -84,6 +92,7 @@ public static Number mod(Number left, Number right) {
public static Number division(Number left, Number right) {

return calculate(left, right,
(l, r) -> l / r,
(l, r) -> l / r,
(l, r) -> l / r,
BigDecimal::divide,
Expand All @@ -92,6 +101,7 @@ public static Number division(Number left, Number right) {

public static Number multiply(Number left, Number right) {
return calculate(left, right,
(l, r) -> l * r,
(l, r) -> l * r,
(l, r) -> l * r,
BigDecimal::multiply,
Expand All @@ -101,13 +111,15 @@ public static Number multiply(Number left, Number right) {
public static Number add(Number left, Number right) {
return calculate(left, right,
Long::sum,
Float::sum,
Double::sum,
BigDecimal::add,
BigInteger::add);
}

public static Number subtract(Number left, Number right) {
return calculate(left, right,
(l, r) -> l - r,
(l, r) -> l - r,
(l, r) -> l - r,
BigDecimal::subtract,
Expand All @@ -117,6 +129,7 @@ public static Number subtract(Number left, Number right) {
public static <T> T calculate(Number left,
Number right,
BiFunction<Long, Long, T> opsForLong,
BiFunction<Float, Float, T> opsForFloat,
BiFunction<Double, Double, T> opsForDouble,
BiFunction<BigDecimal, BigDecimal, T> opsForDecimal,
BiFunction<BigInteger, BigInteger, T> opsForInteger) {
Expand All @@ -132,6 +145,9 @@ public static <T> 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) {
return opsForDouble.apply(left.doubleValue(), right.doubleValue());
Expand Down

0 comments on commit 75c84ff

Please sign in to comment.