Class RhinoMath

java.lang.Object
io.higson.runtime.rhino.RhinoMath

public class RhinoMath extends Object
Utility class providing mathematical functions which can be used within Higson functions
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Euler's number e=2.718..., base of natural logarithm
    static final RhinoMath
     
    static final double
    Number π = 3.14...
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    abs(double a)
    Returns absolute value of the argument using standard Java function Math.abs(double).
    static float
    abs(float a)
    Returns absolute value of the argument using standard Java function Math.abs(float).
    static int
    abs(int a)
    Returns absolute value of the argument using standard Java function Math.abs(int).
    static long
    abs(long a)
    Returns absolute value of the argument using standard Java function Math.abs(long).
    static double
    acos(double a)
    Calculates the arc cosine of an argument, using standard Java function Math.acos(double).
    static double
    asin(double a)
    Calculates the arc sine of an argument using standard Java function Math.asin(double).
    static double
    atan(double a)
    Calculates the arc tangent of an argument, using standard Java function Math.atan(double).
    static double
    cbrt(double a)
    Calculates the cube root using standard Java function StrictMath.cbrt(double).
    static double
    ceil(double a)
    Returns the smallest rounded to whole number value which is greater than or equal to the argument, using standard Java function Math.ceil(double).
    static double
    cos(double a)
    Calculates trigonometric cosine of an angle using standard Java function Math.cos(double).
    static double
    cosh(double x)
    Calculates the hyperbolic cosine, using standard Java function Math.cosh(double).
    static double
    exp(double a)
    Calculates Euler's number e raised to the given power using standard Java function Math.exp(double).
    static double
    floor(double a)
    Returns the largest rounded to whole number value which is less than or equal to the argument, using standard Java function Math.floor(double).
    static double
    log(double a)
    Calculates the natural logarithm using standard Java function Math.log(double).
    static double
    log(double a, double b)
    Calculates logarithm of b with base a.
    static double
    log10(double a)
    Calculates the base 10 logarithm using standard Java function Math.log10(double).
    static double
    max(double a, double b)
    Returns the greater of two values found using standard Java function Math.max(double, double).
    static float
    max(float a, float b)
    Returns the greater of two values found using standard Java function Math.max(float, float).
    static int
    max(int a, int b)
    Returns the greater of two values found using standard Java function Math.max(int, int).
    static long
    max(long a, long b)
    Returns the greater of two values found using standard Java function Math.max(long, long).
    static double
    min(double a, double b)
    Returns the smaller of two values found using standard Java function Math.min(double, double).
    static float
    min(float a, float b)
    Returns the smaller of two values found using standard Java function Math.min(float, float).
    static int
    min(int a, int b)
    Returns the smaller of two values found using standard Java function Math.min(int, int).
    static long
    min(long a, long b)
    Returns the smaller of two values found using standard Java function Math.min(long, long).
    static double
    pow(double a, double b)
    Returns the value of the first argument raised to the power of the second argument, calculated using standard Java function Math.pow(double, double).
    static double
    Returns a pseudo-random number greater than or equal to 0.0 and less than 1.0, using standard Java function Math.random()
    static long
    round(double a)
    Returns the closest long to the argument, with ties rounding to positive infinity, using standard Java function Math.round(double).
    static double
    round(double a, int scale)
    Rounds the number to specified decimal places.
    static int
    round(float a)
    Returns the closest int to the argument, calculated using standard Java function Math.round(float).
    static double
    signum(double d)
    Returns the signum function of the argument, determined using standard Java function Math.signum(double).
    static float
    signum(float f)
    Returns the signum function of the argument, determined using standard Java function Math.signum(float).
    static double
    sin(double a)
    Calculates trigonometric sine of an angle using standard Java function Math.sin(double).
    static double
    sinh(double x)
    Calculates the hyperbolic sine, using standard Java function Math.sinh(double).
    static double
    sqrt(double a)
    Calculates the positive square root using standard Java function Math.sqrt(double).
    static double
    tan(double a)
    Calculates trigonometric tangent of an angle using standard Java function Math.tan(double).
    static double
    toDegrees(double angrad)
    Converts an angle measured in radians to angle measured in degrees, using standard Java function Math.toDegrees(double).
    static double
    toRadians(double angdeg)
    Converts an angle measured in degrees to angle measured in radians, using standard Java function Math.toRadians(double).
    static double
    trunc(double a, int scale)
    Rounds the number to specified decimal places.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • INSTANCE

      public static final RhinoMath INSTANCE
    • E

      public static final double E
      Euler's number e=2.718..., base of natural logarithm
      See Also:
    • PI

      public static final double PI
      Number π = 3.14...
      See Also:
  • Method Details

    • sin

      public static double sin(double a)
      Calculates trigonometric sine of an angle using standard Java function Math.sin(double).
      Parameters:
      a - angle in radians
      Returns:
      sine of a.
    • cos

      public static double cos(double a)
      Calculates trigonometric cosine of an angle using standard Java function Math.cos(double).
      Parameters:
      a - angle in radians
      Returns:
      cosine of a.
    • tan

      public static double tan(double a)
      Calculates trigonometric tangent of an angle using standard Java function Math.tan(double).
      Parameters:
      a - angle in radians
      Returns:
      tangent of a.
    • asin

      public static double asin(double a)
      Calculates the arc sine of an argument using standard Java function Math.asin(double).
      Parameters:
      a - value whose arc sine is to be calculated
      Returns:
      arc sine of a. If the absolute value of a is greater than 1, the result is NaN.
    • acos

      public static double acos(double a)
      Calculates the arc cosine of an argument, using standard Java function Math.acos(double).
      Parameters:
      a - value whose arc cosine is to be calculated
      Returns:
      arc cosine of a. If the absolute value of a is greater than 1, the result is NaN.
    • atan

      public static double atan(double a)
      Calculates the arc tangent of an argument, using standard Java function Math.atan(double).
      Parameters:
      a - value whose arc tangent is to be calculated
      Returns:
      arc tangent of a. Returned value is in the range -pi/2 through pi/2.
    • toRadians

      public static double toRadians(double angdeg)
      Converts an angle measured in degrees to angle measured in radians, using standard Java function Math.toRadians(double).
      Parameters:
      angdeg - angle in degrees
      Returns:
      the measurement of the angle angdeg in radians.
    • toDegrees

      public static double toDegrees(double angrad)
      Converts an angle measured in radians to angle measured in degrees, using standard Java function Math.toDegrees(double).
      Parameters:
      angrad - angle in radians
      Returns:
      the measurement of the angle angrad in degrees.
    • exp

      public static double exp(double a)
      Calculates Euler's number e raised to the given power using standard Java function Math.exp(double).
      Parameters:
      a - the exponent to raise e to
      Returns:
      ea, where e is the base of natural logarithms.
    • log

      public static double log(double a)
      Calculates the natural logarithm using standard Java function Math.log(double).
      Parameters:
      a - the value whose logarithm is to be calculated
      Returns:
      ln a, the natural logarithm of a. If a<0, the result is NaN. If a=0, the result is negative infinity.
    • log10

      public static double log10(double a)
      Calculates the base 10 logarithm using standard Java function Math.log10(double).
      Parameters:
      a - the value whose logarithm with base 10 is to be calculated
      Returns:
      log10a. If a<0, the result is NaN. If a=0, the result is negative infinity.
    • log

      public static double log(double a, double b)
      Calculates logarithm of b with base a.
      Parameters:
      a - the base of the logarithm
      b - the value whose logarithm is to be calculated
      Returns:
      logab. For special values of a and b the result is:
      • if a<0 the result is NaN
      • if a=0 the result is NaN for b<=0 and 0 for b>0
      • if 0<a<1 the result is NaN for b<0 and Infinity for b=0
      • if a=1 the result is NaN for b<0, -Infinity for 0<b<1, NaN for b=1 and Infinity for b>1
      • if a>1 the result is NaN for b<0 and -Infinity for b=0
    • sqrt

      public static double sqrt(double a)
      Calculates the positive square root using standard Java function Math.sqrt(double).
      Parameters:
      a - the value whose square root is to be calculated
      Returns:
      square root of a. If a<0, the result is NaN.
    • cbrt

      public static double cbrt(double a)
      Calculates the cube root using standard Java function StrictMath.cbrt(double).
      Parameters:
      a - the value whose cube root is to be calculated
      Returns:
      cube root of a
    • ceil

      public static double ceil(double a)
      Returns the smallest rounded to whole number value which is greater than or equal to the argument, using standard Java function Math.ceil(double).
      Parameters:
      a - the value whose ceil is to be calculated
      Returns:
      the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
    • floor

      public static double floor(double a)
      Returns the largest rounded to whole number value which is less than or equal to the argument, using standard Java function Math.floor(double).
      Parameters:
      a - the value whose floor is to be calculated
      Returns:
      the largest (closest to positive infinity) value that is less than or equal to a and is equal to a mathematical integer.
    • pow

      public static double pow(double a, double b)
      Returns the value of the first argument raised to the power of the second argument, calculated using standard Java function Math.pow(double, double).
      Parameters:
      a - the base
      b - the exponent
      Returns:
      ab
    • round

      public static int round(float a)
      Returns the closest int to the argument, calculated using standard Java function Math.round(float).
      Parameters:
      a - the value for which the closest integer is calculated
      Returns:
      The int closest to a.

      Special cases:

    • round

      public static double round(double a, int scale)
      Rounds the number to specified decimal places. Values approximately in equal distance to the two closest rounded numbers are rounded up.
      Parameters:
      a - the value to be rounded
      scale - number of decimal places. Negative scale results in rounding to the nearest integer number with the specified number of zeroes at the end of its decimal representation.
      Returns:
      Multiple of 10-scale closest to a. If a is equal to the arithmetic average of the nearest multiples of 10-scale or is a bit smaller, the value is rounded up. More specifically, the result is calculated as:

      floor(a * 10scale+ 0.5 + 0.000001) / 10scale

      See Also:
    • trunc

      public static double trunc(double a, int scale)
      Rounds the number to specified decimal places. Values are rounded down (to the number closer to negative infinity).
      Parameters:
      a - the value to be rounded
      scale - number of decimal places. Negative scale results in rounding to the closest integer number less than or equal to a with the specified number of zeroes at the end of its decimal representation.
      Returns:
      Multiple of 10-scale closest to a, less than or equal to a.
    • round

      public static long round(double a)
      Returns the closest long to the argument, with ties rounding to positive infinity, using standard Java function Math.round(double).
      Parameters:
      a - the value to be rounded
      Returns:
      long closest to a.
    • random

      public static double random()
      Returns a pseudo-random number greater than or equal to 0.0 and less than 1.0, using standard Java function Math.random()
      Returns:
      a pseudorandom double greater than or equal to 0.0 and less than 1.0, with (approximately) uniform distribution.

      When this method is first called, it creates a new pseudo-random number generator, which is used thereafter for all calls to this method.

    • abs

      public static int abs(int a)
      Returns absolute value of the argument using standard Java function Math.abs(int).
      Parameters:
      a - the number whose absolute value is to be found
      Returns:
      absolute value of a.

      In special case when a=Integer.MIN_VALUE, the result is also equal to Integer.MIN_VALUE, which is negative.

    • abs

      public static long abs(long a)
      Returns absolute value of the argument using standard Java function Math.abs(long).
      Parameters:
      a - the number whose absolute value is to be found
      Returns:
      absolute value of a.

      In special case when a=Long.MIN_VALUE, the result is also equal to Long.MIN_VALUE, which is negative.

    • abs

      public static float abs(float a)
      Returns absolute value of the argument using standard Java function Math.abs(float).
      Parameters:
      a - the number whose absolute value is to be found
      Returns:
      absolute value of a.
    • abs

      public static double abs(double a)
      Returns absolute value of the argument using standard Java function Math.abs(double).
      Parameters:
      a - the number whose absolute value is to be found
      Returns:
      absolute value of a.
    • max

      public static int max(int a, int b)
      Returns the greater of two values found using standard Java function Math.max(int, int).
      Parameters:
      a - one value
      b - another value
      Returns:
      the larger of a and b.
    • max

      public static long max(long a, long b)
      Returns the greater of two values found using standard Java function Math.max(long, long).
      Parameters:
      a - one value
      b - another value
      Returns:
      the larger of a and b.
    • max

      public static float max(float a, float b)
      Returns the greater of two values found using standard Java function Math.max(float, float).
      Parameters:
      a - one value
      b - another value
      Returns:
      the larger of a and b.
    • max

      public static double max(double a, double b)
      Returns the greater of two values found using standard Java function Math.max(double, double).
      Parameters:
      a - one value
      b - another value
      Returns:
      the larger of a and b.
    • min

      public static int min(int a, int b)
      Returns the smaller of two values found using standard Java function Math.min(int, int).
      Parameters:
      a - one value
      b - another value
      Returns:
      the smaller of a and b.
    • min

      public static long min(long a, long b)
      Returns the smaller of two values found using standard Java function Math.min(long, long).
      Parameters:
      a - one value
      b - another value
      Returns:
      the smaller of a and b.
    • min

      public static float min(float a, float b)
      Returns the smaller of two values found using standard Java function Math.min(float, float).
      Parameters:
      a - one value
      b - another value
      Returns:
      the smaller of a and b.
    • min

      public static double min(double a, double b)
      Returns the smaller of two values found using standard Java function Math.min(double, double).
      Parameters:
      a - one value
      b - another value
      Returns:
      the smaller of a and b.
    • signum

      public static double signum(double d)
      Returns the signum function of the argument, determined using standard Java function Math.signum(double).
      Parameters:
      d - the number whose signum is determined
      Returns:
      • -1.0 if d<0,
      • 0.0 if d=0,
      • 1.0 if d>0.
    • signum

      public static float signum(float f)
      Returns the signum function of the argument, determined using standard Java function Math.signum(float).
      Parameters:
      f - the number whose signum is determined
      Returns:
      • -1.0f if d<0
      • 0.0f if d=0
      • 1.0f if d>0
    • sinh

      public static double sinh(double x)
      Calculates the hyperbolic sine, using standard Java function Math.sinh(double).
      Parameters:
      x - value, whose hyperbolic sine is to be calculated
      Returns:
      the hyperbolic sine of x.
    • cosh

      public static double cosh(double x)
      Calculates the hyperbolic cosine, using standard Java function Math.cosh(double).
      Parameters:
      x - value, whose hyperbolic cosine is to be calculated
      Returns:
      the hyperbolic cosine of x.