-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5921b67
commit f79db55
Showing
3 changed files
with
55 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.jetbrains.bio.viktor | ||
|
||
import org.junit.Test | ||
import java.util.* | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class MoreMathTest { | ||
@Test fun testLogAddExpEdgeCases() { | ||
val r = Random() | ||
val logx = -Math.abs(r.nextDouble()) | ||
assertEquals(logx, Double.NEGATIVE_INFINITY logAddExp logx) | ||
assertEquals(logx, logx logAddExp Double.NEGATIVE_INFINITY) | ||
assertEquals(Double.NEGATIVE_INFINITY, | ||
Double.NEGATIVE_INFINITY logAddExp Double.NEGATIVE_INFINITY) | ||
} | ||
} | ||
|
||
class KahanSumTest { | ||
@Test fun testPrecision() { | ||
val bigNumber = 10000000 | ||
for (d in 9..15) { | ||
// note that in each case 1/d is not precisely representable as a double, | ||
// which is bound to lead to accumulating rounding errors. | ||
val oneDth = 1.0 / d | ||
val preciseSum = KahanSum() | ||
var impreciseSum = 0.0 | ||
for (i in 0..bigNumber * d - 1) { | ||
preciseSum += oneDth | ||
impreciseSum += oneDth | ||
} | ||
|
||
val imprecision = Math.abs(impreciseSum - bigNumber) | ||
val precision = Math.abs(preciseSum.result() - bigNumber) | ||
assertTrue(imprecision >= precision, | ||
"Kahan's algorithm yielded worse precision than ordinary summation: " + | ||
"$precision is greater than $imprecision") | ||
} | ||
} | ||
} |