[ 
https://issues.apache.org/jira/browse/NUMBERS-163?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17366760#comment-17366760
 ] 

Alex Herbert commented on NUMBERS-163:
--------------------------------------

I think that allowing the addition of a BigDecimal in the API would have 
problems ensuring accuracy. Either you convert the BigDecimal to a double (loss 
of precision) or you convert it to a series of overlapping double values and 
add each one. This will be slow.

Untested...
{code:java}
// Addend
BigDecimal x;

if (x.abs().compareTo(new BigDecimal(Double.MAX_VALUE)) > 0) {
    // Throw or just make the sum infinity
    throw new IllegalArgumentException("Too big");
}

while (true) {
    double d = x.toDouble();
    if (d == 0) {
        // Because BigDecimal can be below Double.MIN_VALUE
        break;
    }
    // Add the double d to our sum

    // Get the rest of the number
    x = x.subtract(new BigDecimal(d));
}
{code}

Do you want to support this?

> Summation and LinearCombination Accumulators
> --------------------------------------------
>
>                 Key: NUMBERS-163
>                 URL: https://issues.apache.org/jira/browse/NUMBERS-163
>             Project: Commons Numbers
>          Issue Type: New Feature
>            Reporter: Matt Juntunen
>            Priority: Major
>         Attachments: FMA.java, Sum.java
>
>          Time Spent: 1h 20m
>  Remaining Estimate: 0h
>
> It would be useful to have simple accumulator classes in {{Summation}} and 
> {{LinearCombination}} to perform extended precision operations on arbitrary 
> collections of values without requiring conversion to {{double[]}}. Ex:
> {code:java}
> Summation.Accumulator sum= Summation.accumulator(1d);
> sum.add(x)
>     .add(y)
>     .add(z)
>    .add(w);
> double sumResult = sum.get();
> LinearCombination.Accumulator comb = LinearCombination.accumulator(1d);
> comb.add(x, scale)
>     .add(y, scale)
>     .add(z, scale)
>     .add(w, scale);
> double combResult = comb.get();
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to