diff --git a/build.gradle b/build.gradle index 14a5951..0d8b448 100644 --- a/build.gradle +++ b/build.gradle @@ -51,7 +51,7 @@ publishing { release(MavenPublication) { groupId = 'org.team4099' artifactId = 'falconutils' - version = '1.1.1' + version = '1.1.2' from(components["kotlin"]) } diff --git a/src/main/kotlin/org/team4099/lib/units/base/Percent.kt b/src/main/kotlin/org/team4099/lib/units/base/Percent.kt new file mode 100644 index 0000000..90f5731 --- /dev/null +++ b/src/main/kotlin/org/team4099/lib/units/base/Percent.kt @@ -0,0 +1,29 @@ +package org.team4099.lib.units.base + +import org.team4099.lib.units.Fraction +import org.team4099.lib.units.Unitless +import org.team4099.lib.units.Value + +typealias Decimal = Value + +typealias Rate = Value> + +const val PERCENTAGE_TO_DOUBLE = 0.01 + +inline val Double.percent: Decimal + get() = Decimal(this * PERCENTAGE_TO_DOUBLE) + +inline val Number.percent: Decimal + get() = this.toDouble().percent + +inline val Decimal.toDecimal: Double + get() = value + +inline val Decimal.toPercent: Double + get() = value / PERCENTAGE_TO_DOUBLE + +inline val Rate.inOutputPerSecond: Double + get() = value + +inline val Rate.inPercentPerSecond: Double + get() = value / PERCENTAGE_TO_DOUBLE diff --git a/src/test/kotlin/team4099/units/PercentTest.kt b/src/test/kotlin/team4099/units/PercentTest.kt new file mode 100644 index 0000000..6da9f95 --- /dev/null +++ b/src/test/kotlin/team4099/units/PercentTest.kt @@ -0,0 +1,25 @@ +package team4099.units + +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import org.team4099.lib.units.base.inOutputPerSecond +import org.team4099.lib.units.base.percent +import org.team4099.lib.units.base.seconds +import org.team4099.lib.units.base.toDecimal +import org.team4099.lib.units.perSecond + +class PercentTest { + private val kEpsilon = 1E-9 + + @Test + fun testPercentToDecimal() { + val percent = 50.0.percent + Assertions.assertEquals(percent.toDecimal, 0.5, kEpsilon) + } + + @Test + fun testRate() { + val rate = 50.0.percent / 2.seconds + Assertions.assertEquals(rate.inOutputPerSecond, 0.25, kEpsilon) + } +}