question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Floating point precision runs into weird behavior in looping mode

See original GitHub issue

#286 # Is this a bug report, a feature request, or a question?

bug report

Have you followed the required steps before opening a bug report?

(Check the step you’ve followed - put an x character between the square brackets ([]).)

Have you made sure that it wasn’t a React Native bug?

I can’t find anything about this issue.

Is the bug specific to iOS or Android? Or can it be reproduced on both platforms?

iOS 12.2 Android Simulator on Snack

Is the bug reproductible in a production environment (not a debug one)?

Yes.

Environment

Environment: React: 16.5.0 React native: 0.57 (Expo SDK 32.0.0) react-native-snap-carousel: 3.8.0

Target Platform: Android (5.0) iOS (9.2)

Expected Behavior

The slides looping on both sides endlessly.

Actual Behavior

The slides loop only once (regarding loopClonesPerSide) on the right side (swiping to left). On the other side, everything works as intended.

floating_error_carousel

Reproducible Demo

https://snack.expo.io/@thehacky/floating-point-error-in-carousel

Steps to Reproduce

  1. Swipe to left until no item will be rendered

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

2reactions
junhoyeocommented, Jul 29, 2020

@TheHacky Sorry, I don’t understand you very much. Is there any code for me to see?

If you calculate your sizes with a percentage, e.g.

width: '50%'

or with a package like react-native-responsive-dimensions:

width: responsiveWidth(50)

then you got floating numbers. Instead, I use Math.floor to round up the value

width: Math.floor(responsiveWidth(50))

I solved this issue using this comment’s solution - by providing integer values in sliderWidth and itemWidth properties in Carousel component. So my code is like this:

const carouselItemWidth = Math.floor(screenWidth * 0.68);

<Carousel
  sliderWidth={carouselItemWidth}
  itemWidth={carouselItemWidth}
  {...otherProps}
/>
1reaction
TheHackycommented, Aug 1, 2019

@TheHacky Sorry, I don’t understand you very much. Is there any code for me to see?

If you calculate your sizes with a percentage, e.g.

width: '50%'

or with a package like react-native-responsive-dimensions:

width: responsiveWidth(50)

then you got floating numbers. Instead, I use Math.floor to round up the value

width: Math.floor(responsiveWidth(50))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Floating point precision runs into weird behavior in looping mode
Floating point precision runs into weird behavior in looping mode.
Read more >
c++ - Bizarre floating-point behavior with vs. without extra ...
Hypothesis: It is a bug. The compiler converts double to unsigned long long correctly but converts extended-precision floating-point (possibly ...
Read more >
What Every Computer Scientist Should Know About ...
If the result of a floating-point computation is 3.12 × 10 -2, and the answer when computed to infinite precision is .0314, it...
Read more >
Intermediate Floating-Point Precision | Random ASCII
It turns out that the x87 FPU has a precision setting. This can be set to 24-bit (float), 53-bit (double), or 64-bit (long...
Read more >
Explanation to weird/strange floating point behaviour in C++
Note that in this mode numbers are temporarily stored as long doubles while being operated on, this can / will cause a kind...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found