diff --git a/distribution/lib/Standard/Test/0.0.0-dev/src/Main.enso b/distribution/lib/Standard/Test/0.0.0-dev/src/Main.enso index abb63e94953d7..3b819cd8dbe29 100644 --- a/distribution/lib/Standard/Test/0.0.0-dev/src/Main.enso +++ b/distribution/lib/Standard/Test/0.0.0-dev/src/Main.enso @@ -272,12 +272,16 @@ Error.should_equal _ frames_to_skip=0 = here.fail_match_on_unexpected_error this example_should_equal = 1.00000001 . should_equal 1.00000002 epsilon=0.0001 Decimal.should_equal : Decimal -> Decimal -> Integer -> Assertion -Decimal.should_equal that (epsilon = 0) (frames_to_skip=0) = case this.equals that epsilon of - True -> Success - False -> - loc = Meta.get_source_location 2+frames_to_skip - msg = this.to_text + " did not equal " + that.to_text + " (at " + loc + ")." - Panic.throw (Failure msg) +Decimal.should_equal that (epsilon = 0) (frames_to_skip=0) = + matches = case that of + Number -> this.equals that epsilon + _ -> False + case matches of + True -> Success + False -> + loc = Meta.get_source_location 2+frames_to_skip + msg = this.to_text + " did not equal " + that.to_text + " (at " + loc + ")." + Panic.throw (Failure msg) ## Asserts that the given `Boolean` is `True` diff --git a/std-bits/table/src/main/java/org/enso/table/aggregations/StandardDeviation.java b/std-bits/table/src/main/java/org/enso/table/aggregations/StandardDeviation.java index a738c392da6d3..954dcdfb31ac7 100644 --- a/std-bits/table/src/main/java/org/enso/table/aggregations/StandardDeviation.java +++ b/std-bits/table/src/main/java/org/enso/table/aggregations/StandardDeviation.java @@ -1,11 +1,10 @@ package org.enso.table.aggregations; +import java.util.List; import org.enso.table.data.column.storage.Storage; import org.enso.table.data.table.Column; import org.enso.table.data.table.problems.InvalidAggregation; -import java.util.List; - /*** * Aggregate Column computing the standard deviation of a group. */ @@ -25,7 +24,7 @@ public Calculation(double value) { private final Storage storage; private final boolean population; - public StandardDeviation(String name, Column column,boolean population) { + public StandardDeviation(String name, Column column, boolean population) { super(name, Storage.Type.DOUBLE); this.storage = column.getStorage(); this.population = population; @@ -34,12 +33,13 @@ public StandardDeviation(String name, Column column,boolean population) { @Override public Object aggregate(List indexes) { Calculation current = null; - for (int row: indexes) { + for (int row : indexes) { Object value = storage.getItemBoxed(row); if (value != null) { Double dValue = CastToDouble(value); if (dValue == null) { - this.addProblem(new InvalidAggregation(this.getName(), row, "Cannot convert to a number.")); + this.addProblem( + new InvalidAggregation(this.getName(), row, "Cannot convert to a number.")); return null; } @@ -48,12 +48,14 @@ public Object aggregate(List indexes) { } else { current.count++; current.total += dValue; - current.total_sqr += dValue*dValue; + current.total_sqr += dValue * dValue; } } } - return current == null ? null : - (population ? 1 : Math.sqrt(current.count / (current.count - 1.0))) * - Math.sqrt(current.total_sqr / current.count - Math.pow(current.total / current.count, 2)); + + if (current == null) return null; + if (!population && current.count <= 1) return null; + return (population ? 1 : Math.sqrt(current.count / (current.count - 1.0))) + * Math.sqrt(current.total_sqr / current.count - Math.pow(current.total / current.count, 2)); } } diff --git a/test/Table_Tests/src/Aggregate_Spec.enso b/test/Table_Tests/src/Aggregate_Spec.enso index 6dc04bab45c05..32ca77e5b980a 100644 --- a/test/Table_Tests/src/Aggregate_Spec.enso +++ b/test/Table_Tests/src/Aggregate_Spec.enso @@ -892,7 +892,7 @@ aggregate_spec prefix table empty_table table_builder materialize is_database te m1 = materialize r1 m1.columns.length . should_equal 2 m1.columns.first.at 0 . should_equal Nothing - m1.columns.first.at 0 . should_equal Nothing + m1.columns.second.at 0 . should_equal 0 Test.group prefix+"Table.aggregate should correctly select result types" pending=pending <| Test.specify " widening to decimals on Average" <| @@ -930,7 +930,6 @@ aggregate_spec prefix table empty_table table_builder materialize is_database te table = table_builder [["X", [1, 2, 3, 4, Nothing]]] r1 = table.aggregate [Standard_Deviation "X" (population=True), Standard_Deviation "X" (population=False)] r1.row_count.should_equal 1 - r1.print m1 = materialize r1 m1.columns.length . should_equal 2 m1.columns.first.at 0 . should_equal 1.1180339887499 epsilon=0.000001