3

I've got this line of code in Objective-C and I absolutely need to "translate it" to Swift.

BOOL hasBlur = blurRadius > __FLT_EPSILON__

Unfortunately I get this error:

Use of unresolved identifier __FLT_EPSILON_

Can you help me? Thanks

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sara Canducci
  • 6,231
  • 5
  • 19
  • 24

4 Answers4

6

For swift 3.1 OR above

CGFloat.ulpOfOne

OR

Double.ulpOfOne
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
Hiren Panchal
  • 2,963
  • 1
  • 25
  • 21
4

__FLT_EPSILON__ is a predefined macro of the compiler, apparently not available in Swift. But <float.h> defines

#define FLT_EPSILON __FLT_EPSILON__

and this is available in Swift as well:

let hasBlur = blurRadius > FLT_EPSILON

Update: Starting with Swift 4, FLT_EPSILON is deprecated. See Hiren Panchal's answer for an up-to-date solution.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

Looks like it comes from a line of Objective C that looked like:

#define __FLT_EPSILON__ 1.19209290e-7F

(Source: this thread). So you can safely replace it here with its literal value.

Community
  • 1
  • 1
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
0

just use this: let FLT_EPSILON = CGFloat(FLT_EPSILON)

dyljqq
  • 41
  • 3