-
Notifications
You must be signed in to change notification settings - Fork 1
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
ed66170
commit 73ec3d3
Showing
10 changed files
with
149 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
package com.github.wakingrufus.elo | ||
|
||
import java.math.BigDecimal | ||
import java.math.MathContext | ||
import java.math.RoundingMode | ||
|
||
fun pow(base: BigDecimal, exponent: BigDecimal): BigDecimal { | ||
var result = BigDecimal.ZERO | ||
fun BigDecimal.pow( exponent: BigDecimal): BigDecimal { | ||
val signOf2 = exponent.signum() | ||
|
||
// Perform X^(A+B)=X^A*X^B (B = remainder) | ||
val dn1 = base.toDouble() | ||
val dn1 = this.toDouble() | ||
// Compare the same row of digits according to context | ||
val n2 = exponent.multiply(BigDecimal(signOf2)) // n2 is now positive | ||
val remainderOf2 = n2.remainder(BigDecimal.ONE) | ||
val n2IntPart = n2.subtract(remainderOf2) | ||
// Calculate big part of the power using context - | ||
// bigger range and performance but lower accuracy | ||
val intPow = base.pow(n2IntPart.intValueExact()) | ||
val intPow = this.pow(n2IntPart.intValueExact()) | ||
val doublePow = BigDecimal(Math.pow(dn1, remainderOf2.toDouble())) | ||
result = intPow.multiply(doublePow) | ||
var result = intPow.multiply(doublePow) | ||
|
||
// Fix negative power | ||
if (signOf2 == -1) | ||
result = BigDecimal.ONE.divide(result, RoundingMode.HALF_UP) | ||
result = BigDecimal.ONE.divide(result, MathContext.DECIMAL64) | ||
return result | ||
} |
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
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
37 changes: 37 additions & 0 deletions
37
src/test/kotlin/com/github/wakingrufus/elo/BigDecimalKtTest.kt
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,37 @@ | ||
package com.github.wakingrufus.elo | ||
|
||
import org.junit.Test | ||
import java.math.BigDecimal | ||
import kotlin.test.assertTrue | ||
|
||
class BigDecimalKtTest { | ||
@Test | ||
fun pow() { | ||
2 `to the` 3 equals 8 | ||
10 `to the` 3 equals 1000 | ||
10 `to the` 1.5 equals BigDecimal("31.622776601683795227870632515987381339073181152343750") | ||
5 `to the` 0 equals 1 | ||
5 `to the` 1 equals 5 | ||
10 `to the` -2 equals BigDecimal("0.01") | ||
10 `to the` -1 equals BigDecimal("0.1") | ||
} | ||
|
||
infix fun Int.`to the`(exp: Int): BigDecimal { | ||
return this.toBigDecimal().pow(exp.toBigDecimal()) | ||
} | ||
|
||
infix fun Int.`to the`(exp: Double): BigDecimal { | ||
return this.toBigDecimal().pow(exp.toBigDecimal()) | ||
} | ||
|
||
infix fun Number.equals(result: Int): Unit { | ||
assertTrue(BigDecimal(this.toString()).compareTo(result.toBigDecimal()) == 0, | ||
"expected ${this} but was $result") | ||
} | ||
|
||
infix fun Number.equals(expected: BigDecimal): Unit { | ||
assertTrue(BigDecimal(this.toString()).compareTo(expected) == 0, | ||
"expected $expected but was $this") | ||
} | ||
|
||
} |
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
54 changes: 54 additions & 0 deletions
54
src/test/kotlin/com/github/wakingrufus/elo/PlayerUtilKtTest.kt
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,54 @@ | ||
package com.github.wakingrufus.elo | ||
|
||
import org.junit.Assert.assertArrayEquals | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
class PlayerUtilKtTest { | ||
@Test | ||
fun `test calculateTeamRating`() { | ||
assertEquals("should return average of ratings", | ||
1500, | ||
calculateTeamRating(listOf( | ||
Player(id = "1", currentRating = 1400), | ||
Player(id = "2", currentRating = 1600) | ||
))) | ||
} | ||
|
||
@Test | ||
fun `test calculateTeamAverageGamesPlayed`() { | ||
assertEquals("returns average", | ||
3, | ||
calculateTeamGamesPlayed(listOf( | ||
Player(id = "1", currentRating = 1400, gamesPlayed = 2), | ||
Player(id = "2", currentRating = 1600, gamesPlayed = 4) | ||
))) | ||
assertEquals("returns average truncated", | ||
3, | ||
calculateTeamGamesPlayed(listOf( | ||
Player(id = "1", currentRating = 1400, gamesPlayed = 3), | ||
Player(id = "2", currentRating = 1600, gamesPlayed = 4) | ||
))) | ||
} | ||
|
||
@Test | ||
fun `test buildTeam`() { | ||
val p1 = Player(id = "1", currentRating = 1550) | ||
val p2 = Player(id = "2", currentRating = 1000) | ||
assertArrayEquals(listOf(p1).toTypedArray(), | ||
buildTeam( | ||
mapOf("1" to p1, "2" to p2), | ||
listOf("1") | ||
).toTypedArray()) | ||
} | ||
|
||
@Test(expected = RuntimeException::class) | ||
fun `test buildTeam with missing id`() { | ||
val p1 = Player(id = "1", currentRating = 1550) | ||
val p2 = Player(id = "2", currentRating = 1000) | ||
buildTeam( | ||
mapOf("1" to p1, "2" to p2), | ||
listOf("1", "3") | ||
) | ||
} | ||
} |