//*********************************************************************************
// class tig.maths.Maths
// Software released under the General Public License (version 2 or later), available at
// http://www.gnu.org/copyleft/gpl.html
//*********************************************************************************
package tig.maths;
/**********************************************************************************
Abstract class containing mathematical constants and static methods.
@author Thierry Graff
@history jul 07 1998 : creation
@todo truncate constants to a useful number of decimals.
**********************************************************************************/
public abstract class Maths{
//=================================================================================
// CONSTANTS
//=================================================================================
/** Constant whose value is 2 x <FONT FACE="symbol">p</FONT>. */
public static final double TWOPI = 6.28318530717958647688;
/** Constant whose value is ln(10). */
public static final double ln10 = 2.30258509299404568401799145468436;
/** Constant to convert arc seconds to radians, whose value is <FONT FACE="symbol">p</FONT> / (180 * 3600). */
public static final double ARCSEC_TO_RAD = 4.84813681109535993589914102357948e-6;
/** Constant to convert radians to arc seconds, whose value is 180 * 3600 / <FONT FACE="symbol">p</FONT>. */
public static final double RAD_TO_ARCSEC = 206264.806247096355156473357330779;
//=================================================================================
// METHODS
//=================================================================================
//*************************** mod360(double) ******************************
/** Reduction of a number in the interval [0, 360].
@param nb any number.
@return <CODE>nb</CODE> modulo <CODE>360</CODE>.
*/
public static double mod360(double nb){
while (nb > 360.0) nb -= 360.0;
while (nb < 0.0) nb += 360.0;
return nb;
}// end mod360(double)
//*************************** mod360(int) **********************************
/** Reduction of a number in the interval [0, 360], for an integer.
@param nb any number.
@return <CODE>nb</CODE> modulo <CODE>360</CODE>.
*/
public static int mod360(int nb){
return (int)(mod360((double)nb));
}// end mod360(int)
//*************************** modX() ***************************************
/** Reduction of a number in the interval [0, limit].
@param nb any number.
@param limit any positive number.
@return <CODE>nb</CODE> modulo <CODE>limit</CODE>.
*/
public static double modX(double nb, double limit){
if (nb>0) return nb - Math.floor(nb/limit)*limit;
else return nb + Math.ceil(nb/limit)*limit;
}//end modX()
//*************************** atan3 ***********************************
/** Computation of arcTangent, giving a result in [0, 2*<FONT FACE="symbol">p</FONT>[.
@return A number <CODE>alpha</CODE> such as <CODE>cos(alpha) = x/sqrt(x*x + y*y)</CODE> and
<CODE>sin(alpha) = y/sqrt(x*x + y*y)</CODE> and alpha belongs to [0, 2*<FONT FACE="symbol">p</FONT>[.
*/
public static double atan3(double y, double x){
double alpha = Math.atan2(y, x); // belongs to [-PI, PI[
if (alpha >= 0)
return alpha;
else
return alpha + 2*Math.PI;
}// end atan3
//*************************** log10 ***********************************
/** Computes the 10 based logarithm of a number.
@return 10 based Logarithm of <CODE>x</CODE>.
*/
public static double log10(double x){
return Math.log(x)/ln10;
}// end log10
//*************************** max(int) ***********************************
/** Returns the greatest integer of an array. */
// Maybe could be optimized...
public static int max(int[] a){
if (a == null) throw new IllegalArgumentException("paramater 'a' can't be null");
int n = a.length;
int max = a[0];
for(int i = 1; i < n; i++){
if(a[i] > max) max = a[i];
}
return max;
}// end max(int)
//*************************** max(double) ***********************************
/** Returns the greatest double of an array. */
// Maybe could be optimized...
public static double max(double[] a){
if (a == null) throw new IllegalArgumentException("paramater 'a' can't be null");
int n = a.length;
double max = a[0];
for(int i = 1; i < n; i++){
if(a[i] > max) max = a[i];
}
return max;
}// end max
//*************************** min(double) ***********************************
/** Returns the smallest double of an array. */
// Maybe could be optimized...
public static double min(double[] a){
if (a == null) throw new IllegalArgumentException("paramater 'a' can't be null");
int n = a.length;
double min = a[0];
for(int i = 1; i < n; i++){
if(a[i] < min) min = a[i];
}
return min;
}// end max
//=================================================================================
//=================================================================================
// TESTS
//=================================================================================
//=================================================================================
/*
// **************** For tests only ****************
//D:\Programs\java\jdk1.4\bin\java -classpath .;bin tig.maths.Maths testMax "15, 18, 65, 5, 8"
public static void main(String[] args){
// no complete argument checking
if(args[0].equalsIgnoreCase("testMax"))
testMax(args[1]);
else
System.out.println("first argument must be 'testMax'");
}// end main
// **************** For tests only ****************
private static void testMax(String args){
// parse args (exclude first element, equal to "testMax")
int a[] = tig.Strings.stringToIntArray(args);
int max = max(a);
System.out.println("max = " + max);
}
*/
}//end class Maths