 dfri - 4 years ago 100
Swift Question

# Why do the expression "DBL_MIN < 0" return false in Swift?

Why do comparison against

`DBL_MIN`
behave as if
`DBL_MIN`
is a positive value, in the sense that the expression
`DBL_MIN < 0`
returns
`false`
?

``````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. Anonymous
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.