CGFloats rounding issues on simulator (x86_64)

When using rounding methods (rounded(), rounded(toPlaces:), round(_: CGFloat)) on CGFloats with the simulator, the returned value is not rounded at all or sometimes makes no sense at all.
Code Block
(lldb) po (398.0 / 165.0).rounded()
2.53e-321

The expression may even crash when called from an attached debugger (the following expression returns 2.4121212... when no debugger attached):
Code Block
(lldb) po (availableWidth / (cellRatio * (tileType.sectionHeight - 50))).rounded()
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x7c).
The process has been returned to the state before expression evaluation.

The same operations are OK on a real device or in a playground on a Mac. It's also OK when converting the CGFloat to a Float and calling roundf() as follows:
Code Block
(lldb) po CGFloat(roundf(Float(availableWidth / (cellRatio * (tileType.sectionHeight - 50)))))
2.0

Finally here are the results when passing various rounding rules to the rounded() method:
  • .toNearestOrAwayFromZero: crash in debugger

  • .toNearestOrEven: 2.412121212121212 (unchanged value)

  • .up: inf

  • .down: 2.412121212121212 (unchanged value)

  • .towardZero: 2.412121212121212 (unchanged value)

  • .awayFromZero: inf

Is there something wrong with computing rounded values of CGFloats on a simulator?

The expression may even crash when called from an attached debugger (the following expression returns 2.4121212... when no debugger attached)

Is there something wrong with computing rounded values of CGFloats on a simulator?

It seems to be a bug of the debugger. Have you sent a bug report?
Can you provide more details about your configuration here? I tried this myself and didn’t see any problems. Specifically:
  1. Using Xcode 12.5 on macOS 11.3.1, I created a new app from the iOS > App template.

  2. I set a breakpoint in the viewDidLoad.

  3. I ran it in the iPhone 12 mini simulator (running the simulated iOS 14.5).

  4. When it stopped at the breakpoint I ran your first command:

Code Block
(lldb) po (398.0 / 165.0).rounded()
2.0


This is what you were expecting, right?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Hi eskimo,

Yes, 2.0 is the expected result.

Here is my detailed configuration:
• MacBook Pro 2020, Intel processor
• MacOS 11.4 (but reproduced yesterday with 11.3.x)
• Xcode 12.5
• Simulators: iPhone 8 (14.5), iPhone 8 (12.4), iPhone 8 Plus (14.5), iPhone 12 Pro Max

I always get the following result:
Code Block
(lldb) po (398.0 / 165.0).rounded()
2.53e-321


You don't even have to set a breakpoint to get an unexpected result (though different) as the instruction
Code Block
print("Rounded result: \((398.0 / 165.0).rounded())")

logs
Code Block
Rounded result: 2.412121212121212

FYI I've filled in a bug report (FB9118612).

You don't even have to set a breakpoint to get an unexpected result (though different) as the instruction

Code Block
print("Rounded result: \((398.0 / 165.0).rounded())")
As far as I tried your code (Mac mini 2018, macOS 11.4, Xcode 12.5, iPhone 8 plus simulator (14.5)), the output was:
Code Block
Rounded result: 2.0

I ended up cleaning the entire Xcode "iOS DeviceSupport" folder as it proved to be useful in the past and I cannot reproduce the issue anymore! However, the other developer in my team has the same issue so it's not specific to my computer.

I guess we will never know what was going on.

I've filled in a bug report (FB9118612).

Thanks for that.

However, the other developer in my team has the same issue so it's not specific to my computer.

Have them try it with a new project. That’ll confirm that the issue is somehow tied to your project’s state.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

CGFloats rounding issues on simulator (x86_64)
 
 
Q