Fixed small warning

This commit is contained in:
Ziver Koc 2017-11-01 16:25:58 +01:00
parent 8455ba5a43
commit c344856989

118
src/zutil/math/ZMath.java Normal file → Executable file
View file

@ -33,69 +33,69 @@ import java.math.BigInteger;
*/ */
public class ZMath { public class ZMath {
/** /**
* Calculates the percentage of the values * Calculates the percentage of the values
*/ */
public static double percent(int min, int max, int value){ public static double percent(int min, int max, int value){
return ((double)(value-min)/(max-min))*100; return ((double)(value-min)/(max-min))*100;
} }
/** /**
* Solves the equation: x^2 + px + q = 0 * Solves the equation: x^2 + px + q = 0
* *
* @return the two values of x as an array * @return the two values of x as an array
*/ */
public static double[] pqFormula(double p, double q){ public static double[] pqFormula(double p, double q){
double[] ret = new double[2]; double[] ret = new double[2];
double t = (p/2); double t = (p/2);
ret[0] = Math.sqrt( t*t - q ); ret[0] = Math.sqrt( t*t - q );
ret[1] = -ret[0]; ret[1] = -ret[0];
t *= -1; t *= -1;
ret[0] += t; ret[0] += t;
ret[1] += t; ret[1] += t;
return ret; return ret;
} }
/** /**
* Solves the equation: x^2 + px + q = 0. * Solves the equation: x^2 + px + q = 0.
* WARNING: This uses only BigInteger, thereby removing the decimals in the calculation * WARNING: This uses only BigInteger, thereby removing the decimals in the calculation
* *
* @return the two values of x as an array * @return the two values of x as an array
*/ */
public static BigInteger[] pqFormula(BigInteger p, BigInteger q){ public static BigInteger[] pqFormula(BigInteger p, BigInteger q){
BigInteger[] ret = new BigInteger[2]; BigInteger[] ret = new BigInteger[2];
BigInteger t = p.divide( BigInteger.valueOf(2) ); BigInteger t = p.divide( BigInteger.valueOf(2) );
ret[0] = ZMath.sqrt( t.multiply( t ).subtract( q ) ); ret[0] = sqrt( t.multiply( t ).subtract( q ) );
ret[1] = ret[0].negate(); ret[1] = ret[0].negate();
t = t.negate(); t = t.negate();
ret[0] = ret[0].add( t ); ret[0] = ret[0].add( t );
ret[1] = ret[1].add( t ); ret[1] = ret[1].add( t );
return ret; return ret;
} }
/** /**
* Calculates the square root of a big number * Calculates the square root of a big number
* *
*/ */
public static BigInteger sqrt(BigInteger value){ public static BigInteger sqrt(BigInteger value){
BigInteger op = value; BigInteger op = value;
BigInteger res = BigInteger.ZERO; BigInteger res = BigInteger.ZERO;
BigInteger one = BigInteger.ONE; BigInteger one = BigInteger.ONE;
while( one.compareTo( op ) < 0 ){ while( one.compareTo( op ) < 0 ){
one = one.shiftLeft( 2 ); one = one.shiftLeft( 2 );
} }
one = one.shiftRight(2); one = one.shiftRight(2);
while( !one.equals( BigInteger.ZERO ) ){ while( !one.equals( BigInteger.ZERO ) ){
if( op.compareTo( res.add( one ) ) >= 0 ){ if( op.compareTo( res.add( one ) ) >= 0 ){
op = op.subtract( res.add( one ) ); op = op.subtract( res.add( one ) );
res = res.add( one.shiftLeft( 1 ) ); res = res.add( one.shiftLeft( 1 ) );
} }
res = res.shiftRight( 1 ); res = res.shiftRight( 1 );
one = one.shiftRight( 2 ); one = one.shiftRight( 2 );
} }
return res; return res;
} }
} }