Skip to content

GridLengthHelper.FromPixels and FromValueAndType truncate the value to float #25242

Closed

Description

@MartinZikmund

Current behavior

GridLengthHelper.FromPixels(double) and GridLengthHelper.FromValueAndType(double, GridUnitType) narrow the argument to float before building the GridLength, even though GridLength.Value is a double:

public static GridLength FromPixels(double pixels) => new GridLength((float)pixels, GridUnitType.Pixel);
public static GridLength FromValueAndType(double value, GridUnitType type) => new GridLength((float)value, type);

So precision is lost:

var gl = GridLengthHelper.FromPixels(0.1);
Console.WriteLine(gl.Value == 0.1);   // False on Uno (0.10000000149011612), True on WinUI
Console.WriteLine(GridLengthHelper.FromPixels(123456.789).Value); // 123456.7890625 on Uno

Expected behavior

The helper keeps the double it was given, like WinUI. WinUI's GridLengthFactory::FromValueAndType takes DOUBLE and stores it as-is (pReturnValue->Value = (type == Auto) ? 1.0 : value;). FromPixels just forwards to it.

Source: GridLength_Partial.cpp (WinUI C++ source inspection, not a runtime repro).

How to reproduce it

  1. Create any Uno app (or runtime test).
  2. Run:
    var a = GridLengthHelper.FromPixels(0.1);
    var b = GridLengthHelper.FromValueAndType(123456.789, GridUnitType.Pixel);
    System.Diagnostics.Debug.WriteLine($"{a.Value == 0.1} {b.Value == 123456.789}");
  3. Uno prints False False; WinUI prints True True.

Confirmed by code inspection on master (the (float) casts are plain C# narrowing, so the result follows directly from the language rules).

Workaround

new GridLength(value, GridUnitType.Pixel) keeps double precision.

Works on UWP/WinUI

Yes.

Environment

Other

NuGet package version

master @ cc48541

Affected platforms

Other (all heads; shared code)

IDE

No response

Relevant plugins

No response

Anything else we need to know?

Root cause: GridLengthHelper.cs (lines 12-14). Suggested fix: drop the (float) casts (and use 1.0 instead of 1.0f for Auto/OneStar, harmless but consistent).

Practical impact: code that writes GridLengthHelper.FromPixels(x) and later compares column.Width.Value against x (for example to skip redundant writes) can miss on large or fractional values.

Invalid input (NaN, infinity, negative, unknown unit) already throws, because the GridLength constructor validates it, so only the precision loss is a divergence.

Activity

  1. added
    kind/bug
    good first issue
    triage/untriaged
    project/layout 🧱
    platform/all
    area/grid
    difficulty/starter 🚀
    on Oct 11, 2026
  2. brunoapolinariodev commented on Oct 15, 2026

    @brunoapolinariodev
    Contributor

    Hi! I opened a PR for this: #25511

    It removes the (float) casts in GridLengthHelper.FromPixels and FromValueAndType so the double value is kept, and adds unit tests covering Pixel and Star.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/griddifficulty/starter 🚀good first issuekind/bugplatform/allproject/layout 🧱triage/untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions