XCode 6.3.1 Swift 1.2
let value: Int = 220904525
let intmax = Int.max
let float = Float(value) // Here is an error probably
let intFromFloat = Int(float)
let double = Double(value)
println("intmax=\(intmax) value=\(value) float=\(float) intFromFloat=\(intFromFloat) double=\(double)")
// intmax=9223372036854775807 value=220904525 float=2.20905e+08 intFromFloat=220904528 double=220904525.0
Run Code Online (Sandbox Code Playgroud)
初始值是220904525.但是当我将它转换为浮动时它变为220904528.为什么?
floating-point ios floating-point-precision floating-point-conversion swift
我在Swift中创建了一个^^运算符.如何使其像所有其他运算符一样使用Integers和Doubles?
infix operator ^^ { }
func ^^ (number:Int, power: Int) -> Int {
var result = power > 0 ? number : 0
if power > 1 { for x in 1..<power { result *= number } }
return result
}
Run Code Online (Sandbox Code Playgroud)