Skip to content

Commit b7b0d5c

Browse files
authored
Add isNaN check for align behavior with JVM (#2526)
[CMP-8562](https://youtrack.jetbrains.com/issue/CMP-8562) iOS: "IllegalArgumentException: Cannot round NaN value" caused by `roundToInt()` ## Release Notes ### Fixes - Multiple Platforms - Align `roundToPx()` behavior between platforms: `NaN` value produces `0` instead of `IllegalArgumentException` on non-JVM platforms now
1 parent 869ceb9 commit b7b0d5c

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

compose/ui/ui-util/src/commonTest/kotlin/androidx/compose/ui/util/InlineClassHelperTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,10 @@ class InlineClassHelperTest {
102102
}
103103

104104
fun multZero(value: Float) = value * 0f
105+
106+
@Test
107+
fun fastRoundNaNToInt() {
108+
assertEquals(0, Float.NaN.fastRoundToInt())
109+
assertEquals(0, Double.NaN.fastRoundToInt())
110+
}
105111
}

compose/ui/ui-util/src/nonJvmMain/kotlin/androidx/compose/ui/util/InlineClassHelper.nonJvm.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ actual inline fun floatFromBits(bits: Int): Float = Float.fromBits(bits)
2424

2525
actual inline fun doubleFromBits(bits: Long): Double = Double.fromBits(bits)
2626

27-
actual inline fun Float.fastRoundToInt(): Int = roundToInt()
27+
actual inline fun Float.fastRoundToInt(): Int = if (isNaN()) 0 else roundToInt()
2828

29-
actual inline fun Double.fastRoundToInt(): Int = roundToInt()
29+
actual inline fun Double.fastRoundToInt(): Int = if (isNaN()) 0 else roundToInt()

0 commit comments

Comments
 (0)