Skip to content

Commit

Permalink
C4244 possible precision loss warning fix
Browse files Browse the repository at this point in the history
Summary:
Fixing `warning C4244: 'argument': conversion from 'double' to 'float', possible loss of data`

Changelog: [Internal]

Reviewed By: SidharthGuglani

Differential Revision: D27132355

fbshipit-source-id: 55ff35be368ef4f6093865eb88c17e753250d179
  • Loading branch information
altaibayar authored and facebook-github-bot committed Mar 20, 2021
1 parent 67b6c24 commit db6be52
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion yoga/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bool YGFloatsEqual(const float a, const float b) {

bool YGDoubleEqual(const double a, const double b) {
if (!yoga::isUndefined(a) && !yoga::isUndefined(b)) {
return fabs(a - b) < 0.0001f;
return fabs(a - b) < 0.0001;
}
return yoga::isUndefined(a) && yoga::isUndefined(b);
}
Expand Down
4 changes: 4 additions & 0 deletions yoga/Yoga-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ inline bool isUndefined(float value) {
return std::isnan(value);
}

inline bool isUndefined(double value) {
return std::isnan(value);
}

} // namespace yoga
} // namespace facebook

Expand Down
28 changes: 16 additions & 12 deletions yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ static int YGDefaultLog(
#undef YG_UNUSED
#endif

static inline bool YGDoubleIsUndefined(const double value) {
return facebook::yoga::isUndefined(value);
}

YOGA_EXPORT bool YGFloatIsUndefined(const float value) {
return facebook::yoga::isUndefined(value);
}
Expand Down Expand Up @@ -3620,10 +3624,10 @@ YOGA_EXPORT float YGRoundValueToPixelGrid(
const double pointScaleFactor,
const bool forceCeil,
const bool forceFloor) {
double scaledValue = ((double) value) * pointScaleFactor;
double scaledValue = value * pointScaleFactor;
// We want to calculate `fractial` such that `floor(scaledValue) = scaledValue
// - fractial`.
double fractial = fmod(scaledValue, 1.0f);
double fractial = fmod(scaledValue, 1.0);
if (fractial < 0) {
// This branch is for handling negative numbers for `value`.
//
Expand All @@ -3645,25 +3649,25 @@ YOGA_EXPORT float YGRoundValueToPixelGrid(
if (YGDoubleEqual(fractial, 0)) {
// First we check if the value is already rounded
scaledValue = scaledValue - fractial;
} else if (YGDoubleEqual(fractial, 1.0f)) {
scaledValue = scaledValue - fractial + 1.0f;
} else if (YGDoubleEqual(fractial, 1.0)) {
scaledValue = scaledValue - fractial + 1.0;
} else if (forceCeil) {
// Next we check if we need to use forced rounding
scaledValue = scaledValue - fractial + 1.0f;
scaledValue = scaledValue - fractial + 1.0;
} else if (forceFloor) {
scaledValue = scaledValue - fractial;
} else {
// Finally we just round the value
scaledValue = scaledValue - fractial +
(!YGFloatIsUndefined(fractial) &&
(fractial > 0.5f || YGDoubleEqual(fractial, 0.5f))
? 1.0f
: 0.0f);
(!YGDoubleIsUndefined(fractial) &&
(fractial > 0.5 || YGDoubleEqual(fractial, 0.5))
? 1.0
: 0.0);
}
return (YGFloatIsUndefined(scaledValue) ||
YGFloatIsUndefined(pointScaleFactor))
return (YGDoubleIsUndefined(scaledValue) ||
YGDoubleIsUndefined(pointScaleFactor))
? YGUndefined
: scaledValue / pointScaleFactor;
: (float) (scaledValue / pointScaleFactor);
}

YOGA_EXPORT bool YGNodeCanUseCachedMeasurement(
Expand Down

0 comments on commit db6be52

Please sign in to comment.