Java "ComputeCompare"

Admin User, erstellt 11. Apr. 2024
         
package nova.envir;
import java.math.BigInteger;
/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
* <p/>
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
* <p/>
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
* <p/>
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
* <p/>
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
public final class ComputeCompare {
public final static BigInteger MIN_LONG = BigInteger.valueOf(Long.MIN_VALUE);
public final static BigInteger MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE);
/**
* <p>Check whether two Prolog numbers are equal.</p>
*
* @param m The first Prolog number.
* @param n The second Prolog number.
* @return True if they are equal, false otherwise.
*/
public static boolean testEq(Number m, Number n) {
switch (Math.max(ComputeElem.numType(m), ComputeElem.numType(n))) {
case ComputeElem.NUM_INTEGER:
case ComputeElem.NUM_BIG_INTEGER:
return m.equals(n);
case ComputeElem.NUM_DOUBLE:
return ComputeElem.narrow_float(m)
== ComputeElem.narrow_float(n);
default:
throw new UnsupportedOperationException(ComputeElem.OP_PERMISSION_NOT_SUPPORTED);
}
}
/**
* <p>Compare two Prolog numbers.</p>
*
* @param m The first Prolog number.
* @param n The second Prolog number.
* @return <0 m < n, 0 m == m, >0 m > n.
*/
public static int computeCmp(Number m, Number n) {
switch (Math.max(ComputeElem.numType(m), ComputeElem.numType(n))) {
case ComputeElem.NUM_INTEGER:
case ComputeElem.NUM_BIG_INTEGER:
return ComputeCompare.integer_compare(m, n);
case ComputeElem.NUM_DOUBLE:
double a = ComputeElem.narrow_float(m);
double b = ComputeElem.narrow_float(n);
return Double.compare(a, b);
default:
throw new UnsupportedOperationException(ComputeElem.OP_PERMISSION_NOT_SUPPORTED);
}
}
/**
* <p>Compare two Prolog integers lexically.</p>
*
* @param alfa The first Prolog integer.
* @param beta The second Prolog integer.
* @return <0 alfa < beta, 0 alfa = beta, >0 alfa > beta
*/
public static int integer_compare(Number alfa, Number beta) {
if (alfa instanceof Integer) {
if (beta instanceof Integer) {
return ((Integer) alfa).compareTo((Integer) beta);
} else {
return -((BigInteger) beta).signum();
}
} else {
if (beta instanceof Integer) {
return ((BigInteger) alfa).signum();
} else {
return ((BigInteger) alfa).compareTo((BigInteger) beta);
}
}
}
/**
* Determine the order value of a Prolog reference.
*
* @param first The Prolog reference.
* @return number The order value.
*/
public static int order_value(Object first) {
if (first == Boolean.FALSE) {
return 0;
} else if (first == Boolean.TRUE) {
return 1;
} else if (first == null) {
return -1;
} else {
throw new UnsupportedOperationException(ComputeElem.OP_PERMISSION_NOT_SUPPORTED);
}
}
/********************************************************************/
/* Binary Operations: */
/* (min)/2: min() */
/* (max)/2: max() */
/********************************************************************/
/**
* <p>Min the two number.</p>
*
* @param m The first number.
* @param n The second number.
* @return The minimum of the two numbers.
*/
public static Number min(Number m, Number n) {
switch (Math.max(ComputeElem.numType(m), ComputeElem.numType(n))) {
case ComputeElem.NUM_INTEGER:
case ComputeElem.NUM_BIG_INTEGER:
if (integer_compare(m, n) > 0) {
return n;
} else {
return m;
}
case ComputeElem.NUM_DOUBLE:
double a = ComputeElem.narrow_float(m);
double b = ComputeElem.narrow_float(n);
if (a > b) {
m = n;
a = b;
}
return (m instanceof Double ? m : Double.valueOf(a));
default:
throw new UnsupportedOperationException(ComputeElem.OP_PERMISSION_NOT_SUPPORTED);
}
}
/**
* <p>Max the two number.</p>
*
* @param m The first number.
* @param n The second number.
* @return The minimum of the two numbers.
*/
public static Number max(Number m, Number n) {
switch (Math.max(ComputeElem.numType(m), ComputeElem.numType(n))) {
case ComputeElem.NUM_INTEGER:
case ComputeElem.NUM_BIG_INTEGER:
if (integer_compare(m, n) < 0) {
return n;
} else {
return m;
}
case ComputeElem.NUM_DOUBLE:
double a = ComputeElem.narrow_float(m);
double b = ComputeElem.narrow_float(n);
if (a < b) {
m = n;
a = b;
}
return (m instanceof Double ? m : Double.valueOf(a));
default:
throw new UnsupportedOperationException(ComputeElem.OP_PERMISSION_NOT_SUPPORTED);
}
}
/********************************************************************/
/* Some Convenience */
/********************************************************************/
/**
* Return the signum of this combined integer.
*
* @param num The integer.
* @return The signum.
*/
public static int integer_signum(Number num) {
if (num instanceof Integer) {
return Integer.signum(num.intValue());
} else {
return ((BigInteger) num).signum();
}
}
/**
* Check whether this combined integer is a long.
*
* @param num The combine integer.
* @return True if it is a long, otherwise false.
*/
public static boolean is_long(Number num) {
if (num instanceof Integer) {
return true;
} else {
return MIN_LONG.compareTo((BigInteger) num) <= 0 &&
((BigInteger) num).compareTo(MAX_LONG) <= 0;
}
}
}