Open Bug 1660520 Opened 6 years ago Updated 6 months ago

Setting margins to 0.1 actually sets them to 0.100000001490116 (at least on Windows)

Categories

(Toolkit :: Printing, defect, P3)

x86_64
Windows 10
defect

Tracking

()

People

(Reporter: mkaply, Unassigned)

Details

(Whiteboard: [print2020] )

If you open Page setup and set the margin to 0. 1 then go to prefs, the margin is actually 0.100000001490116

I'm guessing this is a side effect of using a float. Maybe we shouldn't be?

Note that in 81, the new print UI gets enabled by default. This has a dropdown with a choice of preset margin values so there's no opportunity to input a value and reproduce this bug. However, we do plan to add back the custom margins and we should confirm the new UI doesnt have the same problem at that point.

Mockups for the new print UI are here: https://mozilla.invisionapp.com/share/MFWMR6H5X3K#/screens/411381710

Severity: -- → S3
Priority: -- → P3
Whiteboard: [print2020_v82]

[Moving front-end printing v82 bugs to v83.]

Whiteboard: [print2020_v82] → [print2020_v83]
Whiteboard: [print2020_v83] → [print2020_v84]
Whiteboard: [print2020_v84] → [print2020_v85]

(Moving bugs to 86, part 2.)

Whiteboard: [print2020_v85] → [print2020_v86]
Whiteboard: [print2020_v86] → [print2020_v88]
Whiteboard: [print2020_v88] → [print2020]

I verified this is still happening internally, but doesn't show up in the UI anymore.


Tracked down the root cause. The 0.100000001490116 value is the 32-bit float representation of 0.1, not a double-precision artifact — which means the precision loss is happening somewhere upstream of the IDL getter that returns double.

The culprit is gfx/src/nsCoord.h. The twips-to-inches conversion macros do all their math in 32-bit float:

#define TWIPS_PER_POINT_FLOAT 20.0f
#define POINTS_PER_INCH_FLOAT 72.0f

inline float NSTwipsToUnits(float aTwips, float aUnitsPerPoint) {
  return aTwips * (aUnitsPerPoint / TWIPS_PER_POINT_FLOAT);
}

#define NS_TWIPS_TO_INCHES(x) NSTwipsToUnits((x), 1.0f / POINTS_PER_INCH_FLOAT)

Trace for a 0.1-inch margin:

  1. UI parses 0.1 as a double, sends it down.

  2. Storage converts to twips: 0.1 * 1440 = 144. Integer, exact. mMargin.top = 144 (an nsIntMargin).

  3. Getter in widget/nsPrintSettingsImpl.cpp:240:

    NS_IMETHODIMP nsPrintSettings::GetMarginTop(double* aMarginTop) {
      *aMarginTop = NS_TWIPS_TO_INCHES(mMargin.top);
    }
    

    NS_TWIPS_TO_INCHES(144) expands to NSTwipsToUnits(144, 1.0f / 72.0f). All operands are 32-bit float. 1.0f / 72.0f is the nearest 32-bit float to ~0.01389, (1.0f / 72.0f) / 20.0f is the nearest 32-bit float to ~0.000694, and 144 * 0.000694f is exactly the nearest 32-bit float to 0.1, which is 0.100000001490116119384765625.

  4. NSTwipsToUnits returns float. The IDL declares the getter as double* aMarginTop. Float-to-double widening preserves those low-order bits — it doesn't introduce them, they're already in the 32-bit value. UI receives 0.100000001490116 and displays it.

This also explains Sam's comment #1 from 2020: the new print UI eliminating user margin input only avoids the display path under specific flows. The same precision loss happens any time anything reads margins through the IDL — including any code path that reads the current margin to persist it back to prefs, which is how you end up with 0.100000001490116 saved to disk.

Fix option (surgical): bypass the float macros for the print getters and use exact integer-twips → double math. Patch is roughly 12 lines covering the four margin attributes across mMargin, mEdge, and mUnwriteableMargin in widget/nsPrintSettingsImpl.cpp:

NS_IMETHODIMP nsPrintSettings::GetMarginTop(double* aMarginTop) {
  *aMarginTop = mMargin.top / 1440.0;  // 1440 twips per inch, double math
}
// ... same shape for marginLeft/bottom/right, edgeTop/left/bottom/right,
//     unwriteableMarginTop/left/bottom/right

1440.0 is exact in double; integer twips divided by an exact double constant produces the closest-double-to-the-true-value, which round-trips as "0.1" for any twip value cleanly divisible by 144.

Test plan: set margin to 0.1 via the API, read it back, assert the returned double equals 0.1 (i.e. Math.abs(returnedValue - 0.1) < Number.EPSILON). A mochitest exercising nsIPrintSettings directly is enough; doesn't need to involve the UI.

Fix option (wider) and why I don't recommend it: change NSUnitsToTwips/NSTwipsToUnits themselves to use double. That's the "correct" fix at the root, but those macros are called all over gfx and layout, where 32-bit float was likely a deliberate perf/cache choice for hot paths. The drive-by risk isn't worth the elegance — better to do the narrow fix here, leave gfx alone, and let any layout-side migration happen on its own schedule.

Not taking this myself right now; recording so the analysis isn't lost.

You need to log in before you can comment on or make changes to this bug.