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.
(lldb) po (398.0 / 165.0).rounded()e-321
The expression may even crash when called from an attached debugger (the following expression returns 2.4121212... when no debugger attached):
(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:
(lldb) po CGFloat(roundf(Float(availableWidth / (cellRatio * (tileType.sectionHeight - 50)))))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?
6
0
1.8k