问题描述
我刚刚注意到 Swift 对 Int 和 Double 进行了一些类型转换.当我尝试评估
I've just noticed that Swift does some type casting over Int and Double.When I try to evaluate
(10 / 3.0) - (10 / 3)
0.333...
是预期的,但实际上是 0.0
.有人可以解释一下吗?
0.333...
is expected, but it's actually 0.0
.Could someone explain this please?
推荐答案
是的,我也觉得这很令人惊讶.Double
符合 FloatLiteralConvertible
和 IntegerLiteralConvertible
(Swift 3 中的 ExpressibleByFloatLiteral
和 ExpressibleByIntegerLiteral
).因此一个Double
可以用浮点字面量
Yes, I also found this quite surprising. Double
conforms to both FloatLiteralConvertible
and IntegerLiteralConvertible
(ExpressibleByFloatLiteral
and ExpressibleByIntegerLiteral
in Swift 3). Therefore aDouble
can be initialized with floating point literal
let a = 3.0
或使用整数文字:
let b : Double = 10
(对于其他浮点类型,如 Float
和CGFloat
.)
(The same is true for other floating point types like Float
andCGFloat
.)
现在对我们所有有(Objective-)C背景的人来说可能是出乎意料的这两个陈述
Now it might be unexpected for all of us with an (Objective-)C backgroundthat both statements
let x : Double = 10/4 // x = 2.5 . Really? Yes!
let y = 10/4 as Double // Same here ...
将值 0.25
分配给变量.从上下文来看,结果除法必须是 Double
并且 Swift 不会隐式转换类型.因此/
必须是浮点除法运算符
assign the value 0.25
to the variable. From the context, the result of thedivision must be a Double
and Swift does not implicitly convert types.Therefore /
must be the floating point division operator
func /(lhs: Double, rhs: Double) -> Double
所以编译器从文字中创建两个参数作为 Double
s10"和4".(如果 10/4
被视为两个整数的除法那么结果也将是一个整数,并且不能被赋值到 Double
.)
so the compiler creates both arguments as Double
s from the literals"10" and "4". (If 10/4
were treated as the division of two integersthen the result would also be an integer, and that cannot be assignedto a Double
.)
注意这与
let z = Double(10/4) // z = 2.0 . (I just thought that I understood it &%$!?)
进行整数除法并将结果转换为Double
.Double
有一个 init(_ v: Int)
构造函数,因此 10/4
可以在这里被视为两个整数的除法.
which does an integer division and converts the result to Double
.Double
has an init(_ v: Int)
constructor, and therefore 10/4
can be treated as the division of two integers here.
如果我们总结这些结果,真的有点奇怪:
It really looks a bit strange if we summarize these results:
let x : Double = 10/4 // x = 2.5
let y = 10/4 as Double // y = 2.5
let z = Double(10/4) // z = 2.0
现在我们可以将这些结果应用到您的表达式中
Now we can apply these results to your expression
(10 / 3.0) - (10 / 3)
第一部分(10/3.0)
只能是一个Double
,因此-
必须是浮点减法运算符
The first part (10 / 3.0)
can only be a Double
, therefore -
must be the floating point subtraction operator
func -(lhs: Double, rhs: Double) -> Double
因此 (10/3)
也必须是 Double
.同样,/
必须是浮点除法运算符,所以 10
和 3
被视为 Double
常量.
and thus (10 / 3)
must also be a Double
. Again, /
must be the floating point division operator, so 10
and 3
are treated as Double
constants.
因此表达式等价于
(Double(10) / 3.0) - (Double(10) / Double(3))
并计算为 0.0
.如果将表达式更改为
and evaluates to 0.0
. If you change the expression to
(10 / 3.0) - Double(10 / 3)
那么结果是 0.333...
因为在这种情况下,10/3
是两个整数常量的除法,如上所述.
then the result is 0.333...
because in this context, 10 / 3
is the division of two integer constants, as explained above.
这篇关于奇怪的 Swift 数字类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!