I want to compare two double values in swift
var first_value = 10.20
var second_value = 20.30
if first_value > second_value
println("some statement")
how can we compare this type of stuff..
I want to compare two double values in swift
var first_value = 10.20
var second_value = 20.30
if first_value > second_value
println("some statement")
how can we compare this type of stuff..
The first thing you learn in Swift is {} are mandatory for all if, if else, and else statements.
So, your code should look like this:
var first_value = 10.20
var second_value = 20.30
if first_value > second_value {
print("first_value is greater than second_value")
}
else {
print("first_value is not greater than second_value ")
}
You should'n use '>', '<' and '=' signs to compare float or double. Especially for comparison of close values (float i = 0.56; float j = 0.56; time to time you'll get wrong results in comparison i == j). You should use constants from float.h, as it is described here.
Comparing 2 double numbers using binary signs <,>, == is not entirely correct because of the device of this type of data in the computer's memory. For example, compare 2 double values:
var a: Double = 10.0
var b: Double = 10.0
print(a == b) // false
So, you need determine decimal place precision for comparison.
public extension Double {
func greaterThan(_ value: Double, precise: Int) -> Bool {
let denominator: Double = pow(10.0, Double(precise))
let maxDiff: Double = 1 / denominator
let realDiff: Double = self - value
if fabs(realDiff) >= maxDiff, realDiff > 0 {
return true
} else {
return false
}
}
func greaterThanOrEqual(_ value: Double, precise: Int) -> Bool {
let denominator: Double = pow(10.0, Double(precise))
let maxDiff: Double = 1 / denominator
let realDiff: Double = self - value
if fabs(realDiff) >= maxDiff, realDiff >= 0 {
return true
} else if fabs(realDiff) <= maxDiff {
return true
} else {
return false
}
}
func lessThan(_ value: Double, precise: Int) -> Bool {
let denominator: Double = pow(10.0, Double(precise))
let maxDiff: Double = 1 / denominator
let realDiff: Double = self - value
if fabs(realDiff) >= maxDiff, realDiff < 0 {
return true
} else {
return false
}
}
func lessThanOrEqual(_ value: Double, precise: Int) -> Bool {
let denominator: Double = pow(10.0, Double(precise))
let maxDiff: Double = 1 / denominator
let realDiff: Double = self - value
if fabs(realDiff) >= maxDiff, realDiff <= 0 {
return true
} else if fabs(realDiff) <= maxDiff {
return true
} else {
return false
}
}
func equal(_ value: Double, precise: Int) -> Bool {
let denominator: Double = pow(10.0, Double(precise))
let maxDiff: Double = 1 / denominator
let realDiff: Double = self - value
if fabs(realDiff) <= maxDiff {
return true
} else {
return false
}
}
}
Usage:
var a: Double = 10.0
var b: Double = 10.0
print(a.equal(a, precise: 3)) // true
Updated Ans
var first_value = 10.20
var second_value = 20.30
if first_value.isLess(than: second_value) {
print("first_value is not greater than second_value ")
} else {
print("first_value is greater than second_value ")
}
Please read intro chapters to basic swift syntax:
var first_value = 10.20
var second_value = 20.30
if first_value > second_value {
println("some statement")
}