Skip to content

Commit d7c40d1

Browse files
committed
Extract Double.isIntegral() - a function that checks if a number is integral
1 parent 89cfe9b commit d7c40d1

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package de.ronny_h.aoc.extensions
2+
3+
import kotlin.math.floor
4+
5+
/**
6+
* Checks if a Double number is integral.
7+
*/
8+
fun Double.isIntegral() = floor(this) == this

src/main/kotlin/de/ronny_h/aoc/year24/day13/ClawContraption.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package de.ronny_h.aoc.year24.day13
22

33
import de.ronny_h.aoc.AdventOfCode
4-
import kotlin.math.floor
4+
import de.ronny_h.aoc.extensions.isIntegral
55

66
fun main() = ClawContraption().run(33427, 91649162972270)
77

@@ -76,5 +76,3 @@ class ClawContraption : AdventOfCode<Long>(2024, 13) {
7676
}
7777

7878
data class ClawMachine(val ax: Int, val ay: Int, val bx: Int, val by: Int, val x: Int, val y: Int)
79-
80-
private fun Double.isIntegral() = floor(this) == this
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.ronny_h.aoc.extensions
2+
3+
import io.kotest.core.spec.style.StringSpec
4+
import io.kotest.data.forAll
5+
import io.kotest.data.row
6+
import io.kotest.matchers.shouldBe
7+
8+
class IntegralNumbersTest : StringSpec({
9+
10+
"isIntegral is true for integral numbers" {
11+
forAll(
12+
row(0.0),
13+
row(7.000000000),
14+
row(42.0),
15+
row(70000001.0),
16+
) { value ->
17+
value.isIntegral() shouldBe true
18+
}
19+
}
20+
21+
"isIntegral is false for non-integral numbers" {
22+
forAll(
23+
row(0.5),
24+
row(0.0000001),
25+
row(7.0000001),
26+
row(7.9),
27+
) { value ->
28+
value.isIntegral() shouldBe false
29+
}
30+
}
31+
})

0 commit comments

Comments
 (0)