Class MathG



  • public abstract class MathG
    extends Object
    This provides some alternative implementations of a few methods from the Math class.

    This class may use approximations with various levels of error. The "G" in the name stands for "Graphics", because it was originally conceived as a tool to speed up graphics. When I iterate over every pixel in an image to perform some operation: I don't really need the precision that the Math class offers.

    Many thanks to Oleg E. for some insights regarding machine error and design.

    See MathGDemo.java for a set of tests comparing the speed/accuracy of java.lang.Math and com.bric.math.MathG.

    • Constructor Summary

      Constructors

      Constructor and Description
      MathG()
       
    • Method Summary

      Methods

      Modifier and Type Method and Description
      static double acos(double v)
      Returns an approximate value of the acos(v) that should be within plus-or-minus .00004 of the value returned by Math.acos().
      static double ceilDouble(double d)
      Finds the closest integer that is greater than or equal to the argument as a double.
      static int ceilInt(double d)
      Finds the closest integer that is greater than or equal to the argument as an int.
      static double cos00004(double v)
      Returns an approximate value of the cos(v) that should be within plus-or-minus .00004 of the value returned by Math.cos().
      static double cos01(double v)
      Returns an approximate value of the cos(v) that should be within plus-or-minus .0108 of the value returned by Math.cos().
      static double floorDouble(double d)
      Finds the closest integer that is less than or equal to the argument as a double.
      static int floorInt(double d)
      Finds the closest integer that is less than or equal to the argument as an int.
      static double roundDouble(double d)
      Rounds a double to the nearest integer value.
      static int roundInt(double d)
      Rounds a double to the nearest integer value.
      static double sin00004(double v)
      Returns an approximate value of the sin(v) that should be within plus-or-minus .00004 of the value returned by Math.sin().
      static double sin01(double v)
      Returns an approximate value of the sin(v) that should be within plus-or-minus .0108 of the value returned by Math.sin().
    • Constructor Detail

      • MathG

        public MathG()
    • Method Detail

      • floorDouble

        public static final double floorDouble(double d)
        Finds the closest integer that is less than or equal to the argument as a double.
        Warning: do not use an argument greater than 1e10, or less than 1e-10.
        Parameters:
        d - the value to calculate the floor of.
        Returns:
        the closest integer that is less than the argument as a double.
      • floorInt

        public static final int floorInt(double d)
        Finds the closest integer that is less than or equal to the argument as an int.
        Warning: do not use an argument greater than 1e10, or less than 1e-10.
        Parameters:
        d - the value to calculate the floor of.
        Returns:
        the closest integer that is less than the argument as an int.
      • roundInt

        public static final int roundInt(double d)
        Rounds a double to the nearest integer value.
        Warning: do not use an argument greater than 1e10, or less than 1e-10.
        Parameters:
        d - the value to round.
        Returns:
        the closest integer that is less than the argument.
      • roundDouble

        public static final double roundDouble(double d)
        Rounds a double to the nearest integer value.
        Warning: do not use an argument greater than 1e10, or less than 1e-10.
        Parameters:
        d - the value to round.
        Returns:
        the closest integer that is less than the argument as a double.
      • ceilInt

        public static final int ceilInt(double d)
        Finds the closest integer that is greater than or equal to the argument as an int.
        Warning: do not use an argument greater than 1e10, or less than 1e-10.
        Parameters:
        d - the value to calculate the ceil of.
        Returns:
        the closest integer that is greater than the argument as an int.
      • ceilDouble

        public static final double ceilDouble(double d)
        Finds the closest integer that is greater than or equal to the argument as a double.
        Warning: do not use an argument greater than 1e10, or less than 1e-10.
        Parameters:
        d - the value to calculate the ceil of.
        Returns:
        the closest integer that is greater than the argument as a double.
      • sin01

        public static final double sin01(double v)
        Returns an approximate value of the sin(v) that should be within plus-or-minus .0108 of the value returned by Math.sin().

        If the argument is greater in magnitude than 1e10, then this delegates to Math.sin().

        Parameters:
        v -
        Returns:
        an approximate value of sin(v)
      • cos01

        public static final double cos01(double v)
        Returns an approximate value of the cos(v) that should be within plus-or-minus .0108 of the value returned by Math.cos().

        If the argument is greater in magnitude than 1e10, then this delegates to Math.cos().

        Parameters:
        v -
        Returns:
        an approximate value of cos(v)
      • sin00004

        public static final double sin00004(double v)
        Returns an approximate value of the sin(v) that should be within plus-or-minus .00004 of the value returned by Math.sin().

        If the argument is greater in magnitude than 1e10, then this delegates to Math.sin().

        Parameters:
        v -
        Returns:
        an approximate value of sin(v)
      • acos

        public static final double acos(double v)
        Returns an approximate value of the acos(v) that should be within plus-or-minus .00004 of the value returned by Math.acos().
        Parameters:
        v -
        Returns:
        an approximate value of acos(v)
      • cos00004

        public static final double cos00004(double v)
        Returns an approximate value of the cos(v) that should be within plus-or-minus .00004 of the value returned by Math.cos().

        If the argument is greater in magnitude than 1e10, then this delegates to Math.cos().

        Parameters:
        v -
        Returns:
        an approximate value of cos(v)