File tree Expand file tree Collapse file tree 1 file changed +10
-7
lines changed
core/src/main/java/ai/timefold/solver/core/impl/score/stream/collector Expand file tree Collapse file tree 1 file changed +10
-7
lines changed Original file line number Diff line number Diff line change @@ -96,13 +96,16 @@ public Map<Balanced_, Long> loads() {
9696 @ Override
9797 public BigDecimal unfairness () {
9898 var totalToBalanceCount = balancedItemCountMap .size ();
99- if (totalToBalanceCount == 0 ) {
100- return BigDecimal .ZERO ;
101- }
102- return BigDecimal .valueOf (squaredDeviationFractionNumerator )
103- .divide (BigDecimal .valueOf (totalToBalanceCount ), MathContext .DECIMAL32 ) // Compute w/ greater precision.
104- .add (BigDecimal .valueOf (squaredDeviationIntegralPart ))
105- .sqrt (RESULT_MATH_CONTEXT );
99+ return switch (totalToBalanceCount ) {
100+ case 0 -> BigDecimal .ZERO ;
101+ case 1 -> BigDecimal .valueOf (squaredDeviationFractionNumerator + squaredDeviationIntegralPart )
102+ .sqrt (RESULT_MATH_CONTEXT );
103+ default -> { // Only do the final sqrt as BigDecimal, fast floating point math is good enough for the rest.
104+ var tmp = (squaredDeviationFractionNumerator / (double ) totalToBalanceCount ) + squaredDeviationIntegralPart ;
105+ yield BigDecimal .valueOf (tmp )
106+ .sqrt (RESULT_MATH_CONTEXT );
107+ }
108+ };
106109 }
107110
108111}
You can’t perform that action at this time.
0 commit comments