-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
3 changed files
with
55 additions
and
1 deletion.
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 |
---|---|---|
@@ -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 |
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,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) | ||
} | ||
} |