Alex Herbert created STATISTICS-94:
--------------------------------------

             Summary: 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
            Reporter: Alex Herbert
            Assignee: Alex Herbert


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)

Reply via email to