dfri dfri - 4 years ago 376
Swift Question

Why is DBL_MIN not negative (in Swift)?

Why does the expression

DBL_MIN < 0
returns
false
?

Why do the comparisons against
DLB_MIN
seem to indicate that
DBL_MIN
is positive?

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 */


I suspect it has something to do with floating point precision, but I can't really explain it myself.

I am using Swift 2.1.1 and XCode 7.2.

Answer Source

It is not minimal in the sense that is the largest negative number but the number closest to zero representable as Double:

2.225073858507201 * 10 ^ -308

That simply is a positive value. The - represents a negative exponent, not a negative overall value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download