Skip to content

Commit b378a68

Browse files
Georgiy Kassablifacebook-github-bot
authored andcommitted
Changed pointScaleFactor usage to avoid accumulating error
Summary: Previously pointScaleFactor was stored as a divider that opened way to accumulating difference. For instance in ScreenScale=3, pointScaleFactor = 0.3333343 (0.000001 error). When used for width = 300 that error was multiplied by the number of times pointScale contained in width (300 *3 = 900) and we had almost 0.001 error accumulated. With this change Yoga will avoid such issue Reviewed By: shergin Differential Revision: D5137923 fbshipit-source-id: 652b59bc3da3f35ee93ffa3695936f623298a023
1 parent fa50f04 commit b378a68

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

yoga/Yoga.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,19 +3148,19 @@ static float YGRoundValueToPixelGrid(const float value,
31483148
const float pointScaleFactor,
31493149
const bool forceCeil,
31503150
const bool forceFloor) {
3151-
float fractial = fmodf(value, pointScaleFactor);
3151+
float scaledValue = value * pointScaleFactor;
3152+
float fractial = fmodf(scaledValue, 1.0);
31523153
if (YGFloatsEqual(fractial, 0)) {
31533154
// Still remove fractial as fractial could be extremely small.
3154-
return value - fractial;
3155-
}
3156-
3157-
if (forceCeil) {
3158-
return value - fractial + pointScaleFactor;
3155+
scaledValue = scaledValue - fractial;
3156+
} else if (forceCeil) {
3157+
scaledValue = scaledValue - fractial + 1.0;
31593158
} else if (forceFloor) {
3160-
return value - fractial;
3159+
scaledValue = scaledValue - fractial;
31613160
} else {
3162-
return value - fractial + (fractial >= pointScaleFactor / 2.0f ? pointScaleFactor : 0);
3161+
scaledValue = scaledValue - fractial + (fractial >= 0.5f ? 1.0 : 0);
31633162
}
3163+
return scaledValue / pointScaleFactor;
31643164
}
31653165

31663166
bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode,
@@ -3427,7 +3427,7 @@ void YGConfigSetPointScaleFactor(const YGConfigRef config, const float pixelsInP
34273427
// Zero is used to skip rounding
34283428
config->pointScaleFactor = 0.0f;
34293429
} else {
3430-
config->pointScaleFactor = 1.0f / pixelsInPoint;
3430+
config->pointScaleFactor = pixelsInPoint;
34313431
}
34323432
}
34333433

0 commit comments

Comments
 (0)