Policy.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.numbers.gamma;

  18. /**
  19.  * Encapsulate the policy for function evaluation.
  20.  * This is a reduced implementation of the Boost {@code boost::math::policies}
  21.  * functionality. No settings are preserved for the error handling policy or
  22.  * promotion of data types for computations.
  23.  * This controls the convergence criteria and maximum iterations for series evaluations.
  24.  *
  25.  * @see <a href="https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/policy.html">
  26.  * Policies: Controlling Precision, Error Handling etc</a>
  27.  */
  28. final class Policy {
  29.     /** Default policy. The Boost default uses 2^-52 for the epsilon. This uses
  30.      * 2^-53 to use an extra guard digit in the Kahan series summations.
  31.      * The minimum value for the Commons continued fraction epsilon is also 2^-53. */
  32.     private static final Policy DEFAULT = new Policy(0x1.0p-53, 1000000);

  33.     /** Epsilon value for relative error. */
  34.     private final double eps;
  35.     /** The maximum number of iterations permitted in a series evaluation. */
  36.     private final int maxIterations;

  37.     /**
  38.      * Instantiates a new policy.
  39.      *
  40.      * @param eps the eps
  41.      * @param maxIterations the maximum number of iterations permitted in a series
  42.      * evaluation
  43.      */
  44.     Policy(double eps, int maxIterations) {
  45.         this.eps = eps;
  46.         this.maxIterations = maxIterations;
  47.     }

  48.     /**
  49.      * Gets the default.
  50.      *
  51.      * @return the default policy
  52.      */
  53.     static Policy getDefault() {
  54.         return DEFAULT;
  55.     }

  56.     /**
  57.      * Gets the epsilon value for relative error.
  58.      *
  59.      * @return the epsilon
  60.      */
  61.     double getEps() {
  62.         return eps;
  63.     }

  64.     /**
  65.      * Gets the maximum number of iterations permitted in a series evaluation.
  66.      *
  67.      * @return max iterations
  68.      */
  69.     int getMaxIterations() {
  70.         return maxIterations;
  71.     }
  72. }