简体   繁体   English

比较 swift 中的双值

[英]compare double values in swift

I want to compare two double values in swift我想比较 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.您在 Swift 中学到的第一件事是 {} 对于所有 if、if else 和 else 语句都是强制性的。

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.您不应该使用“>”、“<”和“=”符号来比较 float 或 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).特别是对于接近值的比较(float i = 0.56;float j = 0.56;比较 i == j 时,您有时会得到错误的结果)。 You should use constants from float.h, as it is described here .您应该使用 float.h 中的常量,如此所述。

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:由于计算机memory中此类数据的设备,用二进制符号<,>,==比较2个double数并不完全正确。例如比较2个double值:

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:请阅读基本 swift 语法的介绍章节:

var first_value = 10.20
var second_value = 20.30
if first_value > second_value {
    println("some statement")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM