Layout bug caused by floating point rounding error #749
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Report
Issues and Steps to Reproduce
Expected Behavior
The text on the label should be displayed without problem
Actual Behavior
The text got truncated.

Link to Code
Example project to reproduce the issue https://github.com/leafduo/yoga-rounding
The problem was caused by the accumulation of floating point rounding error in complex layout. The view tree in the example has a height of 4, and causes floating point rounding errors to accumulate. In
YGRoundValueToPixelGrid
, thefractial
variable is 0.999877929 and is considered not a whole integer. In the end, the width of the label is one pixel short, causing the text truncation.This issue is presented when
pointScaleFactor
is 3 (not 1 or 2), and perhaps it's because 1) 3 is bigger and will cause more error, and 2) multiply and division by 2 is pretty accurate because the binary nature of computer.Using
double
instead offloat
to represent points, or raising the threshold inYGFloatsEqual
will make the text fully displayed in the example, but not solve this problem completely. It will still exist in a more complex layout.I think maybe we can store scaled value or use a real fraction representation?
I think this problem is the same as mine:
https://github.com/facebook/yoga/issues/738
how about
YGConfigSetPointScaleFactor(globalConfig, 0);
?@LeoSchleicher It appears so.
@linqingmo I've tried to set
pointScaleFactor
to 0, and the truncation will be solved. But will it cause view not align to pixel grid and become blurry?You might want to try with these changes : https://github.com/facebook/yoga/pull/688
@jmaurice-unity It's not related, #688 is about bug when rounding negative numbers, my coordinates are all positive.
Can I set YGConfigSetPointScaleFactor outside of Yoga sources? How to get globalConfig?
I believe I am running into this issue as well. If I set the
pointScaleFactor
to 0, then widths are calculated correctly. If not, continuous updates lead to constant shrinking of elements. I have an example of this running here: https://codesandbox.io/s/qxnnm7272j我尝试了一下,字符串后面加一个空格可以解决 😅
I tried it, and add a space after string. hhhh
I'm frequently seeing this (or a similar?) issue on text nodes with lots of siblings. Node height gets rounded down, and iOS makes the text overflow on the penultimate line instead of wrapping:
A failing test for the issue could look like this, if we consider that
roundLayoutResultsToPixelGrid
should accept any float dimensions and still never round down text nodes / nodes with custom measure functions:I'm using this patch on my app. but there's also this existing PR. I don't understand the algorithm well enough to know what the right fix should be...
Similar or adjacent issue: https://github.com/facebook/yoga/issues/1574