Fix native implementation of Animated.modulo (#23973)

Summary:
fixes #23875
[ios,android][fixes] incorrect behavior of `Animated.modulo`

Use the same formula as used in js: `mod(a,m)=>(a % m + m) % m` (https://github.com/facebook/react-native/blob/master/Libraries/Animated/src/nodes/AnimatedModulo.js#L35)

Native implementation of `Animated.modulo` was different from what was used in javascript, more details available here: https://github.com/facebook/react-native/issues/23875

[iOS] [Fixed] incorrect behavior of Animated.modulo
[Android] [Fixed] incorrect behavior of Animated.modulo
Pull Request resolved: https://github.com/facebook/react-native/pull/23973

Differential Revision: D14502697

Pulled By: cpojer

fbshipit-source-id: befef2b99ae758f98459caaadc8ebdbbd547e69a
This commit is contained in:
Max Komarychev
2019-03-18 07:22:55 -07:00
committed by Facebook Github Bot
parent f541c34067
commit cc3f9a7538
2 changed files with 4 additions and 2 deletions

View File

@@ -15,7 +15,8 @@
NSNumber *inputNode = self.config[@"input"];
NSNumber *modulus = self.config[@"modulus"];
RCTValueAnimatedNode *parent = (RCTValueAnimatedNode *)[self.parentNodes objectForKey:inputNode];
self.value = fmodf(parent.value, modulus.floatValue);
const float m = modulus.floatValue;
self.value = fmodf(fmodf(parent.value, m) + m, m);
}
@end

View File

@@ -29,7 +29,8 @@ import com.facebook.react.bridge.ReadableMap;
public void update() {
AnimatedNode animatedNode = mNativeAnimatedNodesManager.getNodeById(mInputNode);
if (animatedNode != null && animatedNode instanceof ValueAnimatedNode) {
mValue = ((ValueAnimatedNode) animatedNode).getValue() % mModulus;
final double value = ((ValueAnimatedNode) animatedNode).getValue();
mValue = (value % mModulus + mModulus) % mModulus;
} else {
throw new JSApplicationCausedNativeException("Illegal node ID set as an input for " +
"Animated.modulus node");