|
1 | 1 | /*
|
2 |
| - * Copyright (c) Facebook, Inc. and its affiliates. |
| 2 | + * Copyright (c) 2018-present, Facebook, Inc. |
3 | 3 | *
|
4 | 4 | * This source code is licensed under the MIT license found in the LICENSE
|
5 | 5 | * file in the root directory of this source tree.
|
6 | 6 | *
|
7 | 7 | */
|
8 |
| - |
9 | 8 | #include "Yoga.h"
|
10 | 9 | #include <float.h>
|
11 | 10 | #include <string.h>
|
@@ -3608,7 +3607,27 @@ float YGRoundValueToPixelGrid(
|
3608 | 3607 | const bool forceCeil,
|
3609 | 3608 | const bool forceFloor) {
|
3610 | 3609 | float scaledValue = value * pointScaleFactor;
|
| 3610 | + // We want to calculate `fractial` such that `floor(scaledValue) = scaledValue |
| 3611 | + // - fractial`. |
3611 | 3612 | float fractial = fmodf(scaledValue, 1.0f);
|
| 3613 | + if (fractial < 0) { |
| 3614 | + // This branch is for handling negative numbers for `value`. |
| 3615 | + // |
| 3616 | + // Regarding `floor` and `ceil`. Note that for a number x, `floor(x) <= x <= |
| 3617 | + // ceil(x)` even for negative numbers. Here are a couple of examples: |
| 3618 | + // - x = 2.2: floor( 2.2) = 2, ceil( 2.2) = 3 |
| 3619 | + // - x = -2.2: floor(-2.2) = -3, ceil(-2.2) = -2 |
| 3620 | + // |
| 3621 | + // Regarding `fmodf`. For fractional negative numbers, `fmodf` returns a |
| 3622 | + // negative number. For example, `fmodf(-2.2) = -0.2`. However, we want |
| 3623 | + // `fractial` to be the number such that subtracting it from `value` will |
| 3624 | + // give us `floor(value)`. In the case of negative numbers, adding 1 to |
| 3625 | + // `fmodf(value)` gives us this. Let's continue the example from above: |
| 3626 | + // - fractial = fmodf(-2.2) = -0.2 |
| 3627 | + // - Add 1 to the fraction: fractial2 = fractial + 1 = -0.2 + 1 = 0.8 |
| 3628 | + // - Finding the `floor`: -2.2 - fractial2 = -2.2 - 0.8 = -3 |
| 3629 | + ++fractial; |
| 3630 | + } |
3612 | 3631 | if (YGFloatsEqual(fractial, 0)) {
|
3613 | 3632 | // First we check if the value is already rounded
|
3614 | 3633 | scaledValue = scaledValue - fractial;
|
|
0 commit comments