You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gilles Sadowski (Jira)" <ji...@apache.org> on 2021/12/03 22:52:00 UTC
[jira] [Comment Edited] (NUMBERS-178) FactorialDouble can tabulate the representable factorials
[ https://issues.apache.org/jira/browse/NUMBERS-178?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17453212#comment-17453212 ]
Gilles Sadowski edited comment on NUMBERS-178 at 12/3/21, 10:51 PM:
--------------------------------------------------------------------
{quote}A static value(int) method would match other classes that compute values.
{quote}
Sure, but as you noted in the [source code|https://gitbox.apache.org/repos/asf?p=commons-numbers.git;a=blob;f=commons-numbers-combinatorics/src/main/java/org/apache/commons/numbers/combinatorics/FactorialDouble.java;h=6b0baf0c77d1a331742b86c2be79a268aa0e6469;hb=HEAD#l239]:
{noformat}
// Note: This must remain an instance method for binary compatibility
{noformat}
However, I also think that
{code:java}
FactorialDouble.of(n){code}
looks good.
was (Author: erans):
bq. A static value(int) method would match other classes that compute values.
Sure, but as you noted in the [source code|https://gitbox.apache.org/repos/asf?p=commons-numbers.git;a=blob;f=commons-numbers-combinatorics/src/main/java/org/apache/commons/numbers/combinatorics/FactorialDouble.java;h=6b0baf0c77d1a331742b86c2be79a268aa0e6469;hb=HEAD#l239]:
{noformat}
// Note: This must remain an instance method for binary compatibility
{noformat}
However, I also thinks that {code}FactorialDouble.of(n){code} looks good.
> FactorialDouble can tabulate the representable factorials
> ---------------------------------------------------------
>
> Key: NUMBERS-178
> URL: https://issues.apache.org/jira/browse/NUMBERS-178
> Project: Commons Numbers
> Issue Type: Improvement
> Components: combinatorics
> Affects Versions: 1.0
> Reporter: Alex Herbert
> Priority: Trivial
> Fix For: 1.1
>
>
> The updated Gamma function (see NUMBERS-174) tabulates all representable factorials.
> This suggests one of the following changes:
> # The FactorialDouble class can call the Gamma function to obtain the values.
> # The FactorialDouble class can also tabulate the values and not use a dependency on the Gamma class
> Note that if the call is made to the Gamma class the method is effectively:
> {code:java}
> public static double value(int n) {
> // The Gamma class has all factorial values tabulated.
> return tgamma(n + 1);
> }
> static double tgamma(double z) {
> // Handle integers
> if (Math.rint(z) == z) {
> if (z <= 0) {
> // Pole error
> return Double.NaN;
> }
> if (z <= MAX_GAMMA_Z) {
> // Gamma(n) = (n-1)!
> return FACTORIAL[(int) z - 1];
> }
> // Overflow
> return Double.POSITIVE_INFINITY;
> }
> // ... Not used
> }
> {code}
> So calling the Gamma method has a round trip of the integer to a double, a check it is an integer, then a conversion back to an integer.
> The FactorialDouble class also has a cache of the values. So all the values are precomputed once and stored for the instance.
> I suggest updating the class to remove the cache and just storing the 171 representable double values for values up to 170!. The public API can remain the same but the cache methods marked as deprecated. The value method then becomes:
> {code:java}
> public double value(int n) {
> if (n < 0) {
> throw new CombinatoricsException(CombinatoricsException.NEGATIVE, n);
> }
> if (n < FACTORIAL.length) {
> // Cache of precomputed values up to 170!
> return FACTORIAL[n];
> }
> return Double.POSITIVE_INFINITY;
> }
> {code}
> Note:
> This class is a similar implementation to the LogFactorial class. In that case the maximum representable LogFactorial is very big and the cache functionality makes sense. For a maximum cache size of 171 this caching functionality seems unnecessary.
--
This message was sent by Atlassian Jira
(v8.20.1#820001)