Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more arithmetic scalar functions #14671

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading