Asked • 06/26/19

How dangerous is it to compare floating point values?

I know `UIKit` uses `CGFloat` because of the resolution independent coordinate system.But every time I want to check if for example `frame.origin.x` is `0` it makes me feel sick: if (theView.frame.origin.x == 0) { // do important operation }Isn't `CGFloat` vulnerable to false positives when comparing with `==`, `<=`, `>=`, `<`, `>`?It is a floating point and they have unprecision problems: `0.0000000000041` for example.Is `Objective-C` handling this internally when comparing or can it happen that a `origin.x` which reads as zero does not compare to `0` as true?

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.