1
我寫了一個小巧的Swift程序,將數字添加到前一個數字,直到達到無窮遠。但是,在達到雙倍最大值之前達到無窮大。爲什麼Double在達到Double Max之前達到`Double.infinity`?
- 雙限1.79769313486232e + 308
- 距離限制是4.90703911098917e + 307
- 然而,8.07763763215622e + 307 + 1.3069892237634e + 308達到無窮大
這是爲什麼? (我回答了這個下文)。
運行它自己:
import Foundation
import Darwin
var current: Double = 1
var previous: Double = 0
var register: Double = 0
var infinity = Double.infinity
var isInfinite = infinity.isInfinite
var n = 1
while current < infinity {
register = current
current = previous + register
print("\(n): \(current)")
guard current != infinity else { break }
previous = register
n += 1
}
print("\n")
print("Double limit is \(DBL_MAX)")
print("Distance to limit is \(DBL_MAX - register)")
print("Yet, \(previous) + \(register) reached infinity")
在current = prev + register後添加一個打印電流 – Benj
掛了一分鐘我意識到我將它自己添加到它自己...... –
您不會添加之前的當前,您基本上每倍增加當前時間。 8.98846567431158e + 307 * 2將大於Double.max – courteouselk