You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/10/22 15:33:10 UTC
[groovy] branch master updated: Trivial tweak for calculating average of array
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 ba6d1a7033 Trivial tweak for calculating average of array
ba6d1a7033 is described below
commit ba6d1a7033c690de48df5a730ce7e2d98b3c73de
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Oct 22 23:32:49 2022 +0800
Trivial tweak for calculating average of array
---
.../groovy/runtime/ArrayGroovyMethods.java | 24 ++++++----------------
1 file changed, 6 insertions(+), 18 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
index 21a83c5b35..2268008c7e 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
@@ -391,12 +391,10 @@ public class ArrayGroovyMethods {
*/
public static BigDecimal average(byte[] self) {
long s = 0;
- int count = 0;
for (byte v : self) {
s += v;
- count++;
}
- return BigDecimal.valueOf(s).divide(BigDecimal.valueOf(count));
+ return BigDecimal.valueOf(s).divide(BigDecimal.valueOf(self.length));
}
/**
@@ -409,12 +407,10 @@ public class ArrayGroovyMethods {
*/
public static BigDecimal average(short[] self) {
long s = 0;
- int count = 0;
for (short v : self) {
s += v;
- count++;
}
- return BigDecimal.valueOf(s).divide(BigDecimal.valueOf(count));
+ return BigDecimal.valueOf(s).divide(BigDecimal.valueOf(self.length));
}
/**
@@ -427,12 +423,10 @@ public class ArrayGroovyMethods {
*/
public static BigDecimal average(int[] self) {
long s = 0;
- int count = 0;
for (int v : self) {
s += v;
- count++;
}
- return BigDecimal.valueOf(s).divide(BigDecimal.valueOf(count));
+ return BigDecimal.valueOf(s).divide(BigDecimal.valueOf(self.length));
}
/**
@@ -445,12 +439,10 @@ public class ArrayGroovyMethods {
*/
public static BigDecimal average(long[] self) {
long s = 0;
- int count = 0;
for (long v : self) {
s += v;
- count++;
}
- return BigDecimal.valueOf(s).divide(BigDecimal.valueOf(count));
+ return BigDecimal.valueOf(s).divide(BigDecimal.valueOf(self.length));
}
/**
@@ -463,12 +455,10 @@ public class ArrayGroovyMethods {
*/
public static double average(float[] self) {
double s = 0.0d;
- int count = 0;
for (float v : self) {
s += v;
- count++;
}
- return s / count;
+ return s / self.length;
}
/**
@@ -481,12 +471,10 @@ public class ArrayGroovyMethods {
*/
public static double average(double[] self) {
double s = 0.0d;
- int count = 0;
for (double v : self) {
s += v;
- count++;
}
- return s / count;
+ return s / self.length;
}
//-------------------------------------------------------------------------