From 4c2e3f6e524817e35d864da11e3a0a46fd348f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Tue, 14 Nov 2023 03:40:45 -0800 Subject: [PATCH] Prevent intersectionRatio from being higher than 1 in IntersectionObserverEntry (#41448) Summary: This was possible before due to precision problems with `double` (we were seeing values like 1.000000002). This is an easy way to prevent that problem. Changelog: [internal] Reviewed By: rshest Differential Revision: D51230183 --- .../IntersectionObserver/IntersectionObserverEntry.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/react-native/Libraries/IntersectionObserver/IntersectionObserverEntry.js b/packages/react-native/Libraries/IntersectionObserver/IntersectionObserverEntry.js index 043d66742564a1..23833face664a9 100644 --- a/packages/react-native/Libraries/IntersectionObserver/IntersectionObserverEntry.js +++ b/packages/react-native/Libraries/IntersectionObserver/IntersectionObserverEntry.js @@ -66,10 +66,12 @@ export default class IntersectionObserverEntry { return 0; } - return ( + const ratio = (intersectionRect.width * intersectionRect.height) / - (boundingClientRect.width * boundingClientRect.height) - ); + (boundingClientRect.width * boundingClientRect.height); + + // Prevent rounding errors from making this value greater than 1. + return Math.min(ratio, 1); } /**