为什么表达式DBL_MIN 0返回false? 为什么与DLB_MIN的比较似乎表明DBL_MIN是正数? let a = DBL_MIN // 2.225073858507201e-308let b = DBL_MAX // 1.797693134862316e+308if a 0.0 { print("1. DBL_MIN is indeed less than zero")
为什么与DLB_MIN的比较似乎表明DBL_MIN是正数?
let a = DBL_MIN // 2.225073858507201e-308 let b = DBL_MAX // 1.797693134862316e+308 if a < 0.0 { print("1. DBL_MIN is indeed less than zero") // doesn't print (unexpected) } if DBL_MIN < 0.0 { print("2. DBL_MIN is indeed less than zero") // doesn't print (unexpected) } if DBL_MIN > 0.0 { print("3. DBL_MIN is larger than zero?") // prints (unexpected) } if DBL_MIN > DBL_MAX { print("4. DBL_MIN did some strange flip?") // doesn't print (OK) } if b > 0.0 { print("5. DBL_MAX is indeed larger than zero") // prints (OK) } /* printout: 3. DBL_MIN is larger than zero? 5. DBL_MAX is indeed larger than zero */
我怀疑它与浮点精度有关,但我自己无法解释它.
我使用的是Swift 2.1.1和XCode 7.2.
在最大负数的意义上它不是最小的,但是最接近零的数字可表示为Double:2.225073858507201 * 10 ^ -308
这只是一个积极的价值. – 代表负指数,而不是负整数值.