Skip to content

Commit a3ae58f

Browse files
committedAug 5, 2017
1 parent ca0140f commit a3ae58f

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed
 

Diff for: ‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ The port is finished. The code works and passess all Yoga tests.
2222

2323
The API is awkward by Go standards but it's the best I could do given that I want to stay close to C version.
2424

25-
Logic is currently synced up to https://github.com/facebook/yoga/commit/c9384762ee367f890a3de57ff3270d8f9c445866
25+
Logic is currently synced up to https://github.com/facebook/yoga/commit/f45059e1e696727c1282742b89d2c8bf06345254

Diff for: ‎rounding_function_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package flex
2+
3+
import "testing"
4+
5+
func TestRounding_value(t *testing.T) {
6+
7+
// Test that whole numbers are rounded to whole despite ceil/floor flags
8+
assertFloatEqual(t, 6.0, roundValueToPixelGrid(6.000001, 2.0, false, false))
9+
assertFloatEqual(t, 6.0, roundValueToPixelGrid(6.000001, 2.0, true, false))
10+
assertFloatEqual(t, 6.0, roundValueToPixelGrid(6.000001, 2.0, false, true))
11+
assertFloatEqual(t, 6.0, roundValueToPixelGrid(5.999999, 2.0, false, false))
12+
assertFloatEqual(t, 6.0, roundValueToPixelGrid(5.999999, 2.0, true, false))
13+
assertFloatEqual(t, 6.0, roundValueToPixelGrid(5.999999, 2.0, false, true))
14+
15+
// Test that numbers with fraction are rounded correctly accounting for ceil/floor flags
16+
assertFloatEqual(t, 6.0, roundValueToPixelGrid(6.01, 2.0, false, false))
17+
assertFloatEqual(t, 6.5, roundValueToPixelGrid(6.01, 2.0, true, false))
18+
assertFloatEqual(t, 6.0, roundValueToPixelGrid(6.01, 2.0, false, true))
19+
assertFloatEqual(t, 6.0, roundValueToPixelGrid(5.99, 2.0, false, false))
20+
assertFloatEqual(t, 6.0, roundValueToPixelGrid(5.99, 2.0, true, false))
21+
assertFloatEqual(t, 5.5, roundValueToPixelGrid(5.99, 2.0, false, true))
22+
}

Diff for: ‎yoga.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -2655,13 +2655,17 @@ func roundValueToPixelGrid(value float32, pointScaleFactor float32, forceCeil bo
26552655
scaledValue := value * pointScaleFactor
26562656
fractial := fmodf(scaledValue, 1.0)
26572657
if FloatsEqual(fractial, 0) {
2658-
// Still remove fractial as fractial could be extremely small.
2658+
// First we check if the value is already rounded
26592659
scaledValue = scaledValue - fractial
2660+
} else if FloatsEqual(fractial, 1.0) {
2661+
scaledValue = scaledValue - fractial + 1.0
26602662
} else if forceCeil {
2663+
// Next we check if we need to use forced rounding
26612664
scaledValue = scaledValue - fractial + 1.0
26622665
} else if forceFloor {
26632666
scaledValue = scaledValue - fractial
26642667
} else {
2668+
// Finally we just round the value
26652669
var f float32
26662670
if fractial >= 0.5 {
26672671
f = 1.0

0 commit comments

Comments
 (0)
Please sign in to comment.