You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Alex Herbert (Jira)" <ji...@apache.org> on 2023/07/18 10:37:00 UTC

[jira] [Commented] (NUMBERS-199) Method "round "in class "Precision"

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

Alex Herbert commented on NUMBERS-199:
--------------------------------------

The original report in MATH-1470 links to an example repo that is NA. The issue manifests at higher precisions:
{code:java}
@Test
void testRoundRandom() {
    double[] values = ThreadLocalRandom.current().doubles(10000).toArray();
    for (int p = 3; p <= 16; p++) {
        int n = 0;
        for (double x : values) {
            double y = Precision.round(x, p);
            double e = new BigDecimal(x).setScale(p, RoundingMode.HALF_UP).doubleValue();
            if (y != e) {
                n++;
            }
        }
        System.out.printf("[%d] %d/%d%n", p, n, values.length);
    }
}
{code}
Prints:
{noformat}
[3] 0/10000
[4] 0/10000
[5] 0/10000
[6] 0/10000
[7] 0/10000
[8] 0/10000
[9] 0/10000
[10] 0/10000
[11] 0/10000
[12] 0/10000
[13] 3/10000
[14] 33/10000
[15] 351/10000
[16] 230/10000
{noformat}
Errors can be seen when rounding on the apparent digit 5 as the string representation of a double is not exact. This can be seen when using the following test cases where the exact BigDecimal representation of the value does not have the 5 but 4...:
{code:java}
Assertions.assertEquals(0.1768426638693,
    Precision.round(0.17684266386935, 13),
    "Rounding       0.17684266386934999193414341789321042597293853759765625")
Assertions.assertEquals(0.70150248722621,
    Precision.round(0.701502487226215, 14),
    "Rounding       0.701502487226214999083140355651266872882843017578125")
Assertions.assertEquals(0.601253002244537, 
    Precision.round(0.6012530022445375, 15),
    "Rounding       0.6012530022445374822126495928387157618999481201171875")
Assertions.assertEquals(0.2891688522619807,
    Precision.round(0.28916885226198075, 16),
    "Rounding       0.28916885226198074665404647021205164492130279541015625")
{code}

> Method "round "in class "Precision"
> -----------------------------------
>
>                 Key: NUMBERS-199
>                 URL: https://issues.apache.org/jira/browse/NUMBERS-199
>             Project: Commons Numbers
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.1
>            Reporter: Gilles Sadowski
>            Priority: Major
>             Fix For: 1.2
>
>
> See description in original report: MATH-1470.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)