Class Factorial

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static double[] DOUBLE_FACTORIALS
      All factorials that can be represented as a double (values up to 170!).
      (package private) static long[] FACTORIALS
      All long-representable factorials.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Factorial()
      Private constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double doubleValue​(int n)
      Computes the factorial of n.
      (package private) static double uncheckedFactorial​(int n)
      Return the factorial of n.
      static long value​(int n)
      Computes the factorial of n.
      • Methods inherited from class java.lang.Object

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

      • FACTORIALS

        static final long[] FACTORIALS
        All long-representable factorials.
      • DOUBLE_FACTORIALS

        private static final double[] DOUBLE_FACTORIALS
        All factorials that can be represented as a double (values up to 170!).
    • Constructor Detail

      • Factorial

        private Factorial()
        Private constructor.
    • Method Detail

      • value

        public static long value​(int n)
        Computes the factorial of n.
        Parameters:
        n - Argument.
        Returns:
        n!
        Throws:
        java.lang.IllegalArgumentException - if n < 0.
        java.lang.IllegalArgumentException - if n > 20 (the factorial value is too large to fit in a long).
      • doubleValue

        public static double doubleValue​(int n)
        Computes the factorial of n.

        The result should be small enough to fit into a double: The largest n for which n! does not exceed Double.MAX_VALUE is 170. Double.POSITIVE_INFINITY is returned for n > 170.

        Parameters:
        n - Argument.
        Returns:
        n!
        Throws:
        java.lang.IllegalArgumentException - if n < 0.
        Since:
        1.1
      • uncheckedFactorial

        static double uncheckedFactorial​(int n)
        Return the factorial of n.

        Note: This is an internal method that exposes the tabulated factorials that can be represented as a double. No checks are performed on the argument.

        Parameters:
        n - Argument (must be in [0, 170])
        Returns:
        n!