[groovy] branch master updated: Tweak `stdev`: use `BigDecimal.pow` instead

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[groovy] branch master updated: Tweak `stdev`: use `BigDecimal.pow` instead

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

The following commit(s) were added to refs/heads/master by this push:
     new c55c1cc  Tweak `stdev`: use `BigDecimal.pow` instead
c55c1cc is described below

commit c55c1ccf23a5c0f80bfd2313d58e19410eb01b5b
Author: Daniel Sun <[hidden email]>
AuthorDate: Sat Jan 9 18:12:20 2021 +0800

    Tweak `stdev`: use `BigDecimal.pow` instead
 .../groovy/ginq/provider/collection/runtime/QueryableCollection.java  | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
index 040ccd6..41eb603 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
@@ -54,7 +54,6 @@ import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
 import static groovy.lang.Tuple.tuple;
-import static java.lang.Math.pow;
 import static java.lang.Math.sqrt;
 import static java.util.Comparator.naturalOrder;
 import static java.util.Comparator.nullsFirst;
@@ -418,13 +417,14 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
         Object[] result = agg(q -> q.stream()
-                .map(e -> toBigDecimal(pow(NumberNumberMinus.minus(e, avg).doubleValue(), 2)))
+                .map(e -> toBigDecimal(NumberNumberMinus.minus(e, avg)).pow(2))
                 .reduce(new Object[]{0L, BigDecimal.ZERO}, (r, e) -> {
                     r[0] = (Long) r[0] + 1;
                     r[1] = ((BigDecimal) r[1]).add(e);
                     return r;
                 }, (o1, o2) -> o1));
+        // `BigDecimal.sqrt` is introduced since Java9, so we can not use it for now.
         return toBigDecimal(sqrt(((BigDecimal) result[1]).divide(toBigDecimal((Long) result[0]), 16, RoundingMode.HALF_UP).doubleValue()));

Apache Groovy committer & PMC member

Blog: http://blog.sunlan.me
Twitter: @daniel_sun