Swift: how to compare the first n digits after the decimal point for CGFloat?

How can you compare if A and B, both CGFloats , are equal to five digits after the decimal point? This is necessary because of this problem .

+7
floating-point swift
source share
1 answer

Same as comparing floating point numbers in any other language.

Take the absolute value of the difference of numbers and compare it with your acceptable delta.

 let delta: CGFloat = 0.00001 let a: CGFloat = 3.141592 let b: CGFloat = 3.141593 if abs(ab) < delta { println("close enough for government work") } 
+11
source share

All Articles