Class Maths
public class Maths
extends java.lang.Object
-
Field Summary
Fields Modifier and Type Field Description static java.math.BigDecimalPI -
Constructor Summary
Constructors Constructor Description Maths() -
Method Summary
Modifier and Type Method Description static doubleacos(double p)We have code that computes the arctangent very accurately.static doubleacosDegToGrad(double angDeg)static doubleacosDegToRad(double angDeg)static doubleacosGradToDeg(double angGrad)static doubleacosGradToRad(double angGrad)static doubleacosh(double x)static doubleacosRadToDeg(double angRad)static doubleacosRadToGrad(double angRad)static doubleacoth(double x)static doubleacsch(double x)static java.lang.Stringadd(java.lang.String num1, int base1, java.lang.String num2, int base2, int resultbase)static doubleantiLogToAnyBase(double num, double base)static doubleasech(double x)static doubleasin(double q)We have code that computes the arctangent very accurately.static doubleasinDegToGrad(double angDeg)static doubleasinDegToRad(double angDeg)static doubleasinGradToDeg(double angGrad)static doubleasinGradToRad(double angGrad)static doubleasinh(double x)static doubleasinRadToDeg(double angRad)static doubleasinRadToGrad(double angRad)static doubleatan(double x)We use a = atan(x)....x = tan(a) Then approx...static doubleatanDegToGrad(double angDeg)static doubleatanDegToRad(double angDeg)static doubleatanGradToDeg(double angGrad)static doubleatanGradToRad(double angGrad)static doubleatanh(double x)static doubleatanRadToDeg(double angRad)static doubleatanRadToGrad(double angRad)static doublecbrt(double d)DEVELOPED FOR THE J2ME PLATFORM!static java.lang.StringchangeBase(java.lang.String num, java.lang.String num_base, java.lang.String base)Method changeBase is designed to give flexibility in converting from one base to another method changeBase takes 3 arguments,the number to be converted to base another base, the base system to which the number currently belongs,and the base to which the number is to be converted This method has the ability to convert a number in a specified base back to base 10 and so has the effect of reversing the action of method dec_to_other_base.static doublecosDegToGrad(double angGrad)static doublecosDegToRad(double angDeg)static doublecosGradToDeg(double angDeg)static doublecosGradToRad(double angGrad)static doublecosRadToDeg(double angRad)static doublecosRadToGrad(double angRad)static java.lang.Stringdec_to_other_base(java.lang.String dec_no, java.lang.String base_no)method dec_to_other_base takes 2 arguments,the decimal number to be converted, and the base to which the number is to be converted.static doubledegToGrad(double deg)static doubledegToRad(double deg)static java.lang.Stringdivide(java.lang.String num1, int base1, java.lang.String num2, int base2, int resultbase)static doubleexp(double x)Developed by JIBOYE Oluwagbemiro for the J2ME platform where no proper method for calculating the exponent of a number exists.static java.lang.Stringfact(java.lang.String p)static intgetExponent(double num)method getExponent returns the power to which 10 is raised when the number is written in standard form e.g an argument of 34.543 for the method gives a result of 1,since 34.543=3.4543*10^1static doublegradToDeg(double grad)static doublegradToRad(double grad)static doublelog(double x)Developed by JIBOYE Oluwagbemiro Olaoluwa for the J2ME platform where no proper method for calculating the natural logarithm of a number exists.static doublelogToAnyBase(double num, double base)static voidmain(java.lang.String[] args)static java.lang.Stringmultiply(java.lang.String num1, int base1, java.lang.String num2, int base2, int resultbase)static java.lang.Stringnon_exp_format(java.lang.String num)static java.lang.Stringnum_to_base_10(java.lang.String num, java.lang.String num_base)method num_to_base_10 takes 2 arguments,the number to be converted to base 10, and the base system to which the number currently belongs.static java.lang.StringPI()static doublepow(double number, double exponent)Developed by JIBOYE Oluwagbemiro for the J2ME platform where no proper method for calculating power exists.static doublepower(double number, double exponent)static doubleradToDeg(double rad)static doubleradToGrad(double rad)static java.util.List<java.lang.String>scanintoList(java.lang.String s)Method scanintoList is designed to scan a string of numbers separated by commas into a List serves to separate the individual number objects in a number stringstatic doublesinDegToGrad(double angGrad)static doublesinDegToRad(double angDeg)static doublesinGradToDeg(double angDeg)static doublesinGradToRad(double angGrad)static doublesinRadToDeg(double angRad)static doublesinRadToGrad(double angRad)protected static java.lang.Stringstd_form(java.lang.String num)static java.lang.Stringsubtract(java.lang.String num1, int base1, java.lang.String num2, int base2, int resultbase)static doubletanDegToGrad(double angGrad)static doubletanDegToRad(double angDeg)static doubletanGradToDeg(double angDeg)static doubletanGradToRad(double angGrad)static doubletanRadToDeg(double angRad)static doubletanRadToGrad(double angRad)
-
Field Details
-
PI
public static java.math.BigDecimal PI
-
-
Constructor Details
-
Maths
public Maths()
-
-
Method Details
-
PI
public static java.lang.String PI() -
fact
public static java.lang.String fact(java.lang.String p) -
getExponent
public static int getExponent(double num)method getExponent returns the power to which 10 is raised when the number is written in standard form e.g an argument of 34.543 for the method gives a result of 1,since 34.543=3.4543*10^1- Parameters:
num- the double number argument whose exponent is desire- Returns:
- the base 10 exponent of the number when written in standard form
-
logToAnyBase
public static double logToAnyBase(double num, double base) -
antiLogToAnyBase
public static double antiLogToAnyBase(double num, double base) -
std_form
protected static java.lang.String std_form(java.lang.String num) -
non_exp_format
public static java.lang.String non_exp_format(java.lang.String num)- Parameters:
num- The number to examine- Returns:
- the number as a non-exponential number.
-
dec_to_other_base
public static java.lang.String dec_to_other_base(java.lang.String dec_no, java.lang.String base_no)method dec_to_other_base takes 2 arguments,the decimal number to be converted, and the base to which the number is to be converted. This method has the ability to convert a decimal number to a specified base- Parameters:
dec_no- =the decimal number to be converted,base_no- =the base to which the number is to be converted.- Returns:
- the representation of the input decimal number in the specified base system.
-
num_to_base_10
public static java.lang.String num_to_base_10(java.lang.String num, java.lang.String num_base)method num_to_base_10 takes 2 arguments,the number to be converted to base 10, and the base system to which the number currently belongs. This method has the ability to convert a number in a specified base back to base 10 and so has the effect of reversing the action of method dec_to_other_base.- Parameters:
num- is the number to be converted to base 10num_base- is the base to which the number currently belongs- Returns:
- the decimal representation of the input.
-
changeBase
public static java.lang.String changeBase(java.lang.String num, java.lang.String num_base, java.lang.String base)Method changeBase is designed to give flexibility in converting from one base to another method changeBase takes 3 arguments,the number to be converted to base another base, the base system to which the number currently belongs,and the base to which the number is to be converted This method has the ability to convert a number in a specified base back to base 10 and so has the effect of reversing the action of method dec_to_other_base.- Parameters:
num- is the number to be converted to a new basenum_base- is the base to which the number currently belongsbase- is the base to which the number is to be converted.- Returns:
- the decimal representation of the input.
-
add
public static java.lang.String add(java.lang.String num1, int base1, java.lang.String num2, int base2, int resultbase)- Parameters:
num1- The first number.base1- The base system of the first number.num2- The second number.base2- The base system of the second number.resultbase- The base system of the result.- Returns:
- the sum of the 2 numbers in the target base system.
-
subtract
public static java.lang.String subtract(java.lang.String num1, int base1, java.lang.String num2, int base2, int resultbase)- Parameters:
num1- The first number.base1- The base system of the first number.num2- The second number.base2- The base system of the second number.resultbase- The base system of the result.- Returns:
- the difference of the 2 numbers in the target base system.
-
divide
public static java.lang.String divide(java.lang.String num1, int base1, java.lang.String num2, int base2, int resultbase)- Parameters:
num1- The first number.base1- The base system of the first number.num2- The second number.base2- The base system of the second number.resultbase- The base system of the result.- Returns:
- the division product of the 2 numbers in the target base system.
-
multiply
public static java.lang.String multiply(java.lang.String num1, int base1, java.lang.String num2, int base2, int resultbase)- Parameters:
num1- The first number.base1- The base system of the first number.num2- The second number.base2- The base system of the second number.resultbase- The base system of the result.- Returns:
- the product of the 2 numbers in the target base system.
-
scanintoList
public static java.util.List<java.lang.String> scanintoList(java.lang.String s)Method scanintoList is designed to scan a string of numbers separated by commas into a List serves to separate the individual number objects in a number string- Parameters:
s- is the string of numbers separated by commas- Returns:
- the List of scanned numbers
-
degToRad
public static double degToRad(double deg) -
radToDeg
public static double radToDeg(double rad) -
degToGrad
public static double degToGrad(double deg) -
gradToDeg
public static double gradToDeg(double grad) -
radToGrad
public static double radToGrad(double rad) -
gradToRad
public static double gradToRad(double grad) -
sinRadToDeg
public static double sinRadToDeg(double angRad)- Parameters:
angRad- the angle in rads- Returns:
- the sine of an angle in degrees
-
sinDegToRad
public static double sinDegToRad(double angDeg)- Parameters:
angDeg- the angle in degs- Returns:
- the sine of an angle in rads
-
sinRadToGrad
public static double sinRadToGrad(double angRad)- Parameters:
angRad- the angle in rads- Returns:
- the sine of an angle in grad
-
sinGradToRad
public static double sinGradToRad(double angGrad)- Parameters:
angGrad- the angle in grads- Returns:
- the sine of an angle in rad
-
sinDegToGrad
public static double sinDegToGrad(double angGrad)- Parameters:
angGrad- the angle in degs- Returns:
- the sine of an angle in grad
-
sinGradToDeg
public static double sinGradToDeg(double angDeg)- Parameters:
angDeg- the angle in degs- Returns:
- the sine of an angle in grad
-
cosRadToDeg
public static double cosRadToDeg(double angRad)- Parameters:
angRad- the angle in rads- Returns:
- the cosine of an angle in degrees
-
cosDegToRad
public static double cosDegToRad(double angDeg)- Parameters:
angDeg- the angle in degs- Returns:
- the cosine of an angle in rads
-
cosRadToGrad
public static double cosRadToGrad(double angRad)- Parameters:
angRad- the angle in rads- Returns:
- the cosine of an angle in grad
-
cosGradToRad
public static double cosGradToRad(double angGrad)- Parameters:
angGrad- the angle in grads- Returns:
- the cosine of an angle in rad
-
cosDegToGrad
public static double cosDegToGrad(double angGrad)- Parameters:
angGrad- the angle in degs- Returns:
- the cosine of an angle in grad
-
cosGradToDeg
public static double cosGradToDeg(double angDeg)- Parameters:
angDeg- the angle in degs- Returns:
- the cosine of an angle in grad
-
tanRadToDeg
public static double tanRadToDeg(double angRad)- Parameters:
angRad- the angle in rads- Returns:
- the tangent of an angle in degrees
-
tanDegToRad
public static double tanDegToRad(double angDeg)- Parameters:
angDeg- the angle in degs- Returns:
- the tangent of an angle in rads
-
tanRadToGrad
public static double tanRadToGrad(double angRad)- Parameters:
angRad- the angle in rads- Returns:
- the tangent of an angle in grad
-
tanGradToRad
public static double tanGradToRad(double angGrad)- Parameters:
angGrad- the angle in grads- Returns:
- the tangent of an angle in rad
-
tanDegToGrad
public static double tanDegToGrad(double angGrad)- Parameters:
angGrad- the angle in degs- Returns:
- the tangent of an angle in grad
-
tanGradToDeg
public static double tanGradToDeg(double angDeg)- Parameters:
angDeg- the angle in degs- Returns:
- the tangent of an angle in grad
-
atanRadToDeg
public static double atanRadToDeg(double angRad)- Parameters:
angRad- the angle in rads- Returns:
- the arctan of an angle in degrees
-
acosRadToDeg
public static double acosRadToDeg(double angRad)- Parameters:
angRad- the angle in rads- Returns:
- the arccos of an angle in degrees
-
asinRadToDeg
public static double asinRadToDeg(double angRad)- Parameters:
angRad- the angle in rads- Returns:
- the arcsine of an angle in degrees
-
asinDegToRad
public static double asinDegToRad(double angDeg)- Parameters:
angDeg- the angle in rads- Returns:
- the arcsine of an angle in rads
-
asinRadToGrad
public static double asinRadToGrad(double angRad)- Parameters:
angRad- the angle in rads- Returns:
- the arcsine of an angle in grads
-
asinGradToRad
public static double asinGradToRad(double angGrad)- Parameters:
angGrad- the angle in rads- Returns:
- the arcsine of an angle in rads
-
asinDegToGrad
public static double asinDegToGrad(double angDeg)- Parameters:
angDeg- the angle in rads- Returns:
- the arcsine of an angle in grads
-
asinGradToDeg
public static double asinGradToDeg(double angGrad)- Parameters:
angGrad- the angle in rads- Returns:
- the arcsine of an angle in degrees
-
acosDegToRad
public static double acosDegToRad(double angDeg)- Parameters:
angDeg- the angle in rads- Returns:
- the arccos of an angle in rads
-
acosRadToGrad
public static double acosRadToGrad(double angRad)- Parameters:
angRad- the angle in rads- Returns:
- the arccos of an angle in grads
-
acosGradToRad
public static double acosGradToRad(double angGrad)- Parameters:
angGrad- the angle in rads- Returns:
- the arccos of an angle in rads
-
acosDegToGrad
public static double acosDegToGrad(double angDeg)- Parameters:
angDeg- the angle in rads- Returns:
- the arccos of an angle in grads
-
acosGradToDeg
public static double acosGradToDeg(double angGrad)- Parameters:
angGrad- the angle in rads- Returns:
- the arccos of an angle in degrees
-
atanDegToRad
public static double atanDegToRad(double angDeg)- Parameters:
angDeg- the angle in rads- Returns:
- the arctan of an angle in rads
-
atanRadToGrad
public static double atanRadToGrad(double angRad)- Parameters:
angRad- the angle in rads- Returns:
- the arctan of an angle in grads
-
atanGradToRad
public static double atanGradToRad(double angGrad)- Parameters:
angGrad- the angle in rads- Returns:
- the arctan of an angle in rads
-
atanDegToGrad
public static double atanDegToGrad(double angDeg)- Parameters:
angDeg- the angle in rads- Returns:
- the arctan of an angle in grads
-
atanGradToDeg
public static double atanGradToDeg(double angGrad)- Parameters:
angGrad- the angle in rads- Returns:
- the arctan of an angle in degrees
-
asinh
public static double asinh(double x)- Parameters:
x- The number.- Returns:
- the arc sinh of the number.
-
acosh
public static double acosh(double x)- Parameters:
x- The number.- Returns:
- the arc sinh of the number.
-
atanh
public static double atanh(double x)- Parameters:
x- The number.- Returns:
- the arc sinh of the number.
-
acsch
public static double acsch(double x)- Parameters:
x- The number.- Returns:
- the arc csch of the number.
-
asech
public static double asech(double x)- Parameters:
x- The number.- Returns:
- the arc csch of the number.
-
acoth
public static double acoth(double x)- Parameters:
x- The number.- Returns:
- the arc coth of the number.
-
power
public static double power(double number, double exponent)- Parameters:
number- The numberexponent- The power. May be integer or floating point.- Returns:
- The number parameter raised to the power of the exponent parameter.
-
cbrt
public static double cbrt(double d)DEVELOPED FOR THE J2ME PLATFORM!- Parameters:
d- The number- Returns:
- the cube root.
-
pow
public static double pow(double number, double exponent)Developed by JIBOYE Oluwagbemiro for the J2ME platform where no proper method for calculating power exists. It also works on the J2SE platform.A very high speed method that finds the result when the first argument is raised to the power of the second. But the power must always be a whole number. The method could be as much as 4-5 times faster than the Math.pow method of Java's Math class and should be used whenever the power is non-fractional.
- Parameters:
number- The number to raise to a power.exponent- The power it is to be raised to.- Returns:
- the result of number raised to the power of exponent.
-
log
public static double log(double x)Developed by JIBOYE Oluwagbemiro Olaoluwa for the J2ME platform where no proper method for calculating the natural logarithm of a number exists. It also works on the J2SE platform, but is about 5 times slower than the its counterpart in class Math.- Parameters:
x- The number whose natural logarithm is needed.- Returns:
- the natural logarithm of the number using the
relation: log(x) = 2*(p+p^3/3+p^5/5+p^7/7+...) where p = (x-1)/(x+1)
This formula runs fastest at values of x very close to 1. e.g: 0.9<e;x<e;1.1 ,the accuracy is also highest for this range.
For this reason the algorithm uses a repeated-square-root deduction process to force all numbers to fall into the specified range before applying the log-series formula above. The output of this step is , say
m = log(reduced_sqrt)It then retrieves the number of times it used the square root to force the number into the range.. say this number isn, and findsN=2^n. The log is thenm*N
-
exp
public static double exp(double x)Developed by JIBOYE Oluwagbemiro for the J2ME platform where no proper method for calculating the exponent of a number exists. It runs on the J2SE platform, too and is only slightly slower than its counterpart in class Math.- Parameters:
x- The number whose exponent is needed.- Returns:
- the exponent of the number.
-
asin
public static double asin(double q)We have code that computes the arctangent very accurately. The same principles applied for the arctan do not work well for arcsin and arccos throughout the whole range -1≤x≤1. So we use the relationship between arctan,arccos and arcsin to compute them. This is it: arctan(x)=arccos(1/sqrt(1+x^2))=arcsin(x/sqrt(1+x^2)) So arctan(x)=arccos(p)=arcsin(q) where p = 1/sqrt(1+x^2) and so x = sqrt(1-p^2)/p and q = x/sqrt(1+x^2) and so x = q/sqrt(1-q^2)So for example to compute arccos(0.5): Then p = 0.5 and to use arctan(x) to compute it, we convert p to x coordinates using x = sqrt(1-p^2)/p. So x = sqrt(1-0.5^2)/0.5 = sqrt(3) Then arccos(0.5) = arctan(sqrt(3))
For arcsin, the same thing applies.
So for example to compute arcsin(0.5): Then q = 0.5 and to use arctan(x) to compute it, we convert q to x coordinates using x = q/sqrt(1-q^2) So x = 0.5/sqrt(1-0.5^2) = sqrt(3) Then arcsin(0.5) = arctan(1/sqrt(3))
- Parameters:
q- The number.- Returns:
- the arc sine of the number.
-
acos
public static double acos(double p)We have code that computes the arctangent very accurately. The same principles applied for the arctan do not work well for arcsin and arccos throughout the whole range -1≤x≤1. So we use the relationship between arctan,arccos and arcsin to compute them. This is it: arctan(x)=arccos(1/sqrt(1+x^2))=arcsin(x/sqrt(1+x^2)) So arctan(x)=arccos(p)=arcsin(q) where p = 1/sqrt(1+x^2) and so x = sqrt(1-p^2)/p and q = x/sqrt(1+x^2) and so x = q/sqrt(1-q^2)So for example to compute arccos(0.5): Then p = 0.5 and to use arctan(x) to compute it, we convert p to x coordinates using x = sqrt(1-p^2)/p. So x = sqrt(1-0.5^2)/0.5 = sqrt(3) Then arccos(0.5) = arctan(sqrt(3))
For arcsin, the same thing applies.
So for example to compute arcsin(0.5): Then q = 0.5 and to use arctan(x) to compute it, we convert q to x coordinates using x = q/sqrt(1-q^2) So x = 0.5/sqrt(1-0.5^2) = sqrt(3) Then arcsin(0.5) = arctan(1/sqrt(3))
- Parameters:
p- The number.- Returns:
- the arc cosine of the number.
-
atan
public static double atan(double x)We use a = atan(x)....x = tan(a) Then approx... a = x-x^3/3+x^5/5-x^7/7+x^9/9..... Then use the approx value for a in the Newton-Raphson formula to get a very accurate value for a. i.e x_n1 = x_n - f(x_n)/f'(x_n)- Parameters:
x-- Returns:
-
main
public static void main(java.lang.String[] args)
-