Skip to content

Commit

Permalink
fixup! fix: EXPOSED-117 Set jvmToolchain to 8 for all modules
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Sep 13, 2023
1 parent 4f268b4 commit 00eac07
Showing 1 changed file with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class StatisticsFunctionTests : DatabaseTestsBase() {
private object SampleTestTable : Table("sample_table") {
val number = integer("number").nullable()
}

private val data: List<Int?> = listOf(4, null, 5, null, 6)
private val scale = 4

Expand All @@ -63,7 +64,38 @@ class StatisticsFunctionTests : DatabaseTestsBase() {
}

private fun calculateStandardDeviation(isPopulation: Boolean): BigDecimal {
return calculateVariance(isPopulation).sqrt(MathContext(scale, RoundingMode.HALF_EVEN))
return calculateVariance(isPopulation).simpleSqrt()
}

fun BigDecimal.simpleSqrt(): BigDecimal {
if (this < BigDecimal.ZERO) throw ArithmeticException("Square root of negative number")
if (this == BigDecimal.ZERO) return BigDecimal.ZERO

val TWO = BigDecimal(2)
val EPSILON = BigDecimal(0.1).pow(scale)

var low = BigDecimal.ZERO
var high = max(BigDecimal.ONE)
var result = (low + high).divide(TWO)

while (true) {
val square = result.multiply(result)
val diff = square.subtract(this).abs()
if (diff < EPSILON) {
break
}

if (result.multiply(result) < this) {
low = result
} else {
high = result
}
result = (low + high).divide(TWO)
}

result = result.round(MathContext(scale, RoundingMode.HALF_EVEN))
result = result.setScale(scale)
return result
}

private fun calculateVariance(isPopulation: Boolean): BigDecimal {
Expand Down

0 comments on commit 00eac07

Please sign in to comment.