Skip to content

Instantly share code, notes, and snippets.

@ghasemdev
Last active January 3, 2022 09:24
Show Gist options
  • Save ghasemdev/3e2cc7a97b1a7648033122f81855c642 to your computer and use it in GitHub Desktop.
Save ghasemdev/3e2cc7a97b1a7648033122f81855c642 to your computer and use it in GitHub Desktop.
Operator Functions
import kotlin.math.pow
fun main() {
println("^" * 5) // "^^^^^"
println(5 `^` 3) // 125.0
println(199 `%%` 24) // (24 * 199) / 100 = 47.76
println(5 / 2) // 2
println(5 `÷` 2) // 2.5
println(1.99999 == 2) // false
println(1.99999 `~` 2) // true
}
infix operator fun String.times(repeat: Int) = repeat(repeat)
infix fun <T : Number> T.`^`(other: T) = toDouble().pow(other.toDouble())
infix fun <T : Number> T.`%%`(percentage: T) = (percentage.toDouble() * toDouble()) / 100
infix fun <T : Number> T.`÷`(other: T) = toDouble() / other.toDouble()
infix fun <T : Number> T.`~`(other: T) = round(this.toDouble()) == other.toDouble()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment