Skip to content

Commit

Permalink
add percent units
Browse files Browse the repository at this point in the history
  • Loading branch information
sswadkar committed Jan 23, 2023
1 parent fe60d70 commit fc634ff
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ publishing {
release(MavenPublication) {
groupId = 'org.team4099'
artifactId = 'falconutils'
version = '1.1.1'
version = '1.1.2'

from(components["kotlin"])
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/kotlin/org/team4099/lib/units/base/Percent.kt
Original file line number Diff line number Diff line change
@@ -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<Unitless>

typealias Rate = Value<Fraction<Unitless, Second>>

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
25 changes: 25 additions & 0 deletions src/test/kotlin/team4099/units/PercentTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit fc634ff

Please sign in to comment.