Skip to content

Commit

Permalink
Adding more arithmetic functions (apache#14671)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangfu0 authored Dec 22, 2024
1 parent 616d691 commit becb57e
Show file tree
Hide file tree
Showing 2 changed files with 429 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,59 @@ public static double divide(double a, double b, double defaultValue) {
return (b == 0) ? defaultValue : a / b;
}

@ScalarFunction
public static long intDiv(double a, double b) {
return (long) Math.floor(a / b);
}

@ScalarFunction
public static long intDivOrZero(double a, double b) {
//Same as intDiv but returns zero when dividing by zero or when dividing a minimal negative number by minus one.
return (b == 0 || (a == Long.MIN_VALUE && b == -1)) ? 0 : intDiv(a, b);
}

@ScalarFunction
public static int isFinite(double value) {
return Double.isFinite(value) ? 1 : 0;
}

@ScalarFunction
public static int isInfinite(double value) {
return Double.isInfinite(value) ? 1 : 0;
}

@ScalarFunction
public static double ifNotFinite(double valueToCheck, double defaultValue) {
return Double.isFinite(valueToCheck) ? valueToCheck : defaultValue;
}

@ScalarFunction
public static int isNaN(double value) {
return Double.isNaN(value) ? 1 : 0;
}

@ScalarFunction
public static double mod(double a, double b) {
return a % b;
}

@ScalarFunction
public static double moduloOrZero(double a, double b) {
//Same as mod but returns zero when dividing by zero or when dividing a minimal negative number by minus one.
return (b == 0 || (a == Long.MIN_VALUE && b == -1)) ? 0 : mod(a, b);
}

@ScalarFunction
public static double positiveModulo(double a, double b) {
double result = a % b;
return result >= 0 ? result : result + Math.abs(b);
}

@ScalarFunction
public static double negate(double a) {
return -a;
}

@ScalarFunction
public static double least(double a, double b) {
return Double.min(a, b);
Expand Down Expand Up @@ -117,7 +165,6 @@ public static double power(double a, double exponent) {
return Math.pow(a, exponent);
}


// Big Decimal Implementation has been used here to avoid overflows
// when multiplying by Math.pow(10, scale) for rounding
@ScalarFunction
Expand All @@ -143,4 +190,33 @@ public static double truncate(double a, int scale) {
public static double truncate(double a) {
return Math.signum(a) * Math.floor(Math.abs(a));
}

@ScalarFunction
public static long gcd(long a, long b) {
return a == 0 ? Math.abs(b) : gcd(b % a, a);
}

@ScalarFunction
public static long lcm(long a, long b) {
if (a == 0 || b == 0) {
return 0;
}
return Math.abs(a) / gcd(a, b) * Math.abs(b);
}

@ScalarFunction
public static double hypot(double a, double b) {
return Math.hypot(a, b);
}

@ScalarFunction
public static int byteswapInt(int a) {
return Integer.reverseBytes(a);
}

@ScalarFunction
public static long byteswapLong(long a) {
// Skip the heading 0s in the long value
return Long.reverseBytes(a);
}
}
Loading

0 comments on commit becb57e

Please sign in to comment.