79734863

Date: 2025-08-14 01:03:17
Score: 1
Natty:
Report link

The question is about the behavior of Swift’s Array.max when the array is of type Double and contains one or more NaN values alongside valid numeric values.

The official documentation simply states that max() returns “the sequence’s maximum element” and that it returns nil if the array is empty. However, this leaves ambiguity in cases where the concept of “maximum” is mathematically undefined, such as when NaN is involved, since any comparison with NaN is false.

The user points out that if “maximum” means “an element x such that x >= y for every other y in the array,” then an array containing a NaN technically doesn’t have a maximum element at all. That raises the question: should max() return nil, NaN, or something else in this scenario?

Through experimentation, the user observed that Swift’s implementation seems to ignore NaN values when determining the maximum and instead returns the maximum of the remaining non-NaN numbers. This approach is practical, but it’s not explicitly documented, which makes developers unsure whether it’s a guaranteed behavior or just an implementation detail that could change.

The user is seeking official Apple documentation that explicitly confirms this handling of NaN in Array.max, rather than having to infer it from experiments.

Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Victor Moni