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 2026/04/07 07:48:00 UTC

[jira] [Resolved] (STATISTICS-94) Support median and quantile for long datatype

     [ https://issues.apache.org/jira/browse/STATISTICS-94?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Alex Herbert resolved STATISTICS-94.
------------------------------------
    Fix Version/s: 1.3
       Resolution: Implemented

Added in commit:

7175f6bdb5006b4b02dea1d2b2ddde9743373269

 

> Support median and quantile for long datatype
> ---------------------------------------------
>
>                 Key: STATISTICS-94
>                 URL: https://issues.apache.org/jira/browse/STATISTICS-94
>             Project: Commons Statistics
>          Issue Type: Improvement
>          Components: descriptive
>            Reporter: Alex Herbert
>            Assignee: Alex Herbert
>            Priority: Minor
>             Fix For: 1.3
>
>         Attachments: evaluate.png
>
>
> Add support to the {{Median}} and {{Quantile}} class for the {{long}} datatype. Current support is for {{double[]}} and {{{}int[]{}}}. Via primitive conversion these support float, short, char and byte if an array is appropriately converted. However {{long}} cannot be supported by primitive conversion.
> The current median and quantile result is returned as a {{{}double{}}}. This is not appropriate for the result on a long array as the closest representable double may be outside the range of the input data if the data have more than 53-bits in their integer representation. However if the integer value is representable as less than 53-bits then the double will provide a fractional part that cannot be returned if the result was a long. I suggest using the {{StatisticResult}} for the return value allowing both double and long representations:
> {code:java}
> StatisticResult r = Median.of(new long[] {Long.MIN_VALUE, Long.MAX_VALUE});
> r.getAsDouble();  // -0.5
> r.getAsLong();    // 0{code}
> h2. Rounding
> The current behaviour for {{StatisticResult}} is to return the integer representations using the closest representation of the floating-point value with ties towards positive infinity. So in the example above the median as a long is 0.
> This behaviour could be changed with a rounding mode parameter. Unfortunately the current rounding behaviour is not represented by any mode in [java.math.RoundingMode (Javadoc JDK 21)|https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/math/RoundingMode.html] so this enum cannot be reused to set the mode. The rounding behaviour matches [java.lang.Math.round(double) (Javadoc JDK 21)|https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Math.html#round(double)]. The closest match is RoundingMode.HALF_UP when above zero and RoundingMode.HALF_DOWN when below zero. If rounding to nearest is only concerned with the 0.5 boundary then support for rounding could add modes for rounding ties towards: negative infinity; positive infinity: or zero (floating-point truncation).
>  



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