我认为您应该以不同的方式来考虑。您想要做的是找到每行十的倍数的距离。
十进制的最接近倍数由表达式给出
double mult = 10d * Math.round(v / 10d)
与十的倍数的距离由表达式给出
double delta = Math.abs(v - mult)
对于的任何值mult,您要的行都是最小的行delta。
因此,您只需要迭代行一次。
beam_value
连续获取并找到mult
和delta
。- 如果行的delta比以前发现的任何接近
delta
的mult
,然后再登录该行为mult,否则忽略它。 - 重复直到没有更多行。
还要注意,这种方法将防止记录一行超过十的整数倍,而其他方法很难防止这种情况。
通过示例(由于我没有您的SQL查询,所以我伪造了数据)。输入数据:
`0.5, 12.10, 13.00, 16.01, 21.52`
给出下面的输出,它是正确的(索引110比索引2更近,索引420比索引3更近):
10x row value 0 0 0.5000 10 1 12.1000 20 4 21.5200
与代码:
public static void findClosestRowsToMultiplesOfTen() { // fake row values double[] vals = new double[]{ 0.5, 12.10, 13.00, 16.01, 21.52 }; // get the max value, and its multiple of ten to get the number of buckets double max = Double.MIN_VALUE; for (double v : vals) max = Math.max(max, v); int bucketCount = 1 + (int)(max/10); // initialise the buckets array to store the closest values double[][] buckets = new double[bucketCount][3]; for (int i = 0; i < bucketCount; i++){ // store the current smallest delta in the first element buckets[i][0] = Double.MAX_VALUE; // store the current "closest" index in the second element buckets[i][1] = -1d; // store the current "closest" value in the third element buckets[i][2] = Double.MAX_VALUE; } // iterate the rows for (int i = 0; i < vals.length; i++) { // get the value from the row double v = vals[i]; // get the closest multiple of ten to v double mult = getMultipleOfTen(v); // get the absolute distance of v from the multiple of ten double delta = Math.abs(mult - v); // get the bucket index based on the value of `mult` int bIdx = (int)(mult / 10d); // test the last known "smallest delta" for this bucket if (buckets[bIdx][0] > delta) { // this is closer than the last known "smallest delta" buckets[bIdx][0] = delta; buckets[bIdx][1] = i; buckets[bIdx][2] = v; } } // print out the result System.out.format(" 10x row value%n"); for (int i = 0; i < buckets.length; i++) { double[] bucket = buckets[i]; int multipleOfTen = i * 10; double rowIndex = bucket[1]; double rowValue = bucket[2]; System.out.format(" %,2d %,4.0f %.4f%n",multipleOfTen, rowIndex, rowValue); }}public static double getMultipleOfTen(double v){ return 10d * Math.round(v / 10d);}


