use fmod and YGDoubleEquals for double operations instead of float
Summary: Changelog: [Internal][Yoga] Use double operations during rounding Reviewed By: mdvacca Differential Revision: D21840018 fbshipit-source-id: c5d17fcb8984b1da9832a15ccd4d628e8d742c6a
This commit is contained in:
committed by
Facebook GitHub Bot
parent
ede65bbce4
commit
633cdc9088
@@ -52,6 +52,13 @@ bool YGFloatsEqual(const float a, const float b) {
|
||||
return yoga::isUndefined(a) && yoga::isUndefined(b);
|
||||
}
|
||||
|
||||
bool YGDoubleEqual(const double a, const double b) {
|
||||
if (!yoga::isUndefined(a) && !yoga::isUndefined(b)) {
|
||||
return fabs(a - b) < 0.0001f;
|
||||
}
|
||||
return yoga::isUndefined(a) && yoga::isUndefined(b);
|
||||
}
|
||||
|
||||
float YGFloatSanitize(const float val) {
|
||||
return yoga::isUndefined(val) ? 0 : val;
|
||||
}
|
||||
|
@@ -64,6 +64,8 @@ inline bool YGValueEqual(
|
||||
// difference between two floats is less than 0.0001f or both are undefined.
|
||||
bool YGFloatsEqual(const float a, const float b);
|
||||
|
||||
bool YGDoubleEqual(const double a, const double b);
|
||||
|
||||
float YGFloatMax(const float a, const float b);
|
||||
|
||||
YGFloatOptional YGFloatOptionalMax(
|
||||
|
@@ -3668,7 +3668,7 @@ YOGA_EXPORT float YGRoundValueToPixelGrid(
|
||||
double scaledValue = ((double) value) * pointScaleFactor;
|
||||
// We want to calculate `fractial` such that `floor(scaledValue) = scaledValue
|
||||
// - fractial`.
|
||||
float fractial = fmodf(scaledValue, 1.0f);
|
||||
double fractial = fmod(scaledValue, 1.0f);
|
||||
if (fractial < 0) {
|
||||
// This branch is for handling negative numbers for `value`.
|
||||
//
|
||||
@@ -3687,10 +3687,10 @@ YOGA_EXPORT float YGRoundValueToPixelGrid(
|
||||
// - Finding the `floor`: -2.2 - fractial2 = -2.2 - 0.8 = -3
|
||||
++fractial;
|
||||
}
|
||||
if (YGFloatsEqual(fractial, 0)) {
|
||||
if (YGDoubleEqual(fractial, 0)) {
|
||||
// First we check if the value is already rounded
|
||||
scaledValue = scaledValue - fractial;
|
||||
} else if (YGFloatsEqual(fractial, 1.0f)) {
|
||||
} else if (YGDoubleEqual(fractial, 1.0f)) {
|
||||
scaledValue = scaledValue - fractial + 1.0f;
|
||||
} else if (forceCeil) {
|
||||
// Next we check if we need to use forced rounding
|
||||
@@ -3701,7 +3701,7 @@ YOGA_EXPORT float YGRoundValueToPixelGrid(
|
||||
// Finally we just round the value
|
||||
scaledValue = scaledValue - fractial +
|
||||
(!YGFloatIsUndefined(fractial) &&
|
||||
(fractial > 0.5f || YGFloatsEqual(fractial, 0.5f))
|
||||
(fractial > 0.5f || YGDoubleEqual(fractial, 0.5f))
|
||||
? 1.0f
|
||||
: 0.0f);
|
||||
}
|
||||
@@ -4113,11 +4113,11 @@ static void YGRoundToPixelGrid(
|
||||
// whole number, we don't have any fraction To verify if the result is close
|
||||
// to whole number we want to check both floor and ceil numbers
|
||||
const bool hasFractionalWidth =
|
||||
!YGFloatsEqual(fmodf(nodeWidth * pointScaleFactor, 1.0), 0) &&
|
||||
!YGFloatsEqual(fmodf(nodeWidth * pointScaleFactor, 1.0), 1.0);
|
||||
!YGDoubleEqual(fmod(nodeWidth * pointScaleFactor, 1.0), 0) &&
|
||||
!YGDoubleEqual(fmod(nodeWidth * pointScaleFactor, 1.0), 1.0);
|
||||
const bool hasFractionalHeight =
|
||||
!YGFloatsEqual(fmodf(nodeHeight * pointScaleFactor, 1.0), 0) &&
|
||||
!YGFloatsEqual(fmodf(nodeHeight * pointScaleFactor, 1.0), 1.0);
|
||||
!YGDoubleEqual(fmod(nodeHeight * pointScaleFactor, 1.0), 0) &&
|
||||
!YGDoubleEqual(fmod(nodeHeight * pointScaleFactor, 1.0), 1.0);
|
||||
|
||||
node->setLayoutDimension(
|
||||
YGRoundValueToPixelGrid(
|
||||
|
Reference in New Issue
Block a user