Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallax animation with new architecture Enabled cause "Loss of precision during arithmetic conversion: (long) XX.XXXX" #715

Closed
jiroscripts opened this issue Nov 5, 2024 · 5 comments · Fixed by #716
Assignees
Labels
bug Something isn't working

Comments

@jiroscripts
Copy link
Contributor

jiroscripts commented Nov 5, 2024

Describe the bug

If I use the parallax mode with new architecture enabled it cause the following issue while animation is playing.

To Reproduce
Steps to reproduce the behavior:

  1. Enable new architecture (See the doc)
  2. Go to example expo app
  3. Click on the parallax demo
  4. Enable autoplay
  5. Wait and see

Expected behavior

The demo should run without error

Versions (please complete the following information):

  • "react": "18.2.0",
  • "react-native": "0.74.5",
  • "react-native-reanimated": "~3.10.1",
  • "react-native-reanimated-carousel": "4.0.0-canary.17",
  • "react-native-gesture-handler": "~2.16.1",

Smartphone (please complete the following information):

  • Device: Pixel 7a
  • OS: Android 14

References:
Related to #712

@jiroscripts jiroscripts added the bug Something isn't working label Nov 5, 2024
Copy link

dosubot bot commented Nov 5, 2024

Hello, @thibaultcapelli! I'm here to help you with any bugs, questions, or contributions. Let's work together to solve the issue you're experiencing.

A similar issue was discussed in a closed thread regarding the "Loss of precision during arithmetic conversion" error when using parallax animation with the new architecture enabled [1]. To resolve this, ensure that all numeric values used in the interpolate function and other calculations are explicitly defined as floating-point numbers. This can be done by appending a decimal point to integer literals or using floating-point literals directly. Here's an example of how you can adjust your code:

const translateY = interpolate(
  value,
  [-1, 0, 1],
  [-size + parallaxScrollingOffset, 0, size - parallaxScrollingOffset],
);

const translateX = interpolate(
  value,
  [-1, 0, 1, 2],
  [-size * 0.2, 0, 0, -size * 0.2],
);

const zIndex = interpolate(
  value,
  [-1, 0, 1, 2],
  [0, size, size, 0],
  Extrapolation.CLAMP,
);

const scale = interpolate(
  value,
  [-1, 0, 1, 2],
  [
    parallaxAdjacentItemScale,
    parallaxScrollingScale,
    parallaxScrollingScale,
    parallaxAdjacentItemScale,
  ],
  Extrapolation.CLAMP,
);

const transform = {
  transform: [
    { translateY },
    { translateX },
    { perspective: 200.0 }, // Ensure this is a floating-point number
    {
      rotateY: `${interpolate(
        value,
        [-1, 0, 1, 2],
        [20.0, 0.0, 0.0, 20.0], // Use floating-point numbers
        Extrapolation.CLAMP,
      )}deg`,
    },
    {
      rotateZ: `${interpolate(
        value,
        [-1, 0, 1, 2],
        [-20.0, 0.0, 0.0, -20.0], // Use floating-point numbers
        Extrapolation.CLAMP,
      )}deg`,
    },
    { scale },
  ],
};

By ensuring that all numbers are treated as floating-point, you can avoid precision loss during arithmetic conversions [2].

To continue talking to Dosu, mention @dosu.

@Bekaxp
Copy link

Bekaxp commented Nov 6, 2024

We can link them, as it looks like the same issue => #712 , thanks.

@jiroscripts
Copy link
Contributor Author

We can link them, as it looks like the same issue => #712 , thanks.

Thanks, I missed your issue. Take a look to the PR. I'm using yarn patch for now

@Bekaxp
Copy link

Bekaxp commented Nov 9, 2024

I applied the patch. I can confirm that it works. Thanks for a quick solution and your lib. 👍

@usmanabid94
Copy link

@Bekaxp @dohooo @thibaultcapelli @rdewolff @berdyshev I have updated to the below mentioned version and still face the same issue
"react-native-reanimated": "^3.16.1",
"react-native-reanimated-carousel": "4.0.0-canary.17",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants