Class Arithmetic

java.lang.Object
imagingbook.common.math.Arithmetic

public abstract class Arithmetic extends Object
This class defines static methods implementing arithmetic operations and predicates.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Default tolerance used for comparing double quantities.
    static final float
    Default tolerance used for comparing float quantities.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    angle(double x, double y)
    Returns the angle of the Cartesian point (x,y)
    static double
    clipTo(double x, double low, double high)
    Limits the first argument to the clipping interval specified by the two other arguments (double version).
    static float
    clipTo(float x, float low, float high)
    Limits the first argument to the clipping interval specified by the two other arguments (float version).
    static int
    clipTo(int x, int low, int high)
    Limits the first argument to the clipping interval specified by the two other arguments (float version).
    static boolean
    equals(double x, double y)
    Test for numerical equality (double version) using the default tolerance.
    static boolean
    equals(double x, double y, double tolerance)
    Test for numerical equality (double version) using a specific tolerance.
    static boolean
    equals(float x, float y)
    Test for numerical equality (float version) using the default tolerance.
    static boolean
    equals(float x, float y, float tolerance)
    Test for numerical equality (float version) using a specific tolerance.
    static double[]
    getRealRoots(double a, double b, double c)
    Returns the two real roots of the quadratic function f(x) = ax^2 + bx + c.
    static boolean
    isZero(double x)
    Test for zero (double version) using a predefined tolerance.
    static boolean
    isZero(double x, double tolerance)
    Test for zero (double version) using a specified tolerance.
    static boolean
    isZero(float x)
    Test for zero (float version) using a predefined tolerance.
    static boolean
    isZero(float x, float tolerance)
    Test for zero (float version) using a specified tolerance.
    static double
    max(double a, double... vals)
    Returns the maximum of one or more double values.
    static float
    max(float a, float... vals)
    Returns the maximum of one or more float values.
    static int
    max(int a, int... vals)
    Returns the maximum of one or more integer values.
    static double
    min(double a, double... vals)
    Returns the minimum of one or more double values.
    static float
    min(float a, float... vals)
    Returns the minimum of one or more float values.
    static int
    min(int a, int... vals)
    Returns the minimum of one or more integer values.
    static double
    mod(double a, double b)
    Non-integer version of modulus operator using floored division (see here), with results identical to Mathematica.
    static int
    mod(int a, int b)
    Integer version of the modulus operator (a mod b).
    static double
    radius(double x, double y)
    Returns the radius of the Cartesian point
    static double
    sqr(double x)
    Returns the square of its argument.
    static float
    sqr(float x)
    Returns the square of its argument.
    static int
    sqr(int x)
    Returns the square of its argument.
    static long
    sqr(long x)
    Returns the square of its argument.
    static double[]
    toCartesian(double[] polar)
    Converts polar point coordinates to Cartesian coordinates.
    static double[]
    toCartesian(double radius, double angle)
    Converts polar point coordinates to Cartesian coordinates.
    static double[]
    toPolar(double[] xy)
    Returns the polar coordinates of the Cartesian point xy.
    static double[]
    toPolar(double x, double y)
    Returns the polar coordinates of the Cartesian point (x,y).

    Methods inherited from class java.lang.Object

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

  • Method Details

    • sqr

      public static int sqr(int x)
      Returns the square of its argument.
      Parameters:
      x - argument
      Returns:
      square of argument
    • sqr

      public static long sqr(long x)
      Returns the square of its argument.
      Parameters:
      x - argument
      Returns:
      square of argument
    • sqr

      public static float sqr(float x)
      Returns the square of its argument.
      Parameters:
      x - argument
      Returns:
      square of argument
    • sqr

      public static double sqr(double x)
      Returns the square of its argument.
      Parameters:
      x - argument
      Returns:
      square of argument
    • radius

      public static double radius(double x, double y)
      Returns the radius of the Cartesian point
      Parameters:
      x - x-component
      y - y-component
      Returns:
      the radius
    • angle

      public static double angle(double x, double y)
      Returns the angle of the Cartesian point (x,y)
      Parameters:
      x - x-component
      y - y-component
      Returns:
      the angle
    • toPolar

      public static double[] toPolar(double x, double y)
      Returns the polar coordinates of the Cartesian point (x,y).
      Parameters:
      x - x-component
      y - y-component
      Returns:
      a 2-element array holding the polar coordinates (radius, angle)
    • toPolar

      public static double[] toPolar(double[] xy)
      Returns the polar coordinates of the Cartesian point xy.
      Parameters:
      xy - the Cartesian coordinates
      Returns:
      a 2-element array holding the polar coordinates (radius, angle)
    • toCartesian

      public static double[] toCartesian(double radius, double angle)
      Converts polar point coordinates to Cartesian coordinates.
      Parameters:
      radius - the radius
      angle - the angle
      Returns:
      a 2-element array holding the Cartesian coordinates (x,y)
    • toCartesian

      public static double[] toCartesian(double[] polar)
      Converts polar point coordinates to Cartesian coordinates.
      Parameters:
      polar - the polar coordinates (radius, angle)
      Returns:
      a 2-element array holding the Cartesian coordinates (x,y)
    • mod

      public static int mod(int a, int b)
      Integer version of the modulus operator (a mod b). Also see here. Calls Math.floorMod(a,b) (available in Java 8 and higher).
      Parameters:
      a - dividend
      b - divisor (modulus), must be nonzero
      Returns:
      a mod b
    • mod

      public static double mod(double a, double b)
      Non-integer version of modulus operator using floored division (see here), with results identical to Mathematica. Calculates a mod b for floating-point arguments. An exception is thrown if b is zero. Examples:
       mod( 3.5, 2.1) =  1.4
       mod(-3.5, 2.1) =  0.7
       mod( 3.5,-2.1) = -0.7
       mod(-3.5,-2.1) = -1.4
      Parameters:
      a - dividend
      b - divisor (modulus), must be nonzero
      Returns:
      a mod b
    • isZero

      public static boolean isZero(float x)
      Test for zero (float version) using a predefined tolerance. Returns true if the argument's absolute value is less than EPSILON_FLOAT.
      Parameters:
      x - quantity to be tested
      Returns:
      true if argument is close to zero
    • isZero

      public static boolean isZero(float x, float tolerance)
      Test for zero (float version) using a specified tolerance. Returns true if the argument's absolute value is less than the specified tolerance.
      Parameters:
      x - quantity to be tested
      tolerance - the tolerance to be used
      Returns:
      true if argument is close to zero
    • isZero

      public static boolean isZero(double x)
      Test for zero (double version) using a predefined tolerance. Returns true if the argument's absolute value is less than EPSILON_DOUBLE.
      Parameters:
      x - quantity to be tested
      Returns:
      true if argument is close to zero
    • isZero

      public static boolean isZero(double x, double tolerance)
      Test for zero (double version) using a specified tolerance. Returns true if the argument's absolute value is less than the specified tolerance.
      Parameters:
      x - quantity to be tested
      tolerance - the tolerance to be used
      Returns:
      true if argument is close to zero
    • equals

      public static boolean equals(double x, double y)
      Test for numerical equality (double version) using the default tolerance. Returns true if the absolute difference of the arguments is less than EPSILON_DOUBLE.
      Parameters:
      x - first argument
      y - second argument
      Returns:
      true if the absolute difference of the arguments is less than the tolerance
    • equals

      public static boolean equals(double x, double y, double tolerance)
      Test for numerical equality (double version) using a specific tolerance.
      Parameters:
      x - first argument
      y - second argument
      tolerance - the maximum (absolute) deviation
      Returns:
      true if the absolute difference of the arguments is less than the tolerance
    • equals

      public static boolean equals(float x, float y)
      Test for numerical equality (float version) using the default tolerance. Returns true if the absolute difference of the arguments is less than EPSILON_FLOAT.
      Parameters:
      x - first argument
      y - second argument
      Returns:
      true if the absolute difference of the arguments is less than the tolerance
    • equals

      public static boolean equals(float x, float y, float tolerance)
      Test for numerical equality (float version) using a specific tolerance.
      Parameters:
      x - first argument
      y - second argument
      tolerance - the maximum (absolute) deviation
      Returns:
      true if the absolute difference of the arguments is less than the tolerance
    • max

      public static int max(int a, int... vals)
      Returns the maximum of one or more integer values.
      Parameters:
      a - the first value
      vals - more values
      Returns:
      the maximum value
    • min

      public static int min(int a, int... vals)
      Returns the minimum of one or more integer values.
      Parameters:
      a - the first value
      vals - more values
      Returns:
      the minimum value
    • max

      public static float max(float a, float... vals)
      Returns the maximum of one or more float values.
      Parameters:
      a - the first value
      vals - more values
      Returns:
      the maximum value
    • min

      public static float min(float a, float... vals)
      Returns the minimum of one or more float values.
      Parameters:
      a - the first value
      vals - more values
      Returns:
      the minimum value
    • max

      public static double max(double a, double... vals)
      Returns the maximum of one or more double values.
      Parameters:
      a - the first value
      vals - more values
      Returns:
      the maximum value
    • min

      public static double min(double a, double... vals)
      Returns the minimum of one or more double values.
      Parameters:
      a - the first value
      vals - more values
      Returns:
      the minimum value
    • clipTo

      public static double clipTo(double x, double low, double high)
      Limits the first argument to the clipping interval specified by the two other arguments (double version). The clipped value is returned. Throws an exception if the clipping interval is empty.
      Parameters:
      x - the value to be clipped
      low - the lower boundary of the clipping interval
      high - the upper boundary of the clipping interval
      Returns:
      the clipped value
    • clipTo

      public static float clipTo(float x, float low, float high)
      Limits the first argument to the clipping interval specified by the two other arguments (float version). The clipped value is returned. Throws an exception if the clipping interval is empty.
      Parameters:
      x - the value to be clipped
      low - the lower boundary of the clipping interval
      high - the upper boundary of the clipping interval
      Returns:
      the clipped value
    • clipTo

      public static int clipTo(int x, int low, int high)
      Limits the first argument to the clipping interval specified by the two other arguments (float version). The clipped value is returned. Throws an exception if the clipping interval is empty.
      Parameters:
      x - the value to be clipped
      low - the lower boundary of the clipping interval
      high - the upper boundary of the clipping interval
      Returns:
      the clipped value
    • getRealRoots

      public static double[] getRealRoots(double a, double b, double c)
      Returns the two real roots of the quadratic function f(x) = ax^2 + bx + c. Null is returned if roots are non-real.
      Parameters:
      a - function coefficient
      b - function coefficient
      c - function coefficient
      Returns:
      an array with the two roots x1, x2 (in no particular order) or null if the function has complex roots