What method exists on multiplatform to check if tw...
# multiplatform
m
What method exists on multiplatform to check if two floats are "pretty much equivalent"? e.g.
0.34657359027997264
and
3465735902799727
should be practically equivalent. I know in kotlin.test we have checkFloatsAreEqual, but without including kotlin.test it seems impossible to figure out
j
val prettyMuchEquivalent = second < first+delta && second > first-delta
val prettyMuchEquivalent = abs(first - second) < delta
p
Reminds me of the old JUnit helper method we had -
assertColorAlmostEqualTo(…)
k
If this is being used for tests,
kotlin.test
has the following two functions which include a tolerance for equality of floating point numbers.
Copy code
/** Asserts that the difference between the [actual] and the [expected] is within an [absoluteTolerance], with an optional [message]. */
@SinceKotlin("1.5")
fun assertEquals(expected: Double, actual: Double, absoluteTolerance: Double, message: String? = null) {
    checkDoublesAreEqual(expected, actual, absoluteTolerance, message)
}

/** Asserts that the difference between the [actual] and the [expected] is within an [absoluteTolerance], with an optional [message]. */
@SinceKotlin("1.5")
fun assertEquals(expected: Float, actual: Float, absoluteTolerance: Float, message: String? = null) {
    checkFloatsAreEqual(expected, actual, absoluteTolerance, message)
}
j
you can also just look at what kotlin.test does and do that
it's pretty basic
k
Oh lol I didn’t finish reading the top level message
I know in kotlin.test we have checkFloatsAreEqual, but without including kotlin.test it seems impossible to figure out