问题描述
我刚刚注意到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
( 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
/ em>:
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
s从字面值
10和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数字类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!