相关疑难解决方法(0)

将Int转换为Float会失去Swift中大数字的精度

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

7
推荐指数
1
解决办法
2595
查看次数

如何使我的求幂运算符适用于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)

operators swift

-1
推荐指数
1
解决办法
1167
查看次数