Class Maths

java.lang.Object
math.Maths

public class Maths extends Object
  • Field Details

  • Constructor Details

    • Maths

      public Maths()
  • Method Details

    • PI

      public static String PI()
    • fact

      public static String fact(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)
    • sign

      private static String sign(String num)
    • hasPoint

      private static boolean hasPoint(String num)
    • hasExponent

      private static boolean hasExponent(String num)
    • isNegNumber

      private static boolean isNegNumber(String num)
    • hasNegExponent

      private static boolean hasNegExponent(String num)
    • hasPosExponent

      private static boolean hasPosExponent(String num)
    • hasUnsignedExp

      private static boolean hasUnsignedExp(String num)
    • abs_val

      private static String abs_val(String num)
    • getNumbersBeforePoint

      private static String getNumbersBeforePoint(String num)
    • getNumbersAfterPoint

      private static String getNumbersAfterPoint(String num)
    • getNumbersAfterExp

      private static String getNumbersAfterExp(String num)
    • generateZeroes

      private static String generateZeroes(int n)
    • getNumberOfLeadingZeroes

      private static int getNumberOfLeadingZeroes(String num)
    • std_form

      protected static String std_form(String num)
    • non_exp_format

      public static String non_exp_format(String num)
      Parameters:
      num - The number to examine
      Returns:
      the number as a non-exponential number.
    • whole_dec_to_other_base

      private static String whole_dec_to_other_base(String dec_no, 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 the whole part of 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.
    • frac_dec_to_other_base

      private static String frac_dec_to_other_base(String dec_no, String base_no)
      method frac_dec_to_other_base takes 2 arguments,the decimal number to be converted, and the base to which the number is to be converted. It however deals with positive decimals less than 1 and greater than 0. 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.
    • dec_to_other_base

      public static String dec_to_other_base(String dec_no, 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 String num_to_base_10(String num, 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 10
      num_base - is the base to which the number currently belongs
      Returns:
      the decimal representation of the input.
    • changeBase

      public static String changeBase(String num, String num_base, 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 base
      num_base - is the base to which the number currently belongs
      base - is the base to which the number is to be converted.
      Returns:
      the decimal representation of the input.
    • add

      public static String add(String num1, int base1, 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 String subtract(String num1, int base1, 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 String divide(String num1, int base1, 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 String multiply(String num1, int base1, 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 List<String> scanintoList(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 number
      exponent - 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 is n, and finds N=2^n. The log is then m*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(String[] args)