gpt4 book ai didi

swift - 在 Swift 中比较 double 和整数时,运算符 '<' 的使用不明确

转载 作者:行者123 更新时间:2023-12-05 02:25:49 25 4
gpt4 key购买 nike

为什么 swift 在比较 double 和整数时表现得如此奇怪?

考虑以下用于 repl/playground 的代码:

12>12.0        //false 

13 > 12.0 //true
13.0 > 12 //true

13 >= 12.0 //true
13 <= 12.0 //false

12.0 < 13 //Error: Ambiguous use of operator '<'
12 < 13.0 //Error: Ambiguous use of operator '<'

这里运算符的使用有歧义到底是什么意思?

调试控制台输出也不是特别有用:

error: ambiguous use of operator '<'
12 < 13.0
^

Foundation.RunLoop:18:32: note: found this candidate
public static func < (lhs: RunLoop.SchedulerTimeType.Stride, rhs: RunLoop.SchedulerTimeType.Stride) -> Bool
^

Foundation.Decimal:4:24: note: found this candidate
public static func < (lhs: Decimal, rhs: Decimal) -> Bool
^

Foundation.OperationQueue:18:32: note: found this candidate
public static func < (lhs: OperationQueue.SchedulerTimeType.Stride, rhs: OperationQueue.SchedulerTimeType.Stride) -> Bool
^

Dispatch.DispatchQueue:22:32: note: found this candidate
public static func < (lhs: DispatchQueue.SchedulerTimeType.Stride, rhs: DispatchQueue.SchedulerTimeType.Stride) -> Bool

UPD:澄清一下,我知道我可以显式转换值。

问题是为什么这种语言会以这种方式运行,它是否被设计为使用 < 以这种方式运行?运营商?

最佳答案

我认为运算符 >、>=、=、!= 是由 Swift 的转换机制实现的,用于比较。操作符 <, == 是两个特殊的操作符,必须是同一类型才能比较。

我找到了一份 Swift 文档,里面有一些关于它的信息。 Compare

关于swift - 在 Swift 中比较 double 和整数时,运算符 '<' 的使用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74284654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com